HeadLink.php 13 KB

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