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

/vendor/magento/zendframework1/library/Zend/View/Helper/HeadMeta.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 449 lines | 234 code | 41 blank | 174 comment | 47 complexity | aa10c18a6682c14eb546be878882b0ef 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-2015 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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. * @method $this appendHttpEquiv($keyValue, $content, $conditionalHttpEquiv)
  34. * @method $this appendName($keyValue, $content, $conditionalName)
  35. * @method $this appendProperty($property, $content, $modifiers)
  36. * @method $this offsetSetHttpEquiv($index, $keyValue, $content, $conditionalHttpEquiv)
  37. * @method $this offsetSetName($index, $keyValue, $content, $conditionalName)
  38. * @method $this offsetSetProperty($index, $property, $content, $modifiers)
  39. * @method $this prependHttpEquiv($keyValue, $content, $conditionalHttpEquiv)
  40. * @method $this prependName($keyValue, $content, $conditionalName)
  41. * @method $this prependProperty($property, $content, $modifiers)
  42. * @method $this setCharset($charset)
  43. * @method $this setHttpEquiv($keyValue, $content, $modifiers)
  44. * @method $this setName($keyValue, $content, $modifiers)
  45. * @method $this setProperty($property, $content, $modifiers)
  46. */
  47. class Zend_View_Helper_HeadMeta extends Zend_View_Helper_Placeholder_Container_Standalone
  48. {
  49. /**
  50. * Types of attributes
  51. * @var array
  52. */
  53. protected $_typeKeys = array('name', 'http-equiv', 'charset', 'property');
  54. protected $_requiredKeys = array('content');
  55. protected $_modifierKeys = array('lang', 'scheme');
  56. /**
  57. * @var string registry key
  58. */
  59. protected $_regKey = 'Zend_View_Helper_HeadMeta';
  60. /**
  61. * Constructor
  62. *
  63. * Set separator to PHP_EOL
  64. *
  65. * @return void
  66. */
  67. public function __construct()
  68. {
  69. parent::__construct();
  70. $this->setSeparator(PHP_EOL);
  71. }
  72. /**
  73. * Retrieve object instance; optionally add meta tag
  74. *
  75. * @param string $content
  76. * @param string $keyValue
  77. * @param string $keyType
  78. * @param array $modifiers
  79. * @param string $placement
  80. * @return Zend_View_Helper_HeadMeta
  81. */
  82. public function headMeta($content = null, $keyValue = null, $keyType = 'name', $modifiers = array(), $placement = Zend_View_Helper_Placeholder_Container_Abstract::APPEND)
  83. {
  84. if ((null !== $content) && (null !== $keyValue)) {
  85. $item = $this->createData($keyType, $keyValue, $content, $modifiers);
  86. $action = strtolower($placement);
  87. switch ($action) {
  88. case 'append':
  89. case 'prepend':
  90. case 'set':
  91. $this->$action($item);
  92. break;
  93. default:
  94. $this->append($item);
  95. break;
  96. }
  97. }
  98. return $this;
  99. }
  100. protected function _normalizeType($type)
  101. {
  102. switch ($type) {
  103. case 'Name':
  104. return 'name';
  105. case 'HttpEquiv':
  106. return 'http-equiv';
  107. case 'Property':
  108. return 'property';
  109. default:
  110. #require_once 'Zend/View/Exception.php';
  111. $e = new Zend_View_Exception(sprintf('Invalid type "%s" passed to _normalizeType', $type));
  112. $e->setView($this->view);
  113. throw $e;
  114. }
  115. }
  116. /**
  117. * Overload method access
  118. *
  119. * Allows the following 'virtual' methods:
  120. * - appendName($keyValue, $content, $modifiers = array())
  121. * - offsetGetName($index, $keyValue, $content, $modifers = array())
  122. * - prependName($keyValue, $content, $modifiers = array())
  123. * - setName($keyValue, $content, $modifiers = array())
  124. * - appendHttpEquiv($keyValue, $content, $modifiers = array())
  125. * - offsetGetHttpEquiv($index, $keyValue, $content, $modifers = array())
  126. * - prependHttpEquiv($keyValue, $content, $modifiers = array())
  127. * - setHttpEquiv($keyValue, $content, $modifiers = array())
  128. * - appendProperty($keyValue, $content, $modifiers = array())
  129. * - offsetGetProperty($index, $keyValue, $content, $modifiers = array())
  130. * - prependProperty($keyValue, $content, $modifiers = array())
  131. * - setProperty($keyValue, $content, $modifiers = array())
  132. *
  133. * @param string $method
  134. * @param array $args
  135. * @return Zend_View_Helper_HeadMeta
  136. */
  137. public function __call($method, $args)
  138. {
  139. if (preg_match('/^(?P<action>set|(pre|ap)pend|offsetSet)(?P<type>Name|HttpEquiv|Property)$/', $method, $matches)) {
  140. $action = $matches['action'];
  141. $type = $this->_normalizeType($matches['type']);
  142. $argc = count($args);
  143. $index = null;
  144. if ('offsetSet' == $action) {
  145. if (0 < $argc) {
  146. $index = array_shift($args);
  147. --$argc;
  148. }
  149. }
  150. if (2 > $argc) {
  151. #require_once 'Zend/View/Exception.php';
  152. $e = new Zend_View_Exception('Too few arguments provided; requires key value, and content');
  153. $e->setView($this->view);
  154. throw $e;
  155. }
  156. if (3 > $argc) {
  157. $args[] = array();
  158. }
  159. $item = $this->createData($type, $args[0], $args[1], $args[2]);
  160. if ('offsetSet' == $action) {
  161. return $this->offsetSet($index, $item);
  162. }
  163. $this->$action($item);
  164. return $this;
  165. }
  166. return parent::__call($method, $args);
  167. }
  168. /**
  169. * Create an HTML5-style meta charset tag. Something like <meta charset="utf-8">
  170. *
  171. * Not valid in a non-HTML5 doctype
  172. *
  173. * @param string $charset
  174. * @return Zend_View_Helper_HeadMeta Provides a fluent interface
  175. */
  176. public function setCharset($charset)
  177. {
  178. $item = new stdClass;
  179. $item->type = 'charset';
  180. $item->charset = $charset;
  181. $item->content = null;
  182. $item->modifiers = array();
  183. $this->set($item);
  184. return $this;
  185. }
  186. /**
  187. * Determine if item is valid
  188. *
  189. * @param mixed $item
  190. * @return boolean
  191. */
  192. protected function _isValid($item)
  193. {
  194. if ((!$item instanceof stdClass)
  195. || !isset($item->type)
  196. || !isset($item->modifiers))
  197. {
  198. return false;
  199. }
  200. $isHtml5 = is_null($this->view) ? false : $this->view->doctype()->isHtml5();
  201. if (!isset($item->content)
  202. && (! $isHtml5 || (! $isHtml5 && $item->type !== 'charset'))) {
  203. return false;
  204. }
  205. // <meta property= ... /> is only supported with doctype RDFa
  206. if ( !is_null($this->view) && !$this->view->doctype()->isRdfa()
  207. && $item->type === 'property') {
  208. return false;
  209. }
  210. return true;
  211. }
  212. /**
  213. * Append
  214. *
  215. * @param string $value
  216. * @return void
  217. * @throws Zend_View_Exception
  218. */
  219. public function append($value)
  220. {
  221. if (!$this->_isValid($value)) {
  222. #require_once 'Zend/View/Exception.php';
  223. $e = new Zend_View_Exception('Invalid value passed to append; please use appendMeta()');
  224. $e->setView($this->view);
  225. throw $e;
  226. }
  227. return $this->getContainer()->append($value);
  228. }
  229. /**
  230. * OffsetSet
  231. *
  232. * @param string|int $index
  233. * @param string $value
  234. * @return void
  235. * @throws Zend_View_Exception
  236. */
  237. public function offsetSet($index, $value)
  238. {
  239. if (!$this->_isValid($value)) {
  240. #require_once 'Zend/View/Exception.php';
  241. $e = new Zend_View_Exception('Invalid value passed to offsetSet; please use offsetSetName() or offsetSetHttpEquiv()');
  242. $e->setView($this->view);
  243. throw $e;
  244. }
  245. return $this->getContainer()->offsetSet($index, $value);
  246. }
  247. /**
  248. * OffsetUnset
  249. *
  250. * @param string|int $index
  251. * @return void
  252. * @throws Zend_View_Exception
  253. */
  254. public function offsetUnset($index)
  255. {
  256. if (!in_array($index, $this->getContainer()->getKeys())) {
  257. #require_once 'Zend/View/Exception.php';
  258. $e = new Zend_View_Exception('Invalid index passed to offsetUnset()');
  259. $e->setView($this->view);
  260. throw $e;
  261. }
  262. return $this->getContainer()->offsetUnset($index);
  263. }
  264. /**
  265. * Prepend
  266. *
  267. * @param string $value
  268. * @return void
  269. * @throws Zend_View_Exception
  270. */
  271. public function prepend($value)
  272. {
  273. if (!$this->_isValid($value)) {
  274. #require_once 'Zend/View/Exception.php';
  275. $e = new Zend_View_Exception('Invalid value passed to prepend; please use prependMeta()');
  276. $e->setView($this->view);
  277. throw $e;
  278. }
  279. return $this->getContainer()->prepend($value);
  280. }
  281. /**
  282. * Set
  283. *
  284. * @param string $value
  285. * @return void
  286. * @throws Zend_View_Exception
  287. */
  288. public function set($value)
  289. {
  290. if (!$this->_isValid($value)) {
  291. #require_once 'Zend/View/Exception.php';
  292. $e = new Zend_View_Exception('Invalid value passed to set; please use setMeta()');
  293. $e->setView($this->view);
  294. throw $e;
  295. }
  296. $container = $this->getContainer();
  297. foreach ($container->getArrayCopy() as $index => $item) {
  298. if ($item->type == $value->type && $item->{$item->type} == $value->{$value->type}) {
  299. $this->offsetUnset($index);
  300. }
  301. }
  302. return $this->append($value);
  303. }
  304. /**
  305. * Build meta HTML string
  306. *
  307. * @param string $type
  308. * @param string $typeValue
  309. * @param string $content
  310. * @param array $modifiers
  311. * @return string
  312. */
  313. public function itemToString(stdClass $item)
  314. {
  315. if (!in_array($item->type, $this->_typeKeys)) {
  316. #require_once 'Zend/View/Exception.php';
  317. $e = new Zend_View_Exception(sprintf('Invalid type "%s" provided for meta', $item->type));
  318. $e->setView($this->view);
  319. throw $e;
  320. }
  321. $type = $item->type;
  322. $modifiersString = '';
  323. foreach ($item->modifiers as $key => $value) {
  324. if (!is_null($this->view) && $this->view->doctype()->isHtml5()
  325. && $key == 'scheme') {
  326. #require_once 'Zend/View/Exception.php';
  327. throw new Zend_View_Exception('Invalid modifier '
  328. . '"scheme" provided; not supported by HTML5');
  329. }
  330. if (!in_array($key, $this->_modifierKeys)) {
  331. continue;
  332. }
  333. $modifiersString .= $key . '="' . $this->_escape($value) . '" ';
  334. }
  335. if ($this->view instanceof Zend_View_Abstract) {
  336. if ($this->view->doctype()->isHtml5()
  337. && $type == 'charset') {
  338. $tpl = ($this->view->doctype()->isXhtml())
  339. ? '<meta %s="%s"/>'
  340. : '<meta %s="%s">';
  341. } elseif ($this->view->doctype()->isXhtml()) {
  342. $tpl = '<meta %s="%s" content="%s" %s/>';
  343. } else {
  344. $tpl = '<meta %s="%s" content="%s" %s>';
  345. }
  346. } else {
  347. $tpl = '<meta %s="%s" content="%s" %s/>';
  348. }
  349. $meta = sprintf(
  350. $tpl,
  351. $type,
  352. $this->_escape($item->$type),
  353. $this->_escape($item->content),
  354. $modifiersString
  355. );
  356. if (isset($item->modifiers['conditional'])
  357. && !empty($item->modifiers['conditional'])
  358. && is_string($item->modifiers['conditional']))
  359. {
  360. if (str_replace(' ', '', $item->modifiers['conditional']) === '!IE') {
  361. $meta = '<!-->' . $meta . '<!--';
  362. }
  363. $meta = '<!--[if ' . $this->_escape($item->modifiers['conditional']) . ']>' . $meta . '<![endif]-->';
  364. }
  365. return $meta;
  366. }
  367. /**
  368. * Render placeholder as string
  369. *
  370. * @param string|int $indent
  371. * @return string
  372. */
  373. public function toString($indent = null)
  374. {
  375. $indent = (null !== $indent)
  376. ? $this->getWhitespace($indent)
  377. : $this->getIndent();
  378. $items = array();
  379. $this->getContainer()->ksort();
  380. try {
  381. foreach ($this as $item) {
  382. $items[] = $this->itemToString($item);
  383. }
  384. } catch (Zend_View_Exception $e) {
  385. trigger_error($e->getMessage(), E_USER_WARNING);
  386. return '';
  387. }
  388. return $indent . implode($this->_escape($this->getSeparator()) . $indent, $items);
  389. }
  390. /**
  391. * Create data item for inserting into stack
  392. *
  393. * @param string $type
  394. * @param string $typeValue
  395. * @param string $content
  396. * @param array $modifiers
  397. * @return stdClass
  398. */
  399. public function createData($type, $typeValue, $content, array $modifiers)
  400. {
  401. $data = new stdClass;
  402. $data->type = $type;
  403. $data->$type = $typeValue;
  404. $data->content = $content;
  405. $data->modifiers = $modifiers;
  406. return $data;
  407. }
  408. }