PageRenderTime 24ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Zend/View/Helper/HeadMeta.php

https://github.com/gryzz/crystal_magento
PHP | 363 lines | 179 code | 36 blank | 148 comment | 31 complexity | a7cbf9125f5beaed66dc99d0b1068196 MD5 | raw file
  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: HeadMeta.php 16971 2009-07-22 18:05:45Z mikaelkael $
  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. throw new Zend_View_Exception(sprintf('Invalid type "%s" passed to _normalizeType', $type));
  97. }
  98. }
  99. /**
  100. * Overload method access
  101. *
  102. * Allows the following 'virtual' methods:
  103. * - appendName($keyValue, $content, $modifiers = array())
  104. * - offsetGetName($index, $keyValue, $content, $modifers = array())
  105. * - prependName($keyValue, $content, $modifiers = array())
  106. * - setName($keyValue, $content, $modifiers = array())
  107. * - appendHttpEquiv($keyValue, $content, $modifiers = array())
  108. * - offsetGetHttpEquiv($index, $keyValue, $content, $modifers = array())
  109. * - prependHttpEquiv($keyValue, $content, $modifiers = array())
  110. * - setHttpEquiv($keyValue, $content, $modifiers = array())
  111. *
  112. * @param string $method
  113. * @param array $args
  114. * @return Zend_View_Helper_HeadMeta
  115. */
  116. public function __call($method, $args)
  117. {
  118. if (preg_match('/^(?P<action>set|(pre|ap)pend|offsetSet)(?P<type>Name|HttpEquiv)$/', $method, $matches)) {
  119. $action = $matches['action'];
  120. $type = $this->_normalizeType($matches['type']);
  121. $argc = count($args);
  122. $index = null;
  123. if ('offsetSet' == $action) {
  124. if (0 < $argc) {
  125. $index = array_shift($args);
  126. --$argc;
  127. }
  128. }
  129. if (2 > $argc) {
  130. #require_once 'Zend/View/Exception.php';
  131. throw new Zend_View_Exception('Too few arguments provided; requires key value, and content');
  132. }
  133. if (3 > $argc) {
  134. $args[] = array();
  135. }
  136. $item = $this->createData($type, $args[0], $args[1], $args[2]);
  137. if ('offsetSet' == $action) {
  138. return $this->offsetSet($index, $item);
  139. }
  140. if ($action == 'set') {
  141. //var_dump($this->getContainer());
  142. }
  143. $this->$action($item);
  144. return $this;
  145. }
  146. return parent::__call($method, $args);
  147. }
  148. /**
  149. * Determine if item is valid
  150. *
  151. * @param mixed $item
  152. * @return boolean
  153. */
  154. protected function _isValid($item)
  155. {
  156. if ((!$item instanceof stdClass)
  157. || !isset($item->type)
  158. || !isset($item->content)
  159. || !isset($item->modifiers))
  160. {
  161. return false;
  162. }
  163. return true;
  164. }
  165. /**
  166. * Append
  167. *
  168. * @param string $value
  169. * @return void
  170. * @throws Zend_View_Exception
  171. */
  172. public function append($value)
  173. {
  174. if (!$this->_isValid($value)) {
  175. #require_once 'Zend/View/Exception.php';
  176. throw new Zend_View_Exception('Invalid value passed to append; please use appendMeta()');
  177. }
  178. return $this->getContainer()->append($value);
  179. }
  180. /**
  181. * OffsetSet
  182. *
  183. * @param string|int $index
  184. * @param string $value
  185. * @return void
  186. * @throws Zend_View_Exception
  187. */
  188. public function offsetSet($index, $value)
  189. {
  190. if (!$this->_isValid($value)) {
  191. #require_once 'Zend/View/Exception.php';
  192. throw new Zend_View_Exception('Invalid value passed to offsetSet; please use offsetSetMeta()');
  193. }
  194. return $this->getContainer()->offsetSet($index, $value);
  195. }
  196. /**
  197. * OffsetUnset
  198. *
  199. * @param string|int $index
  200. * @return void
  201. * @throws Zend_View_Exception
  202. */
  203. public function offsetUnset($index)
  204. {
  205. if (!in_array($index, $this->getContainer()->getKeys())) {
  206. #require_once 'Zend/View/Exception.php';
  207. throw new Zend_View_Exception('Invalid index passed to offsetUnset.');
  208. }
  209. return $this->getContainer()->offsetUnset($index);
  210. }
  211. /**
  212. * Prepend
  213. *
  214. * @param string $value
  215. * @return void
  216. * @throws Zend_View_Exception
  217. */
  218. public function prepend($value)
  219. {
  220. if (!$this->_isValid($value)) {
  221. #require_once 'Zend/View/Exception.php';
  222. throw new Zend_View_Exception('Invalid value passed to prepend; please use prependMeta()');
  223. }
  224. return $this->getContainer()->prepend($value);
  225. }
  226. /**
  227. * Set
  228. *
  229. * @param string $value
  230. * @return void
  231. * @throws Zend_View_Exception
  232. */
  233. public function set($value)
  234. {
  235. if (!$this->_isValid($value)) {
  236. #require_once 'Zend/View/Exception.php';
  237. throw new Zend_View_Exception('Invalid value passed to set; please use setMeta()');
  238. }
  239. $container = $this->getContainer();
  240. foreach ($container->getArrayCopy() as $index => $item) {
  241. if ($item->type == $value->type && $item->{$item->type} == $value->{$value->type}) {
  242. $this->offsetUnset($index);
  243. }
  244. }
  245. return $this->append($value);
  246. }
  247. /**
  248. * Build meta HTML string
  249. *
  250. * @param string $type
  251. * @param string $typeValue
  252. * @param string $content
  253. * @param array $modifiers
  254. * @return string
  255. */
  256. public function itemToString(stdClass $item)
  257. {
  258. if (!in_array($item->type, $this->_typeKeys)) {
  259. #require_once 'Zend/View/Exception.php';
  260. throw new Zend_View_Exception(sprintf('Invalid type "%s" provided for meta', $item->type));
  261. }
  262. $type = $item->type;
  263. $modifiersString = '';
  264. foreach ($item->modifiers as $key => $value) {
  265. if (!in_array($key, $this->_modifierKeys)) {
  266. continue;
  267. }
  268. $modifiersString .= $key . '="' . $this->_escape($value) . '" ';
  269. }
  270. if ($this->view instanceof Zend_View_Abstract) {
  271. $tpl = ($this->view->doctype()->isXhtml())
  272. ? '<meta %s="%s" content="%s" %s/>'
  273. : '<meta %s="%s" content="%s" %s>';
  274. } else {
  275. $tpl = '<meta %s="%s" content="%s" %s/>';
  276. }
  277. $meta = sprintf(
  278. $tpl,
  279. $type,
  280. $this->_escape($item->$type),
  281. $this->_escape($item->content),
  282. $modifiersString
  283. );
  284. return $meta;
  285. }
  286. /**
  287. * Render placeholder as string
  288. *
  289. * @param string|int $indent
  290. * @return string
  291. */
  292. public function toString($indent = null)
  293. {
  294. $indent = (null !== $indent)
  295. ? $this->getWhitespace($indent)
  296. : $this->getIndent();
  297. $items = array();
  298. $this->getContainer()->ksort();
  299. try {
  300. foreach ($this as $item) {
  301. $items[] = $this->itemToString($item);
  302. }
  303. } catch (Zend_View_Exception $e) {
  304. trigger_error($e->getMessage(), E_USER_WARNING);
  305. return '';
  306. }
  307. return $indent . implode($this->_escape($this->getSeparator()) . $indent, $items);
  308. }
  309. /**
  310. * Create data item for inserting into stack
  311. *
  312. * @param string $type
  313. * @param string $typeValue
  314. * @param string $content
  315. * @param array $modifiers
  316. * @return stdClass
  317. */
  318. public function createData($type, $typeValue, $content, array $modifiers)
  319. {
  320. $data = new stdClass;
  321. $data->type = $type;
  322. $data->$type = $typeValue;
  323. $data->content = $content;
  324. $data->modifiers = $modifiers;
  325. return $data;
  326. }
  327. }