HeadMeta.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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-2009 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-2009 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. if ($action == 'set') {
  145. //var_dump($this->getContainer());
  146. }
  147. $this->$action($item);
  148. return $this;
  149. }
  150. return parent::__call($method, $args);
  151. }
  152. /**
  153. * Create an HTML5-style meta charset tag. Something like <meta charset="utf-8">
  154. *
  155. * Not valid in a non-HTML5 doctype
  156. *
  157. * @param string $charset
  158. * @return Zend_View_Helper_HeadMeta Provides a fluent interface
  159. */
  160. public function setCharset($charset)
  161. {
  162. $item = new stdClass;
  163. $item->type = 'charset';
  164. $item->charset = $charset;
  165. $item->content = null;
  166. $item->modifiers = array();
  167. $this->set($item);
  168. return $this;
  169. }
  170. /**
  171. * Determine if item is valid
  172. *
  173. * @param mixed $item
  174. * @return boolean
  175. */
  176. protected function _isValid($item)
  177. {
  178. if ((!$item instanceof stdClass)
  179. || !isset($item->type)
  180. || !isset($item->modifiers))
  181. {
  182. return false;
  183. }
  184. if (!isset($item->content)
  185. && (! $this->view->doctype()->isHtml5()
  186. || (! $this->view->doctype()->isHtml5() && $item->type !== 'charset'))) {
  187. return false;
  188. }
  189. return true;
  190. }
  191. /**
  192. * Append
  193. *
  194. * @param string $value
  195. * @return void
  196. * @throws Zend_View_Exception
  197. */
  198. public function append($value)
  199. {
  200. if (!$this->_isValid($value)) {
  201. require_once 'Zend/View/Exception.php';
  202. $e = new Zend_View_Exception('Invalid value passed to append; please use appendMeta()');
  203. $e->setView($this->view);
  204. throw $e;
  205. }
  206. return $this->getContainer()->append($value);
  207. }
  208. /**
  209. * OffsetSet
  210. *
  211. * @param string|int $index
  212. * @param string $value
  213. * @return void
  214. * @throws Zend_View_Exception
  215. */
  216. public function offsetSet($index, $value)
  217. {
  218. if (!$this->_isValid($value)) {
  219. require_once 'Zend/View/Exception.php';
  220. $e = new Zend_View_Exception('Invalid value passed to offsetSet; please use offsetSetMeta()');
  221. $e->setView($this->view);
  222. throw $e;
  223. }
  224. return $this->getContainer()->offsetSet($index, $value);
  225. }
  226. /**
  227. * OffsetUnset
  228. *
  229. * @param string|int $index
  230. * @return void
  231. * @throws Zend_View_Exception
  232. */
  233. public function offsetUnset($index)
  234. {
  235. if (!in_array($index, $this->getContainer()->getKeys())) {
  236. require_once 'Zend/View/Exception.php';
  237. $e = new Zend_View_Exception('Invalid index passed to offsetUnset()');
  238. $e->setView($this->view);
  239. throw $e;
  240. }
  241. return $this->getContainer()->offsetUnset($index);
  242. }
  243. /**
  244. * Prepend
  245. *
  246. * @param string $value
  247. * @return void
  248. * @throws Zend_View_Exception
  249. */
  250. public function prepend($value)
  251. {
  252. if (!$this->_isValid($value)) {
  253. require_once 'Zend/View/Exception.php';
  254. $e = new Zend_View_Exception('Invalid value passed to prepend; please use prependMeta()');
  255. $e->setView($this->view);
  256. throw $e;
  257. }
  258. return $this->getContainer()->prepend($value);
  259. }
  260. /**
  261. * Set
  262. *
  263. * @param string $value
  264. * @return void
  265. * @throws Zend_View_Exception
  266. */
  267. public function set($value)
  268. {
  269. if (!$this->_isValid($value)) {
  270. require_once 'Zend/View/Exception.php';
  271. $e = new Zend_View_Exception('Invalid value passed to set; please use setMeta()');
  272. $e->setView($this->view);
  273. throw $e;
  274. }
  275. $container = $this->getContainer();
  276. foreach ($container->getArrayCopy() as $index => $item) {
  277. if ($item->type == $value->type && $item->{$item->type} == $value->{$value->type}) {
  278. $this->offsetUnset($index);
  279. }
  280. }
  281. return $this->append($value);
  282. }
  283. /**
  284. * Build meta HTML string
  285. *
  286. * @param string $type
  287. * @param string $typeValue
  288. * @param string $content
  289. * @param array $modifiers
  290. * @return string
  291. */
  292. public function itemToString(stdClass $item)
  293. {
  294. if (!in_array($item->type, $this->_typeKeys)) {
  295. require_once 'Zend/View/Exception.php';
  296. $e = new Zend_View_Exception(sprintf('Invalid type "%s" provided for meta', $item->type));
  297. $e->setView($this->view);
  298. throw $e;
  299. }
  300. $type = $item->type;
  301. $modifiersString = '';
  302. foreach ($item->modifiers as $key => $value) {
  303. if ($this->view->doctype()->isHtml5()
  304. && $key == 'scheme') {
  305. require_once 'Zend/View/Exception.php';
  306. throw new Zend_View_Exception('Invalid modifier '
  307. . '"scheme" provided; not supported by HTML5');
  308. }
  309. if (!in_array($key, $this->_modifierKeys)) {
  310. continue;
  311. }
  312. $modifiersString .= $key . '="' . $this->_escape($value) . '" ';
  313. }
  314. if ($this->view instanceof Zend_View_Abstract) {
  315. if ($this->view->doctype()->isHtml5()
  316. && $type == 'charset') {
  317. $tpl = ($this->view->doctype()->isXhtml())
  318. ? '<meta %s="%s"/>'
  319. : '<meta %s="%s">';
  320. } elseif ($this->view->doctype()->isXhtml()) {
  321. $tpl = '<meta %s="%s" content="%s" %s/>';
  322. } else {
  323. $tpl = '<meta %s="%s" content="%s" %s>';
  324. }
  325. } else {
  326. $tpl = '<meta %s="%s" content="%s" %s/>';
  327. }
  328. $meta = sprintf(
  329. $tpl,
  330. $type,
  331. $this->_escape($item->$type),
  332. $this->_escape($item->content),
  333. $modifiersString
  334. );
  335. return $meta;
  336. }
  337. /**
  338. * Render placeholder as string
  339. *
  340. * @param string|int $indent
  341. * @return string
  342. */
  343. public function toString($indent = null)
  344. {
  345. $indent = (null !== $indent)
  346. ? $this->getWhitespace($indent)
  347. : $this->getIndent();
  348. $items = array();
  349. $this->getContainer()->ksort();
  350. try {
  351. foreach ($this as $item) {
  352. $items[] = $this->itemToString($item);
  353. }
  354. } catch (Zend_View_Exception $e) {
  355. trigger_error($e->getMessage(), E_USER_WARNING);
  356. return '';
  357. }
  358. return $indent . implode($this->_escape($this->getSeparator()) . $indent, $items);
  359. }
  360. /**
  361. * Create data item for inserting into stack
  362. *
  363. * @param string $type
  364. * @param string $typeValue
  365. * @param string $content
  366. * @param array $modifiers
  367. * @return stdClass
  368. */
  369. public function createData($type, $typeValue, $content, array $modifiers)
  370. {
  371. $data = new stdClass;
  372. $data->type = $type;
  373. $data->$type = $typeValue;
  374. $data->content = $content;
  375. $data->modifiers = $modifiers;
  376. return $data;
  377. }
  378. }