HeadLink.php 14 KB

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