PageRenderTime 62ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/app/vendors/Zend/View/Helper/HeadMeta.php

https://github.com/rogerwu99/randomizr
PHP | 357 lines | 183 code | 36 blank | 138 comment | 31 complexity | 4d816e877615054a5b1a3bcb88b92f22 MD5 | raw file
Possible License(s): MIT, LGPL-3.0
  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-2008 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-2008 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. foreach ($this as $item) {
  299. $items[] = $this->itemToString($item);
  300. }
  301. return $indent . implode($this->_escape($this->getSeparator()) . $indent, $items);
  302. }
  303. /**
  304. * Create data item for inserting into stack
  305. *
  306. * @param string $type
  307. * @param string $typeValue
  308. * @param string $content
  309. * @param array $modifiers
  310. * @return stdClass
  311. */
  312. public function createData($type, $typeValue, $content, array $modifiers)
  313. {
  314. $data = new stdClass;
  315. $data->type = $type;
  316. $data->$type = $typeValue;
  317. $data->content = $content;
  318. $data->modifiers = $modifiers;
  319. return $data;
  320. }
  321. }