PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/View/Helper/Doctype.php

https://gitlab.com/devtoannh/cafe
PHP | 239 lines | 122 code | 17 blank | 100 comment | 9 complexity | 26ccadcba4c30e5003681b15bf868014 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_View
  17. * @subpackage Helper
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @version $Id: Doctype.php 24201 2011-07-05 16:22:04Z matthew $
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. /** Zend_Registry */
  23. require_once 'Zend/Registry.php';
  24. /** Zend_View_Helper_Abstract.php */
  25. require_once 'Zend/View/Helper/Abstract.php';
  26. /**
  27. * Helper for setting and retrieving the doctype
  28. *
  29. * @package Zend_View
  30. * @subpackage Helper
  31. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_View_Helper_Doctype extends Zend_View_Helper_Abstract
  35. {
  36. /**#@+
  37. * DocType constants
  38. */
  39. const XHTML11 = 'XHTML11';
  40. const XHTML1_STRICT = 'XHTML1_STRICT';
  41. const XHTML1_TRANSITIONAL = 'XHTML1_TRANSITIONAL';
  42. const XHTML1_FRAMESET = 'XHTML1_FRAMESET';
  43. const XHTML1_RDFA = 'XHTML1_RDFA';
  44. const XHTML_BASIC1 = 'XHTML_BASIC1';
  45. const XHTML5 = 'XHTML5';
  46. const HTML4_STRICT = 'HTML4_STRICT';
  47. const HTML4_LOOSE = 'HTML4_LOOSE';
  48. const HTML4_FRAMESET = 'HTML4_FRAMESET';
  49. const HTML5 = 'HTML5';
  50. const CUSTOM_XHTML = 'CUSTOM_XHTML';
  51. const CUSTOM = 'CUSTOM';
  52. /**#@-*/
  53. /**
  54. * Default DocType
  55. * @var string
  56. */
  57. protected $_defaultDoctype = self::HTML4_LOOSE;
  58. /**
  59. * Registry containing current doctype and mappings
  60. * @var ArrayObject
  61. */
  62. protected $_registry;
  63. /**
  64. * Registry key in which helper is stored
  65. * @var string
  66. */
  67. protected $_regKey = 'Zend_View_Helper_Doctype';
  68. /**
  69. * Constructor
  70. *
  71. * Map constants to doctype strings, and set default doctype
  72. *
  73. * @return void
  74. */
  75. public function __construct()
  76. {
  77. if (!Zend_Registry::isRegistered($this->_regKey)) {
  78. $this->_registry = new ArrayObject(array(
  79. 'doctypes' => array(
  80. self::XHTML11 => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">',
  81. self::XHTML1_STRICT => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
  82. self::XHTML1_TRANSITIONAL => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
  83. self::XHTML1_FRAMESET => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">',
  84. self::XHTML1_RDFA => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">',
  85. self::XHTML_BASIC1 => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.0//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd">',
  86. self::XHTML5 => '<!DOCTYPE html>',
  87. self::HTML4_STRICT => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">',
  88. self::HTML4_LOOSE => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">',
  89. self::HTML4_FRAMESET => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">',
  90. self::HTML5 => '<!DOCTYPE html>',
  91. )
  92. ));
  93. Zend_Registry::set($this->_regKey, $this->_registry);
  94. $this->setDoctype($this->_defaultDoctype);
  95. } else {
  96. $this->_registry = Zend_Registry::get($this->_regKey);
  97. }
  98. }
  99. /**
  100. * Set or retrieve doctype
  101. *
  102. * @param string $doctype
  103. * @return Zend_View_Helper_Doctype
  104. */
  105. public function doctype($doctype = null)
  106. {
  107. if (null !== $doctype) {
  108. switch ($doctype) {
  109. case self::XHTML11:
  110. case self::XHTML1_STRICT:
  111. case self::XHTML1_TRANSITIONAL:
  112. case self::XHTML1_FRAMESET:
  113. case self::XHTML_BASIC1:
  114. case self::XHTML1_RDFA:
  115. case self::XHTML5:
  116. case self::HTML4_STRICT:
  117. case self::HTML4_LOOSE:
  118. case self::HTML4_FRAMESET:
  119. case self::HTML5:
  120. $this->setDoctype($doctype);
  121. break;
  122. default:
  123. if (substr($doctype, 0, 9) != '<!DOCTYPE') {
  124. require_once 'Zend/View/Exception.php';
  125. $e = new Zend_View_Exception('The specified doctype is malformed');
  126. $e->setView($this->view);
  127. throw $e;
  128. }
  129. if (stristr($doctype, 'xhtml')) {
  130. $type = self::CUSTOM_XHTML;
  131. } else {
  132. $type = self::CUSTOM;
  133. }
  134. $this->setDoctype($type);
  135. $this->_registry['doctypes'][$type] = $doctype;
  136. break;
  137. }
  138. }
  139. return $this;
  140. }
  141. /**
  142. * Set doctype
  143. *
  144. * @param string $doctype
  145. * @return Zend_View_Helper_Doctype
  146. */
  147. public function setDoctype($doctype)
  148. {
  149. $this->_registry['doctype'] = $doctype;
  150. return $this;
  151. }
  152. /**
  153. * Retrieve doctype
  154. *
  155. * @return string
  156. */
  157. public function getDoctype()
  158. {
  159. return $this->_registry['doctype'];
  160. }
  161. /**
  162. * Get doctype => string mappings
  163. *
  164. * @return array
  165. */
  166. public function getDoctypes()
  167. {
  168. return $this->_registry['doctypes'];
  169. }
  170. /**
  171. * Is doctype XHTML?
  172. *
  173. * @return boolean
  174. */
  175. public function isXhtml()
  176. {
  177. return (stristr($this->getDoctype(), 'xhtml') ? true : false);
  178. }
  179. /**
  180. * Is doctype strict?
  181. *
  182. * @return boolean
  183. */
  184. public function isStrict()
  185. {
  186. switch ( $this->getDoctype() )
  187. {
  188. case self::XHTML1_STRICT:
  189. case self::XHTML11:
  190. case self::HTML4_STRICT:
  191. return true;
  192. default:
  193. return false;
  194. }
  195. }
  196. /**
  197. * Is doctype HTML5? (HeadMeta uses this for validation)
  198. *
  199. * @return booleean
  200. */
  201. public function isHtml5() {
  202. return (stristr($this->doctype(), '<!DOCTYPE html>') ? true : false);
  203. }
  204. /**
  205. * Is doctype RDFa?
  206. *
  207. * @return booleean
  208. */
  209. public function isRdfa() {
  210. return (stristr($this->getDoctype(), 'rdfa') ? true : false);
  211. }
  212. /**
  213. * String representation of doctype
  214. *
  215. * @return string
  216. */
  217. public function __toString()
  218. {
  219. $doctypes = $this->getDoctypes();
  220. return $doctypes[$this->getDoctype()];
  221. }
  222. }