PageRenderTime 70ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/src/application/libraries/Zend/View/Helper/HeadLink.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 437 lines | 235 code | 46 blank | 156 comment | 50 complexity | e41cea6b9d5a90958e47879f616318f4 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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @version $Id: HeadLink.php 23775 2011-03-01 17:25:24Z ralph $
  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_HeadLink
  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-2011 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_HeadLink extends Zend_View_Helper_Placeholder_Container_Standalone
  35. {
  36. /**
  37. * $_validAttributes
  38. *
  39. * @var array
  40. */
  41. protected $_itemKeys = array('charset', 'href', 'hreflang', 'id', 'media', 'rel', 'rev', 'type', 'title', 'extras');
  42. /**
  43. * @var string registry key
  44. */
  45. protected $_regKey = 'Zend_View_Helper_HeadLink';
  46. /**
  47. * Constructor
  48. *
  49. * Use PHP_EOL as separator
  50. *
  51. * @return void
  52. */
  53. public function __construct()
  54. {
  55. parent::__construct();
  56. $this->setSeparator(PHP_EOL);
  57. }
  58. /**
  59. * headLink() - View Helper Method
  60. *
  61. * Returns current object instance. Optionally, allows passing array of
  62. * values to build link.
  63. *
  64. * @return Zend_View_Helper_HeadLink
  65. */
  66. public function headLink(array $attributes = null, $placement = Zend_View_Helper_Placeholder_Container_Abstract::APPEND)
  67. {
  68. if (null !== $attributes) {
  69. $item = $this->createData($attributes);
  70. switch ($placement) {
  71. case Zend_View_Helper_Placeholder_Container_Abstract::SET:
  72. $this->set($item);
  73. break;
  74. case Zend_View_Helper_Placeholder_Container_Abstract::PREPEND:
  75. $this->prepend($item);
  76. break;
  77. case Zend_View_Helper_Placeholder_Container_Abstract::APPEND:
  78. default:
  79. $this->append($item);
  80. break;
  81. }
  82. }
  83. return $this;
  84. }
  85. /**
  86. * Overload method access
  87. *
  88. * Creates the following virtual methods:
  89. * - appendStylesheet($href, $media, $conditionalStylesheet, $extras)
  90. * - offsetSetStylesheet($index, $href, $media, $conditionalStylesheet, $extras)
  91. * - prependStylesheet($href, $media, $conditionalStylesheet, $extras)
  92. * - setStylesheet($href, $media, $conditionalStylesheet, $extras)
  93. * - appendAlternate($href, $type, $title, $extras)
  94. * - offsetSetAlternate($index, $href, $type, $title, $extras)
  95. * - prependAlternate($href, $type, $title, $extras)
  96. * - setAlternate($href, $type, $title, $extras)
  97. *
  98. * Items that may be added in the future:
  99. * - Navigation? need to find docs on this
  100. * - public function appendStart()
  101. * - public function appendContents()
  102. * - public function appendPrev()
  103. * - public function appendNext()
  104. * - public function appendIndex()
  105. * - public function appendEnd()
  106. * - public function appendGlossary()
  107. * - public function appendAppendix()
  108. * - public function appendHelp()
  109. * - public function appendBookmark()
  110. * - Other?
  111. * - public function appendCopyright()
  112. * - public function appendChapter()
  113. * - public function appendSection()
  114. * - public function appendSubsection()
  115. *
  116. * @param mixed $method
  117. * @param mixed $args
  118. * @return void
  119. */
  120. public function __call($method, $args)
  121. {
  122. if (preg_match('/^(?P<action>set|(ap|pre)pend|offsetSet)(?P<type>Stylesheet|Alternate)$/', $method, $matches)) {
  123. $argc = count($args);
  124. $action = $matches['action'];
  125. $type = $matches['type'];
  126. $index = null;
  127. if ('offsetSet' == $action) {
  128. if (0 < $argc) {
  129. $index = array_shift($args);
  130. --$argc;
  131. }
  132. }
  133. if (1 > $argc) {
  134. require_once 'Zend/View/Exception.php';
  135. $e = new Zend_View_Exception(sprintf('%s requires at least one argument', $method));
  136. $e->setView($this->view);
  137. throw $e;
  138. }
  139. if (is_array($args[0])) {
  140. $item = $this->createData($args[0]);
  141. } else {
  142. $dataMethod = 'createData' . $type;
  143. $item = $this->$dataMethod($args);
  144. }
  145. if ($item) {
  146. if ('offsetSet' == $action) {
  147. $this->offsetSet($index, $item);
  148. } else {
  149. $this->$action($item);
  150. }
  151. }
  152. return $this;
  153. }
  154. return parent::__call($method, $args);
  155. }
  156. /**
  157. * Check if value is valid
  158. *
  159. * @param mixed $value
  160. * @return boolean
  161. */
  162. protected function _isValid($value)
  163. {
  164. if (!$value instanceof stdClass) {
  165. return false;
  166. }
  167. $vars = get_object_vars($value);
  168. $keys = array_keys($vars);
  169. $intersection = array_intersect($this->_itemKeys, $keys);
  170. if (empty($intersection)) {
  171. return false;
  172. }
  173. return true;
  174. }
  175. /**
  176. * append()
  177. *
  178. * @param array $value
  179. * @return void
  180. */
  181. public function append($value)
  182. {
  183. if (!$this->_isValid($value)) {
  184. require_once 'Zend/View/Exception.php';
  185. $e = new Zend_View_Exception('append() expects a data token; please use one of the custom append*() methods');
  186. $e->setView($this->view);
  187. throw $e;
  188. }
  189. return $this->getContainer()->append($value);
  190. }
  191. /**
  192. * offsetSet()
  193. *
  194. * @param string|int $index
  195. * @param array $value
  196. * @return void
  197. */
  198. public function offsetSet($index, $value)
  199. {
  200. if (!$this->_isValid($value)) {
  201. require_once 'Zend/View/Exception.php';
  202. $e = new Zend_View_Exception('offsetSet() expects a data token; please use one of the custom offsetSet*() methods');
  203. $e->setView($this->view);
  204. throw $e;
  205. }
  206. return $this->getContainer()->offsetSet($index, $value);
  207. }
  208. /**
  209. * prepend()
  210. *
  211. * @param array $value
  212. * @return Zend_Layout_ViewHelper_HeadLink
  213. */
  214. public function prepend($value)
  215. {
  216. if (!$this->_isValid($value)) {
  217. require_once 'Zend/View/Exception.php';
  218. $e = new Zend_View_Exception('prepend() expects a data token; please use one of the custom prepend*() methods');
  219. $e->setView($this->view);
  220. throw $e;
  221. }
  222. return $this->getContainer()->prepend($value);
  223. }
  224. /**
  225. * set()
  226. *
  227. * @param array $value
  228. * @return Zend_Layout_ViewHelper_HeadLink
  229. */
  230. public function set($value)
  231. {
  232. if (!$this->_isValid($value)) {
  233. require_once 'Zend/View/Exception.php';
  234. $e = new Zend_View_Exception('set() expects a data token; please use one of the custom set*() methods');
  235. $e->setView($this->view);
  236. throw $e;
  237. }
  238. return $this->getContainer()->set($value);
  239. }
  240. /**
  241. * Create HTML link element from data item
  242. *
  243. * @param stdClass $item
  244. * @return string
  245. */
  246. public function itemToString(stdClass $item)
  247. {
  248. $attributes = (array) $item;
  249. $link = '<link ';
  250. foreach ($this->_itemKeys as $itemKey) {
  251. if (isset($attributes[$itemKey])) {
  252. if(is_array($attributes[$itemKey])) {
  253. foreach($attributes[$itemKey] as $key => $value) {
  254. $link .= sprintf('%s="%s" ', $key, ($this->_autoEscape) ? $this->_escape($value) : $value);
  255. }
  256. } else {
  257. $link .= sprintf('%s="%s" ', $itemKey, ($this->_autoEscape) ? $this->_escape($attributes[$itemKey]) : $attributes[$itemKey]);
  258. }
  259. }
  260. }
  261. if ($this->view instanceof Zend_View_Abstract) {
  262. $link .= ($this->view->doctype()->isXhtml()) ? '/>' : '>';
  263. } else {
  264. $link .= '/>';
  265. }
  266. if (($link == '<link />') || ($link == '<link >')) {
  267. return '';
  268. }
  269. if (isset($attributes['conditionalStylesheet'])
  270. && !empty($attributes['conditionalStylesheet'])
  271. && is_string($attributes['conditionalStylesheet']))
  272. {
  273. $link = '<!--[if ' . $attributes['conditionalStylesheet'] . ']> ' . $link . '<![endif]-->';
  274. }
  275. return $link;
  276. }
  277. /**
  278. * Render link elements as string
  279. *
  280. * @param string|int $indent
  281. * @return string
  282. */
  283. public function toString($indent = null)
  284. {
  285. $indent = (null !== $indent)
  286. ? $this->getWhitespace($indent)
  287. : $this->getIndent();
  288. $items = array();
  289. $this->getContainer()->ksort();
  290. foreach ($this as $item) {
  291. $items[] = $this->itemToString($item);
  292. }
  293. return $indent . implode($this->_escape($this->getSeparator()) . $indent, $items);
  294. }
  295. /**
  296. * Create data item for stack
  297. *
  298. * @param array $attributes
  299. * @return stdClass
  300. */
  301. public function createData(array $attributes)
  302. {
  303. $data = (object) $attributes;
  304. return $data;
  305. }
  306. /**
  307. * Create item for stylesheet link item
  308. *
  309. * @param array $args
  310. * @return stdClass|false Returns fals if stylesheet is a duplicate
  311. */
  312. public function createDataStylesheet(array $args)
  313. {
  314. $rel = 'stylesheet';
  315. $type = 'text/css';
  316. $media = 'screen';
  317. $conditionalStylesheet = false;
  318. $href = array_shift($args);
  319. if ($this->_isDuplicateStylesheet($href)) {
  320. return false;
  321. }
  322. if (0 < count($args)) {
  323. $media = array_shift($args);
  324. if(is_array($media)) {
  325. $media = implode(',', $media);
  326. } else {
  327. $media = (string) $media;
  328. }
  329. }
  330. if (0 < count($args)) {
  331. $conditionalStylesheet = array_shift($args);
  332. if(!empty($conditionalStylesheet) && is_string($conditionalStylesheet)) {
  333. $conditionalStylesheet = (string) $conditionalStylesheet;
  334. } else {
  335. $conditionalStylesheet = null;
  336. }
  337. }
  338. if(0 < count($args) && is_array($args[0])) {
  339. $extras = array_shift($args);
  340. $extras = (array) $extras;
  341. }
  342. $attributes = compact('rel', 'type', 'href', 'media', 'conditionalStylesheet', 'extras');
  343. return $this->createData($attributes);
  344. }
  345. /**
  346. * Is the linked stylesheet a duplicate?
  347. *
  348. * @param string $uri
  349. * @return bool
  350. */
  351. protected function _isDuplicateStylesheet($uri)
  352. {
  353. foreach ($this->getContainer() as $item) {
  354. if (($item->rel == 'stylesheet') && ($item->href == $uri)) {
  355. return true;
  356. }
  357. }
  358. return false;
  359. }
  360. /**
  361. * Create item for alternate link item
  362. *
  363. * @param array $args
  364. * @return stdClass
  365. */
  366. public function createDataAlternate(array $args)
  367. {
  368. if (3 > count($args)) {
  369. require_once 'Zend/View/Exception.php';
  370. $e = new Zend_View_Exception(sprintf('Alternate tags require 3 arguments; %s provided', count($args)));
  371. $e->setView($this->view);
  372. throw $e;
  373. }
  374. $rel = 'alternate';
  375. $href = array_shift($args);
  376. $type = array_shift($args);
  377. $title = array_shift($args);
  378. if(0 < count($args) && is_array($args[0])) {
  379. $extras = array_shift($args);
  380. $extras = (array) $extras;
  381. if(isset($extras['media']) && is_array($extras['media'])) {
  382. $extras['media'] = implode(',', $extras['media']);
  383. }
  384. }
  385. $href = (string) $href;
  386. $type = (string) $type;
  387. $title = (string) $title;
  388. $attributes = compact('rel', 'href', 'type', 'title', 'extras');
  389. return $this->createData($attributes);
  390. }
  391. }