HeadMeta.php 11 KB

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