PageRenderTime 25ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/zendframework/zendframework/library/Zend/View/Helper/HeadMeta.php

https://bitbucket.org/juan_sanchez/aiyellow
PHP | 420 lines | 240 code | 43 blank | 137 comment | 46 complexity | 8a996e5dc74f79c28f8ae2e6d673cdc0 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\View\Helper;
  10. use stdClass;
  11. use Zend\View;
  12. use Zend\View\Exception;
  13. /**
  14. * Zend_Layout_View_Helper_HeadMeta
  15. *
  16. * @see http://www.w3.org/TR/xhtml1/dtds.html
  17. */
  18. class HeadMeta extends Placeholder\Container\AbstractStandalone
  19. {
  20. /**
  21. * Types of attributes
  22. * @var array
  23. */
  24. protected $typeKeys = array('name', 'http-equiv', 'charset', 'property');
  25. protected $requiredKeys = array('content');
  26. protected $modifierKeys = array('lang', 'scheme');
  27. /**
  28. * @var string registry key
  29. */
  30. protected $regKey = 'Zend_View_Helper_HeadMeta';
  31. /**
  32. * Constructor
  33. *
  34. * Set separator to PHP_EOL
  35. *
  36. */
  37. public function __construct()
  38. {
  39. parent::__construct();
  40. $this->setSeparator(PHP_EOL);
  41. }
  42. /**
  43. * Retrieve object instance; optionally add meta tag
  44. *
  45. * @param string $content
  46. * @param string $keyValue
  47. * @param string $keyType
  48. * @param array $modifiers
  49. * @param string $placement
  50. * @return \Zend\View\Helper\HeadMeta
  51. */
  52. public function __invoke($content = null, $keyValue = null, $keyType = 'name', $modifiers = array(), $placement = Placeholder\Container\AbstractContainer::APPEND)
  53. {
  54. if ((null !== $content) && (null !== $keyValue)) {
  55. $item = $this->createData($keyType, $keyValue, $content, $modifiers);
  56. $action = strtolower($placement);
  57. switch ($action) {
  58. case 'append':
  59. case 'prepend':
  60. case 'set':
  61. $this->$action($item);
  62. break;
  63. default:
  64. $this->append($item);
  65. break;
  66. }
  67. }
  68. return $this;
  69. }
  70. /**
  71. * Normalize type attribute of meta
  72. *
  73. * @param string $type type in CamelCase
  74. * @return string
  75. * @throws Exception\DomainException
  76. */
  77. protected function normalizeType($type)
  78. {
  79. switch ($type) {
  80. case 'Name':
  81. return 'name';
  82. case 'HttpEquiv':
  83. return 'http-equiv';
  84. case 'Property':
  85. return 'property';
  86. default:
  87. throw new Exception\DomainException(sprintf(
  88. 'Invalid type "%s" passed to normalizeType',
  89. $type
  90. ));
  91. }
  92. }
  93. /**
  94. * Overload method access
  95. *
  96. * Allows the following 'virtual' methods:
  97. * - appendName($keyValue, $content, $modifiers = array())
  98. * - offsetGetName($index, $keyValue, $content, $modifiers = array())
  99. * - prependName($keyValue, $content, $modifiers = array())
  100. * - setName($keyValue, $content, $modifiers = array())
  101. * - appendHttpEquiv($keyValue, $content, $modifiers = array())
  102. * - offsetGetHttpEquiv($index, $keyValue, $content, $modifiers = array())
  103. * - prependHttpEquiv($keyValue, $content, $modifiers = array())
  104. * - setHttpEquiv($keyValue, $content, $modifiers = array())
  105. * - appendProperty($keyValue, $content, $modifiers = array())
  106. * - offsetGetProperty($index, $keyValue, $content, $modifiers = array())
  107. * - prependProperty($keyValue, $content, $modifiers = array())
  108. * - setProperty($keyValue, $content, $modifiers = array())
  109. *
  110. * @param string $method
  111. * @param array $args
  112. * @return \Zend\View\Helper\HeadMeta
  113. * @throws Exception\BadMethodCallException
  114. */
  115. public function __call($method, $args)
  116. {
  117. if (preg_match('/^(?P<action>set|(pre|ap)pend|offsetSet)(?P<type>Name|HttpEquiv|Property)$/', $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. throw new Exception\BadMethodCallException(
  130. 'Too few arguments provided; requires key value, and content'
  131. );
  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. $this->$action($item);
  141. return $this;
  142. }
  143. return parent::__call($method, $args);
  144. }
  145. /**
  146. * Create an HTML5-style meta charset tag. Something like <meta charset="utf-8">
  147. *
  148. * Not valid in a non-HTML5 doctype
  149. *
  150. * @param string $charset
  151. * @return \Zend\View\Helper\HeadMeta Provides a fluent interface
  152. */
  153. public function setCharset($charset)
  154. {
  155. $item = new stdClass;
  156. $item->type = 'charset';
  157. $item->charset = $charset;
  158. $item->content = null;
  159. $item->modifiers = array();
  160. $this->set($item);
  161. return $this;
  162. }
  163. /**
  164. * Determine if item is valid
  165. *
  166. * @param mixed $item
  167. * @return bool
  168. */
  169. protected function isValid($item)
  170. {
  171. if ((!$item instanceof stdClass)
  172. || !isset($item->type)
  173. || !isset($item->modifiers))
  174. {
  175. return false;
  176. }
  177. if (!isset($item->content)
  178. && (! $this->view->plugin('doctype')->isHtml5()
  179. || (! $this->view->plugin('doctype')->isHtml5() && $item->type !== 'charset'))) {
  180. return false;
  181. }
  182. // <meta property= ... /> is only supported with doctype RDFa
  183. if (!$this->view->plugin('doctype')->isRdfa()
  184. && $item->type === 'property') {
  185. return false;
  186. }
  187. return true;
  188. }
  189. /**
  190. * Append
  191. *
  192. * @param string $value
  193. * @return void
  194. * @throws Exception\InvalidArgumentException
  195. */
  196. public function append($value)
  197. {
  198. if (!$this->isValid($value)) {
  199. throw new Exception\InvalidArgumentException(
  200. 'Invalid value passed to append; please use appendMeta()'
  201. );
  202. }
  203. return $this->getContainer()->append($value);
  204. }
  205. /**
  206. * OffsetSet
  207. *
  208. * @param string|int $index
  209. * @param string $value
  210. * @return void
  211. * @throws Exception\InvalidArgumentException
  212. */
  213. public function offsetSet($index, $value)
  214. {
  215. if (!$this->isValid($value)) {
  216. throw new Exception\InvalidArgumentException(
  217. 'Invalid value passed to offsetSet; please use offsetSetName() or offsetSetHttpEquiv()'
  218. );
  219. }
  220. return $this->getContainer()->offsetSet($index, $value);
  221. }
  222. /**
  223. * OffsetUnset
  224. *
  225. * @param string|int $index
  226. * @return void
  227. * @throws Exception\InvalidArgumentException
  228. */
  229. public function offsetUnset($index)
  230. {
  231. if (!in_array($index, $this->getContainer()->getKeys())) {
  232. throw new Exception\InvalidArgumentException('Invalid index passed to offsetUnset()');
  233. }
  234. return $this->getContainer()->offsetUnset($index);
  235. }
  236. /**
  237. * Prepend
  238. *
  239. * @param string $value
  240. * @return void
  241. * @throws Exception\InvalidArgumentException
  242. */
  243. public function prepend($value)
  244. {
  245. if (!$this->isValid($value)) {
  246. throw new Exception\InvalidArgumentException(
  247. 'Invalid value passed to prepend; please use prependMeta()'
  248. );
  249. }
  250. return $this->getContainer()->prepend($value);
  251. }
  252. /**
  253. * Set
  254. *
  255. * @param string $value
  256. * @return void
  257. * @throws Exception\InvalidArgumentException
  258. */
  259. public function set($value)
  260. {
  261. if (!$this->isValid($value)) {
  262. throw new Exception\InvalidArgumentException('Invalid value passed to set; please use setMeta()');
  263. }
  264. $container = $this->getContainer();
  265. foreach ($container->getArrayCopy() as $index => $item) {
  266. if ($item->type == $value->type && $item->{$item->type} == $value->{$value->type}) {
  267. $this->offsetUnset($index);
  268. }
  269. }
  270. return $this->append($value);
  271. }
  272. /**
  273. * Build meta HTML string
  274. *
  275. * @param stdClass $item
  276. * @throws Exception\InvalidArgumentException
  277. * @return string
  278. */
  279. public function itemToString(stdClass $item)
  280. {
  281. if (!in_array($item->type, $this->typeKeys)) {
  282. throw new Exception\InvalidArgumentException(sprintf(
  283. 'Invalid type "%s" provided for meta',
  284. $item->type
  285. ));
  286. }
  287. $type = $item->type;
  288. $modifiersString = '';
  289. foreach ($item->modifiers as $key => $value) {
  290. if ($this->view->plugin('doctype')->isHtml5()
  291. && $key == 'scheme'
  292. ) {
  293. throw new Exception\InvalidArgumentException(
  294. 'Invalid modifier "scheme" provided; not supported by HTML5'
  295. );
  296. }
  297. if (!in_array($key, $this->modifierKeys)) {
  298. continue;
  299. }
  300. $modifiersString .= $key . '="' . $this->escape($value) . '" ';
  301. }
  302. $modifiersString = rtrim($modifiersString);
  303. if ('' != $modifiersString) {
  304. $modifiersString = ' ' . $modifiersString;
  305. }
  306. if (method_exists($this->view, 'plugin')) {
  307. if ($this->view->plugin('doctype')->isHtml5()
  308. && $type == 'charset'
  309. ) {
  310. $tpl = ($this->view->plugin('doctype')->isXhtml())
  311. ? '<meta %s="%s"/>'
  312. : '<meta %s="%s">';
  313. } elseif ($this->view->plugin('doctype')->isXhtml()) {
  314. $tpl = '<meta %s="%s" content="%s"%s />';
  315. } else {
  316. $tpl = '<meta %s="%s" content="%s"%s>';
  317. }
  318. } else {
  319. $tpl = '<meta %s="%s" content="%s"%s />';
  320. }
  321. $meta = sprintf(
  322. $tpl,
  323. $type,
  324. $this->escape($item->$type),
  325. $this->escape($item->content),
  326. $modifiersString
  327. );
  328. if (isset($item->modifiers['conditional'])
  329. && !empty($item->modifiers['conditional'])
  330. && is_string($item->modifiers['conditional']))
  331. {
  332. $meta = '<!--[if ' . $this->escape($item->modifiers['conditional']) . ']>' . $meta . '<![endif]-->';
  333. }
  334. return $meta;
  335. }
  336. /**
  337. * Render placeholder as string
  338. *
  339. * @param string|int $indent
  340. * @return string
  341. */
  342. public function toString($indent = null)
  343. {
  344. $indent = (null !== $indent)
  345. ? $this->getWhitespace($indent)
  346. : $this->getIndent();
  347. $items = array();
  348. $this->getContainer()->ksort();
  349. try {
  350. foreach ($this as $item) {
  351. $items[] = $this->itemToString($item);
  352. }
  353. } catch (Exception\InvalidArgumentException $e) {
  354. trigger_error($e->getMessage(), E_USER_WARNING);
  355. return '';
  356. }
  357. return $indent . implode($this->escape($this->getSeparator()) . $indent, $items);
  358. }
  359. /**
  360. * Create data item for inserting into stack
  361. *
  362. * @param string $type
  363. * @param string $typeValue
  364. * @param string $content
  365. * @param array $modifiers
  366. * @return stdClass
  367. */
  368. public function createData($type, $typeValue, $content, array $modifiers)
  369. {
  370. $data = new stdClass;
  371. $data->type = $type;
  372. $data->$type = $typeValue;
  373. $data->content = $content;
  374. $data->modifiers = $modifiers;
  375. return $data;
  376. }
  377. }