2
0

HeadMeta.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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$
  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-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_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', 'property');
  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. case 'Property':
  95. return 'property';
  96. default:
  97. require_once 'Zend/View/Exception.php';
  98. $e = new Zend_View_Exception(sprintf('Invalid type "%s" passed to _normalizeType', $type));
  99. $e->setView($this->view);
  100. throw $e;
  101. }
  102. }
  103. /**
  104. * Overload method access
  105. *
  106. * Allows the following 'virtual' methods:
  107. * - appendName($keyValue, $content, $modifiers = array())
  108. * - offsetGetName($index, $keyValue, $content, $modifers = array())
  109. * - prependName($keyValue, $content, $modifiers = array())
  110. * - setName($keyValue, $content, $modifiers = array())
  111. * - appendHttpEquiv($keyValue, $content, $modifiers = array())
  112. * - offsetGetHttpEquiv($index, $keyValue, $content, $modifers = array())
  113. * - prependHttpEquiv($keyValue, $content, $modifiers = array())
  114. * - setHttpEquiv($keyValue, $content, $modifiers = array())
  115. * - appendProperty($keyValue, $content, $modifiers = array())
  116. * - offsetGetProperty($index, $keyValue, $content, $modifiers = array())
  117. * - prependProperty($keyValue, $content, $modifiers = array())
  118. * - setProperty($keyValue, $content, $modifiers = array())
  119. *
  120. * @param string $method
  121. * @param array $args
  122. * @return Zend_View_Helper_HeadMeta
  123. */
  124. public function __call($method, $args)
  125. {
  126. if (preg_match('/^(?P<action>set|(pre|ap)pend|offsetSet)(?P<type>Name|HttpEquiv|Property)$/', $method, $matches)) {
  127. $action = $matches['action'];
  128. $type = $this->_normalizeType($matches['type']);
  129. $argc = count($args);
  130. $index = null;
  131. if ('offsetSet' == $action) {
  132. if (0 < $argc) {
  133. $index = array_shift($args);
  134. --$argc;
  135. }
  136. }
  137. if (2 > $argc) {
  138. require_once 'Zend/View/Exception.php';
  139. $e = new Zend_View_Exception('Too few arguments provided; requires key value, and content');
  140. $e->setView($this->view);
  141. throw $e;
  142. }
  143. if (3 > $argc) {
  144. $args[] = array();
  145. }
  146. $item = $this->createData($type, $args[0], $args[1], $args[2]);
  147. if ('offsetSet' == $action) {
  148. return $this->offsetSet($index, $item);
  149. }
  150. $this->$action($item);
  151. return $this;
  152. }
  153. return parent::__call($method, $args);
  154. }
  155. /**
  156. * Create an HTML5-style meta charset tag. Something like <meta charset="utf-8">
  157. *
  158. * Not valid in a non-HTML5 doctype
  159. *
  160. * @param string $charset
  161. * @return Zend_View_Helper_HeadMeta Provides a fluent interface
  162. */
  163. public function setCharset($charset)
  164. {
  165. $item = new stdClass;
  166. $item->type = 'charset';
  167. $item->charset = $charset;
  168. $item->content = null;
  169. $item->modifiers = array();
  170. $this->set($item);
  171. return $this;
  172. }
  173. /**
  174. * Determine if item is valid
  175. *
  176. * @param mixed $item
  177. * @return boolean
  178. */
  179. protected function _isValid($item)
  180. {
  181. if ((!$item instanceof stdClass)
  182. || !isset($item->type)
  183. || !isset($item->modifiers))
  184. {
  185. return false;
  186. }
  187. $isHtml5 = is_null($this->view) ? false : $this->view->doctype()->isHtml5();
  188. if (!isset($item->content)
  189. && (! $isHtml5 || (! $isHtml5 && $item->type !== 'charset'))) {
  190. return false;
  191. }
  192. // <meta property= ... /> is only supported with doctype RDFa
  193. if ( !is_null($this->view) && !$this->view->doctype()->isRdfa()
  194. && $item->type === 'property') {
  195. return false;
  196. }
  197. return true;
  198. }
  199. /**
  200. * Append
  201. *
  202. * @param string $value
  203. * @return void
  204. * @throws Zend_View_Exception
  205. */
  206. public function append($value)
  207. {
  208. if (!$this->_isValid($value)) {
  209. require_once 'Zend/View/Exception.php';
  210. $e = new Zend_View_Exception('Invalid value passed to append; please use appendMeta()');
  211. $e->setView($this->view);
  212. throw $e;
  213. }
  214. return $this->getContainer()->append($value);
  215. }
  216. /**
  217. * OffsetSet
  218. *
  219. * @param string|int $index
  220. * @param string $value
  221. * @return void
  222. * @throws Zend_View_Exception
  223. */
  224. public function offsetSet($index, $value)
  225. {
  226. if (!$this->_isValid($value)) {
  227. require_once 'Zend/View/Exception.php';
  228. $e = new Zend_View_Exception('Invalid value passed to offsetSet; please use offsetSetName() or offsetSetHttpEquiv()');
  229. $e->setView($this->view);
  230. throw $e;
  231. }
  232. return $this->getContainer()->offsetSet($index, $value);
  233. }
  234. /**
  235. * OffsetUnset
  236. *
  237. * @param string|int $index
  238. * @return void
  239. * @throws Zend_View_Exception
  240. */
  241. public function offsetUnset($index)
  242. {
  243. if (!in_array($index, $this->getContainer()->getKeys())) {
  244. require_once 'Zend/View/Exception.php';
  245. $e = new Zend_View_Exception('Invalid index passed to offsetUnset()');
  246. $e->setView($this->view);
  247. throw $e;
  248. }
  249. return $this->getContainer()->offsetUnset($index);
  250. }
  251. /**
  252. * Prepend
  253. *
  254. * @param string $value
  255. * @return void
  256. * @throws Zend_View_Exception
  257. */
  258. public function prepend($value)
  259. {
  260. if (!$this->_isValid($value)) {
  261. require_once 'Zend/View/Exception.php';
  262. $e = new Zend_View_Exception('Invalid value passed to prepend; please use prependMeta()');
  263. $e->setView($this->view);
  264. throw $e;
  265. }
  266. return $this->getContainer()->prepend($value);
  267. }
  268. /**
  269. * Set
  270. *
  271. * @param string $value
  272. * @return void
  273. * @throws Zend_View_Exception
  274. */
  275. public function set($value)
  276. {
  277. if (!$this->_isValid($value)) {
  278. require_once 'Zend/View/Exception.php';
  279. $e = new Zend_View_Exception('Invalid value passed to set; please use setMeta()');
  280. $e->setView($this->view);
  281. throw $e;
  282. }
  283. $container = $this->getContainer();
  284. foreach ($container->getArrayCopy() as $index => $item) {
  285. if ($item->type == $value->type && $item->{$item->type} == $value->{$value->type}) {
  286. $this->offsetUnset($index);
  287. }
  288. }
  289. return $this->append($value);
  290. }
  291. /**
  292. * Build meta HTML string
  293. *
  294. * @param string $type
  295. * @param string $typeValue
  296. * @param string $content
  297. * @param array $modifiers
  298. * @return string
  299. */
  300. public function itemToString(stdClass $item)
  301. {
  302. if (!in_array($item->type, $this->_typeKeys)) {
  303. require_once 'Zend/View/Exception.php';
  304. $e = new Zend_View_Exception(sprintf('Invalid type "%s" provided for meta', $item->type));
  305. $e->setView($this->view);
  306. throw $e;
  307. }
  308. $type = $item->type;
  309. $modifiersString = '';
  310. foreach ($item->modifiers as $key => $value) {
  311. if (!is_null($this->view) && $this->view->doctype()->isHtml5()
  312. && $key == 'scheme') {
  313. require_once 'Zend/View/Exception.php';
  314. throw new Zend_View_Exception('Invalid modifier '
  315. . '"scheme" provided; not supported by HTML5');
  316. }
  317. if (!in_array($key, $this->_modifierKeys)) {
  318. continue;
  319. }
  320. $modifiersString .= $key . '="' . $this->_escape($value) . '" ';
  321. }
  322. if ($this->view instanceof Zend_View_Abstract) {
  323. if ($this->view->doctype()->isHtml5()
  324. && $type == 'charset') {
  325. $tpl = ($this->view->doctype()->isXhtml())
  326. ? '<meta %s="%s"/>'
  327. : '<meta %s="%s">';
  328. } elseif ($this->view->doctype()->isXhtml()) {
  329. $tpl = '<meta %s="%s" content="%s" %s/>';
  330. } else {
  331. $tpl = '<meta %s="%s" content="%s" %s>';
  332. }
  333. } else {
  334. $tpl = '<meta %s="%s" content="%s" %s/>';
  335. }
  336. $meta = sprintf(
  337. $tpl,
  338. $type,
  339. $this->_escape($item->$type),
  340. $this->_escape($item->content),
  341. $modifiersString
  342. );
  343. if (isset($item->modifiers['conditional'])
  344. && !empty($item->modifiers['conditional'])
  345. && is_string($item->modifiers['conditional']))
  346. {
  347. $meta = '<!--[if ' . $this->_escape($item->modifiers['conditional']) . ']>' . $meta . '<![endif]-->';
  348. }
  349. return $meta;
  350. }
  351. /**
  352. * Render placeholder as string
  353. *
  354. * @param string|int $indent
  355. * @return string
  356. */
  357. public function toString($indent = null)
  358. {
  359. $indent = (null !== $indent)
  360. ? $this->getWhitespace($indent)
  361. : $this->getIndent();
  362. $items = array();
  363. $this->getContainer()->ksort();
  364. try {
  365. foreach ($this as $item) {
  366. $items[] = $this->itemToString($item);
  367. }
  368. } catch (Zend_View_Exception $e) {
  369. trigger_error($e->getMessage(), E_USER_WARNING);
  370. return '';
  371. }
  372. return $indent . implode($this->_escape($this->getSeparator()) . $indent, $items);
  373. }
  374. /**
  375. * Create data item for inserting into stack
  376. *
  377. * @param string $type
  378. * @param string $typeValue
  379. * @param string $content
  380. * @param array $modifiers
  381. * @return stdClass
  382. */
  383. public function createData($type, $typeValue, $content, array $modifiers)
  384. {
  385. $data = new stdClass;
  386. $data->type = $type;
  387. $data->$type = $typeValue;
  388. $data->content = $content;
  389. $data->modifiers = $modifiers;
  390. return $data;
  391. }
  392. }