PageRenderTime 54ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/Zend/View/Helper/Doctype.php

http://grupal.googlecode.com/
PHP | 242 lines | 125 code | 17 blank | 100 comment | 9 complexity | cfb1bd5cdf4f6c5155dccc4fa6996cca MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @version $Id: Doctype.php 25100 2012-11-07 20:26:37Z rob $
  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-2012 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 XHTML1_RDFA11 = 'XHTML1_RDFA11';
  45. const XHTML_BASIC1 = 'XHTML_BASIC1';
  46. const XHTML5 = 'XHTML5';
  47. const HTML4_STRICT = 'HTML4_STRICT';
  48. const HTML4_LOOSE = 'HTML4_LOOSE';
  49. const HTML4_FRAMESET = 'HTML4_FRAMESET';
  50. const HTML5 = 'HTML5';
  51. const CUSTOM_XHTML = 'CUSTOM_XHTML';
  52. const CUSTOM = 'CUSTOM';
  53. /**#@-*/
  54. /**
  55. * Default DocType
  56. * @var string
  57. */
  58. protected $_defaultDoctype = self::HTML4_LOOSE;
  59. /**
  60. * Registry containing current doctype and mappings
  61. * @var ArrayObject
  62. */
  63. protected $_registry;
  64. /**
  65. * Registry key in which helper is stored
  66. * @var string
  67. */
  68. protected $_regKey = 'Zend_View_Helper_Doctype';
  69. /**
  70. * Constructor
  71. *
  72. * Map constants to doctype strings, and set default doctype
  73. *
  74. * @return void
  75. */
  76. public function __construct()
  77. {
  78. if (!Zend_Registry::isRegistered($this->_regKey)) {
  79. $this->_registry = new ArrayObject(array(
  80. 'doctypes' => array(
  81. self::XHTML11 => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">',
  82. self::XHTML1_STRICT => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
  83. self::XHTML1_TRANSITIONAL => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
  84. self::XHTML1_FRAMESET => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">',
  85. self::XHTML1_RDFA => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">',
  86. self::XHTML1_RDFA11 => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.1//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-2.dtd">',
  87. self::XHTML_BASIC1 => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.0//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd">',
  88. self::XHTML5 => '<!DOCTYPE html>',
  89. self::HTML4_STRICT => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">',
  90. self::HTML4_LOOSE => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">',
  91. self::HTML4_FRAMESET => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">',
  92. self::HTML5 => '<!DOCTYPE html>',
  93. )
  94. ));
  95. Zend_Registry::set($this->_regKey, $this->_registry);
  96. $this->setDoctype($this->_defaultDoctype);
  97. } else {
  98. $this->_registry = Zend_Registry::get($this->_regKey);
  99. }
  100. }
  101. /**
  102. * Set or retrieve doctype
  103. *
  104. * @param string $doctype
  105. * @return Zend_View_Helper_Doctype
  106. */
  107. public function doctype($doctype = null)
  108. {
  109. if (null !== $doctype) {
  110. switch ($doctype) {
  111. case self::XHTML11:
  112. case self::XHTML1_STRICT:
  113. case self::XHTML1_TRANSITIONAL:
  114. case self::XHTML1_FRAMESET:
  115. case self::XHTML_BASIC1:
  116. case self::XHTML1_RDFA:
  117. case self::XHTML1_RDFA11:
  118. case self::XHTML5:
  119. case self::HTML4_STRICT:
  120. case self::HTML4_LOOSE:
  121. case self::HTML4_FRAMESET:
  122. case self::HTML5:
  123. $this->setDoctype($doctype);
  124. break;
  125. default:
  126. if (substr($doctype, 0, 9) != '<!DOCTYPE') {
  127. require_once 'Zend/View/Exception.php';
  128. $e = new Zend_View_Exception('The specified doctype is malformed');
  129. $e->setView($this->view);
  130. throw $e;
  131. }
  132. if (stristr($doctype, 'xhtml')) {
  133. $type = self::CUSTOM_XHTML;
  134. } else {
  135. $type = self::CUSTOM;
  136. }
  137. $this->setDoctype($type);
  138. $this->_registry['doctypes'][$type] = $doctype;
  139. break;
  140. }
  141. }
  142. return $this;
  143. }
  144. /**
  145. * Set doctype
  146. *
  147. * @param string $doctype
  148. * @return Zend_View_Helper_Doctype
  149. */
  150. public function setDoctype($doctype)
  151. {
  152. $this->_registry['doctype'] = $doctype;
  153. return $this;
  154. }
  155. /**
  156. * Retrieve doctype
  157. *
  158. * @return string
  159. */
  160. public function getDoctype()
  161. {
  162. return $this->_registry['doctype'];
  163. }
  164. /**
  165. * Get doctype => string mappings
  166. *
  167. * @return array
  168. */
  169. public function getDoctypes()
  170. {
  171. return $this->_registry['doctypes'];
  172. }
  173. /**
  174. * Is doctype XHTML?
  175. *
  176. * @return boolean
  177. */
  178. public function isXhtml()
  179. {
  180. return (stristr($this->getDoctype(), 'xhtml') ? true : false);
  181. }
  182. /**
  183. * Is doctype strict?
  184. *
  185. * @return boolean
  186. */
  187. public function isStrict()
  188. {
  189. switch ( $this->getDoctype() )
  190. {
  191. case self::XHTML1_STRICT:
  192. case self::XHTML11:
  193. case self::HTML4_STRICT:
  194. return true;
  195. default:
  196. return false;
  197. }
  198. }
  199. /**
  200. * Is doctype HTML5? (HeadMeta uses this for validation)
  201. *
  202. * @return booleean
  203. */
  204. public function isHtml5() {
  205. return (stristr($this->doctype(), '<!DOCTYPE html>') ? true : false);
  206. }
  207. /**
  208. * Is doctype RDFa?
  209. *
  210. * @return booleean
  211. */
  212. public function isRdfa() {
  213. return (stristr($this->getDoctype(), 'rdfa') ? true : false);
  214. }
  215. /**
  216. * String representation of doctype
  217. *
  218. * @return string
  219. */
  220. public function __toString()
  221. {
  222. $doctypes = $this->getDoctypes();
  223. return $doctypes[$this->getDoctype()];
  224. }
  225. }