HeadMeta.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @version $Id$
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. /** Zend_View_Helper_Placeholder_Container_Standalone */
  23. require_once 'Zend/View/Helper/Placeholder/Container/Standalone.php';
  24. /**
  25. * Zend_Layout_View_Helper_HeadMeta
  26. *
  27. * @see http://www.w3.org/TR/xhtml1/dtds.html
  28. * @uses Zend_View_Helper_Placeholder_Container_Standalone
  29. * @package Zend_View
  30. * @subpackage Helper
  31. * @copyright Copyright (c) 2005-2010 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_HeadMeta extends Zend_View_Helper_Placeholder_Container_Standalone
  35. {
  36. /**
  37. * Types of attributes
  38. * @var array
  39. */
  40. protected $_typeKeys = array('name', 'http-equiv', 'charset');
  41. protected $_requiredKeys = array('content');
  42. protected $_modifierKeys = array('lang', 'scheme');
  43. /**
  44. * @var string registry key
  45. */
  46. protected $_regKey = 'Zend_View_Helper_HeadMeta';
  47. /**
  48. * Constructor
  49. *
  50. * Set separator to PHP_EOL
  51. *
  52. * @return void
  53. */
  54. public function __construct()
  55. {
  56. parent::__construct();
  57. $this->setSeparator(PHP_EOL);
  58. }
  59. /**
  60. * Retrieve object instance; optionally add meta tag
  61. *
  62. * @param string $content
  63. * @param string $keyValue
  64. * @param string $keyType
  65. * @param array $modifiers
  66. * @param string $placement
  67. * @return Zend_View_Helper_HeadMeta
  68. */
  69. public function headMeta($content = null, $keyValue = null, $keyType = 'name', $modifiers = array(), $placement = Zend_View_Helper_Placeholder_Container_Abstract::APPEND)
  70. {
  71. if ((null !== $content) && (null !== $keyValue)) {
  72. $item = $this->createData($keyType, $keyValue, $content, $modifiers);
  73. $action = strtolower($placement);
  74. switch ($action) {
  75. case 'append':
  76. case 'prepend':
  77. case 'set':
  78. $this->$action($item);
  79. break;
  80. default:
  81. $this->append($item);
  82. break;
  83. }
  84. }
  85. return $this;
  86. }
  87. protected function _normalizeType($type)
  88. {
  89. switch ($type) {
  90. case 'Name':
  91. return 'name';
  92. case 'HttpEquiv':
  93. return 'http-equiv';
  94. default:
  95. require_once 'Zend/View/Exception.php';
  96. $e = new Zend_View_Exception(sprintf('Invalid type "%s" passed to _normalizeType', $type));
  97. $e->setView($this->view);
  98. throw $e;
  99. }
  100. }
  101. /**
  102. * Overload method access
  103. *
  104. * Allows the following 'virtual' methods:
  105. * - appendName($keyValue, $content, $modifiers = array())
  106. * - offsetGetName($index, $keyValue, $content, $modifers = array())
  107. * - prependName($keyValue, $content, $modifiers = array())
  108. * - setName($keyValue, $content, $modifiers = array())
  109. * - appendHttpEquiv($keyValue, $content, $modifiers = array())
  110. * - offsetGetHttpEquiv($index, $keyValue, $content, $modifers = array())
  111. * - prependHttpEquiv($keyValue, $content, $modifiers = array())
  112. * - setHttpEquiv($keyValue, $content, $modifiers = array())
  113. *
  114. * @param string $method
  115. * @param array $args
  116. * @return Zend_View_Helper_HeadMeta
  117. */
  118. public function __call($method, $args)
  119. {
  120. if (preg_match('/^(?P<action>set|(pre|ap)pend|offsetSet)(?P<type>Name|HttpEquiv)$/', $method, $matches)) {
  121. $action = $matches['action'];
  122. $type = $this->_normalizeType($matches['type']);
  123. $argc = count($args);
  124. $index = null;
  125. if ('offsetSet' == $action) {
  126. if (0 < $argc) {
  127. $index = array_shift($args);
  128. --$argc;
  129. }
  130. }
  131. if (2 > $argc) {
  132. require_once 'Zend/View/Exception.php';
  133. $e = new Zend_View_Exception('Too few arguments provided; requires key value, and content');
  134. $e->setView($this->view);
  135. throw $e;
  136. }
  137. if (3 > $argc) {
  138. $args[] = array();
  139. }
  140. $item = $this->createData($type, $args[0], $args[1], $args[2]);
  141. if ('offsetSet' == $action) {
  142. return $this->offsetSet($index, $item);
  143. }
  144. $this->$action($item);
  145. return $this;
  146. }
  147. return parent::__call($method, $args);
  148. }
  149. /**
  150. * Create an HTML5-style meta charset tag. Something like <meta charset="utf-8">
  151. *
  152. * Not valid in a non-HTML5 doctype
  153. *
  154. * @param string $charset
  155. * @return Zend_View_Helper_HeadMeta Provides a fluent interface
  156. */
  157. public function setCharset($charset)
  158. {
  159. $item = new stdClass;
  160. $item->type = 'charset';
  161. $item->charset = $charset;
  162. $item->content = null;
  163. $item->modifiers = array();
  164. $this->set($item);
  165. return $this;
  166. }
  167. /**
  168. * Determine if item is valid
  169. *
  170. * @param mixed $item
  171. * @return boolean
  172. */
  173. protected function _isValid($item)
  174. {
  175. if ((!$item instanceof stdClass)
  176. || !isset($item->type)
  177. || !isset($item->modifiers))
  178. {
  179. return false;
  180. }
  181. if (!isset($item->content)
  182. && (! $this->view->doctype()->isHtml5()
  183. || (! $this->view->doctype()->isHtml5() && $item->type !== 'charset'))) {
  184. return false;
  185. }
  186. return true;
  187. }
  188. /**
  189. * Append
  190. *
  191. * @param string $value
  192. * @return void
  193. * @throws Zend_View_Exception
  194. */
  195. public function append($value)
  196. {
  197. if (!$this->_isValid($value)) {
  198. require_once 'Zend/View/Exception.php';
  199. $e = new Zend_View_Exception('Invalid value passed to append; please use appendMeta()');
  200. $e->setView($this->view);
  201. throw $e;
  202. }
  203. return $this->getContainer()->append($value);
  204. }
  205. /**
  206. * OffsetSet
  207. *
  208. * @param string|int $index
  209. * @param string $value
  210. * @return void
  211. * @throws Zend_View_Exception
  212. */
  213. public function offsetSet($index, $value)
  214. {
  215. if (!$this->_isValid($value)) {
  216. require_once 'Zend/View/Exception.php';
  217. $e = new Zend_View_Exception('Invalid value passed to offsetSet; please use offsetSetName() or offsetSetHttpEquiv()');
  218. $e->setView($this->view);
  219. throw $e;
  220. }
  221. return $this->getContainer()->offsetSet($index, $value);
  222. }
  223. /**
  224. * OffsetUnset
  225. *
  226. * @param string|int $index
  227. * @return void
  228. * @throws Zend_View_Exception
  229. */
  230. public function offsetUnset($index)
  231. {
  232. if (!in_array($index, $this->getContainer()->getKeys())) {
  233. require_once 'Zend/View/Exception.php';
  234. $e = new Zend_View_Exception('Invalid index passed to offsetUnset()');
  235. $e->setView($this->view);
  236. throw $e;
  237. }
  238. return $this->getContainer()->offsetUnset($index);
  239. }
  240. /**
  241. * Prepend
  242. *
  243. * @param string $value
  244. * @return void
  245. * @throws Zend_View_Exception
  246. */
  247. public function prepend($value)
  248. {
  249. if (!$this->_isValid($value)) {
  250. require_once 'Zend/View/Exception.php';
  251. $e = new Zend_View_Exception('Invalid value passed to prepend; please use prependMeta()');
  252. $e->setView($this->view);
  253. throw $e;
  254. }
  255. return $this->getContainer()->prepend($value);
  256. }
  257. /**
  258. * Set
  259. *
  260. * @param string $value
  261. * @return void
  262. * @throws Zend_View_Exception
  263. */
  264. public function set($value)
  265. {
  266. if (!$this->_isValid($value)) {
  267. require_once 'Zend/View/Exception.php';
  268. $e = new Zend_View_Exception('Invalid value passed to set; please use setMeta()');
  269. $e->setView($this->view);
  270. throw $e;
  271. }
  272. $container = $this->getContainer();
  273. foreach ($container->getArrayCopy() as $index => $item) {
  274. if ($item->type == $value->type && $item->{$item->type} == $value->{$value->type}) {
  275. $this->offsetUnset($index);
  276. }
  277. }
  278. return $this->append($value);
  279. }
  280. /**
  281. * Build meta HTML string
  282. *
  283. * @param string $type
  284. * @param string $typeValue
  285. * @param string $content
  286. * @param array $modifiers
  287. * @return string
  288. */
  289. public function itemToString(stdClass $item)
  290. {
  291. if (!in_array($item->type, $this->_typeKeys)) {
  292. require_once 'Zend/View/Exception.php';
  293. $e = new Zend_View_Exception(sprintf('Invalid type "%s" provided for meta', $item->type));
  294. $e->setView($this->view);
  295. throw $e;
  296. }
  297. $type = $item->type;
  298. $modifiersString = '';
  299. foreach ($item->modifiers as $key => $value) {
  300. if ($this->view->doctype()->isHtml5()
  301. && $key == 'scheme') {
  302. require_once 'Zend/View/Exception.php';
  303. throw new Zend_View_Exception('Invalid modifier '
  304. . '"scheme" provided; not supported by HTML5');
  305. }
  306. if (!in_array($key, $this->_modifierKeys)) {
  307. continue;
  308. }
  309. $modifiersString .= $key . '="' . $this->_escape($value) . '" ';
  310. }
  311. if ($this->view instanceof Zend_View_Abstract) {
  312. if ($this->view->doctype()->isHtml5()
  313. && $type == 'charset') {
  314. $tpl = ($this->view->doctype()->isXhtml())
  315. ? '<meta %s="%s"/>'
  316. : '<meta %s="%s">';
  317. } elseif ($this->view->doctype()->isXhtml()) {
  318. $tpl = '<meta %s="%s" content="%s" %s/>';
  319. } else {
  320. $tpl = '<meta %s="%s" content="%s" %s>';
  321. }
  322. } else {
  323. $tpl = '<meta %s="%s" content="%s" %s/>';
  324. }
  325. $meta = sprintf(
  326. $tpl,
  327. $type,
  328. $this->_escape($item->$type),
  329. $this->_escape($item->content),
  330. $modifiersString
  331. );
  332. return $meta;
  333. }
  334. /**
  335. * Render placeholder as string
  336. *
  337. * @param string|int $indent
  338. * @return string
  339. */
  340. public function toString($indent = null)
  341. {
  342. $indent = (null !== $indent)
  343. ? $this->getWhitespace($indent)
  344. : $this->getIndent();
  345. $items = array();
  346. $this->getContainer()->ksort();
  347. try {
  348. foreach ($this as $item) {
  349. $items[] = $this->itemToString($item);
  350. }
  351. } catch (Zend_View_Exception $e) {
  352. trigger_error($e->getMessage(), E_USER_WARNING);
  353. return '';
  354. }
  355. return $indent . implode($this->_escape($this->getSeparator()) . $indent, $items);
  356. }
  357. /**
  358. * Create data item for inserting into stack
  359. *
  360. * @param string $type
  361. * @param string $typeValue
  362. * @param string $content
  363. * @param array $modifiers
  364. * @return stdClass
  365. */
  366. public function createData($type, $typeValue, $content, array $modifiers)
  367. {
  368. $data = new stdClass;
  369. $data->type = $type;
  370. $data->$type = $typeValue;
  371. $data->content = $content;
  372. $data->modifiers = $modifiers;
  373. return $data;
  374. }
  375. }