HeadMeta.php 10 KB

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