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

/ZendFramework/library/Zend/View/Helper/HeadLink.php

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