3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\View\Helper
;
13 use Zend\View\Exception
;
16 * Helper for setting and retrieving the doctype
18 class Doctype
extends AbstractHelper
23 const XHTML11
= 'XHTML11';
24 const XHTML1_STRICT
= 'XHTML1_STRICT';
25 const XHTML1_TRANSITIONAL
= 'XHTML1_TRANSITIONAL';
26 const XHTML1_FRAMESET
= 'XHTML1_FRAMESET';
27 const XHTML1_RDFA
= 'XHTML1_RDFA';
28 const XHTML1_RDFA11
= 'XHTML1_RDFA11';
29 const XHTML_BASIC1
= 'XHTML_BASIC1';
30 const XHTML5
= 'XHTML5';
31 const HTML4_STRICT
= 'HTML4_STRICT';
32 const HTML4_LOOSE
= 'HTML4_LOOSE';
33 const HTML4_FRAMESET
= 'HTML4_FRAMESET';
34 const HTML5
= 'HTML5';
35 const CUSTOM_XHTML
= 'CUSTOM_XHTML';
36 const CUSTOM
= 'CUSTOM';
44 protected $defaultDoctype = self
::HTML4_LOOSE
;
47 * Registry containing current doctype and mappings
54 * @var ArrayObject Shared doctypes to use throughout all instances
56 protected static $registeredDoctypes;
61 * Map constants to doctype strings, and set default doctype
63 public function __construct()
65 if (null === static::$registeredDoctypes) {
66 static::registerDefaultDoctypes();
67 $this->setDoctype($this->defaultDoctype
);
69 $this->registry
= static::$registeredDoctypes;
73 * Set or retrieve doctype
75 * @param string $doctype
76 * @throws Exception\DomainException
79 public function __invoke($doctype = null)
81 if (null !== $doctype) {
84 case self
::XHTML1_STRICT
:
85 case self
::XHTML1_TRANSITIONAL
:
86 case self
::XHTML1_FRAMESET
:
87 case self
::XHTML_BASIC1
:
88 case self
::XHTML1_RDFA
:
89 case self
::XHTML1_RDFA11
:
91 case self
::HTML4_STRICT
:
92 case self
::HTML4_LOOSE
:
93 case self
::HTML4_FRAMESET
:
95 $this->setDoctype($doctype);
98 if (substr($doctype, 0, 9) != '<!DOCTYPE') {
99 throw new Exception\
DomainException('The specified doctype is malformed');
101 if (stristr($doctype, 'xhtml')) {
102 $type = self
::CUSTOM_XHTML
;
104 $type = self
::CUSTOM
;
106 $this->setDoctype($type);
107 $this->registry
['doctypes'][$type] = $doctype;
116 * String representation of doctype
120 public function __toString()
122 $doctypes = $this->getDoctypes();
124 return $doctypes[$this->getDoctype()];
128 * Register the default doctypes we understand
132 protected static function registerDefaultDoctypes()
134 static::$registeredDoctypes = new ArrayObject(array(
136 self
::XHTML11
=> '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">',
137 self
::XHTML1_STRICT
=> '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
138 self
::XHTML1_TRANSITIONAL
=> '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
139 self
::XHTML1_FRAMESET
=> '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">',
140 self
::XHTML1_RDFA
=> '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">',
141 self
::XHTML1_RDFA11
=> '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.1//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-2.dtd">',
142 self
::XHTML_BASIC1
=> '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.0//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd">',
143 self
::XHTML5
=> '<!DOCTYPE html>',
144 self
::HTML4_STRICT
=> '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">',
145 self
::HTML4_LOOSE
=> '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">',
146 self
::HTML4_FRAMESET
=> '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">',
147 self
::HTML5
=> '<!DOCTYPE html>',
153 * Unset the static doctype registry
155 * Mainly useful for testing purposes. Sets {@link $registeredDoctypes} to
160 public static function unsetDoctypeRegistry()
162 static::$registeredDoctypes = null;
168 * @param string $doctype
171 public function setDoctype($doctype)
173 $this->registry
['doctype'] = $doctype;
182 public function getDoctype()
184 if (!isset($this->registry
['doctype'])) {
185 $this->setDoctype($this->defaultDoctype
);
188 return $this->registry
['doctype'];
192 * Get doctype => string mappings
196 public function getDoctypes()
198 return $this->registry
['doctypes'];
206 public function isXhtml()
208 return (stristr($this->getDoctype(), 'xhtml') ?
true : false);
212 * Is doctype HTML5? (HeadMeta uses this for validation)
216 public function isHtml5()
218 return (stristr($this->__invoke(), '<!DOCTYPE html>') ?
true : false);
226 public function isRdfa()
228 return ($this->isHtml5() ||
stristr($this->getDoctype(), 'rdfa') ?
true : false);