2
0

HeadLink.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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-2009 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-2009 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', '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. throw new Zend_View_Exception(sprintf('%s requires at least one argument', $method));
  136. }
  137. if (is_array($args[0])) {
  138. $item = $this->createData($args[0]);
  139. } else {
  140. $dataMethod = 'createData' . $type;
  141. $item = $this->$dataMethod($args);
  142. }
  143. if ($item) {
  144. if ('offsetSet' == $action) {
  145. $this->offsetSet($index, $item);
  146. } else {
  147. $this->$action($item);
  148. }
  149. }
  150. return $this;
  151. }
  152. return parent::__call($method, $args);
  153. }
  154. /**
  155. * Check if value is valid
  156. *
  157. * @param mixed $value
  158. * @return boolean
  159. */
  160. protected function _isValid($value)
  161. {
  162. if (!$value instanceof stdClass) {
  163. return false;
  164. }
  165. $vars = get_object_vars($value);
  166. $keys = array_keys($vars);
  167. $intersection = array_intersect($this->_itemKeys, $keys);
  168. if (empty($intersection)) {
  169. return false;
  170. }
  171. return true;
  172. }
  173. /**
  174. * append()
  175. *
  176. * @param array $value
  177. * @return void
  178. */
  179. public function append($value)
  180. {
  181. if (!$this->_isValid($value)) {
  182. require_once 'Zend/View/Exception.php';
  183. throw new Zend_View_Exception('append() expects a data token; please use one of the custom append*() methods');
  184. }
  185. return $this->getContainer()->append($value);
  186. }
  187. /**
  188. * offsetSet()
  189. *
  190. * @param string|int $index
  191. * @param array $value
  192. * @return void
  193. */
  194. public function offsetSet($index, $value)
  195. {
  196. if (!$this->_isValid($value)) {
  197. require_once 'Zend/View/Exception.php';
  198. throw new Zend_View_Exception('offsetSet() expects a data token; please use one of the custom offsetSet*() methods');
  199. }
  200. return $this->getContainer()->offsetSet($index, $value);
  201. }
  202. /**
  203. * prepend()
  204. *
  205. * @param array $value
  206. * @return Zend_Layout_ViewHelper_HeadLink
  207. */
  208. public function prepend($value)
  209. {
  210. if (!$this->_isValid($value)) {
  211. require_once 'Zend/View/Exception.php';
  212. throw new Zend_View_Exception('prepend() expects a data token; please use one of the custom prepend*() methods');
  213. }
  214. return $this->getContainer()->prepend($value);
  215. }
  216. /**
  217. * set()
  218. *
  219. * @param array $value
  220. * @return Zend_Layout_ViewHelper_HeadLink
  221. */
  222. public function set($value)
  223. {
  224. if (!$this->_isValid($value)) {
  225. require_once 'Zend/View/Exception.php';
  226. throw new Zend_View_Exception('set() expects a data token; please use one of the custom set*() methods');
  227. }
  228. return $this->getContainer()->set($value);
  229. }
  230. /**
  231. * Create HTML link element from data item
  232. *
  233. * @param stdClass $item
  234. * @return string
  235. */
  236. public function itemToString(stdClass $item)
  237. {
  238. $attributes = (array) $item;
  239. $link = '<link ';
  240. foreach ($this->_itemKeys as $itemKey) {
  241. if (isset($attributes[$itemKey])) {
  242. if(is_array($attributes[$itemKey])) {
  243. foreach($attributes[$itemKey] as $key => $value) {
  244. $link .= sprintf('%s="%s" ', $key, ($this->_autoEscape) ? $this->_escape($value) : $value);
  245. }
  246. } else {
  247. $link .= sprintf('%s="%s" ', $itemKey, ($this->_autoEscape) ? $this->_escape($attributes[$itemKey]) : $attributes[$itemKey]);
  248. }
  249. }
  250. }
  251. if ($this->view instanceof Zend_View_Abstract) {
  252. $link .= ($this->view->doctype()->isXhtml()) ? '/>' : '>';
  253. } else {
  254. $link .= '/>';
  255. }
  256. if (($link == '<link />') || ($link == '<link >')) {
  257. return '';
  258. }
  259. if (isset($attributes['conditionalStylesheet'])
  260. && !empty($attributes['conditionalStylesheet'])
  261. && is_string($attributes['conditionalStylesheet']))
  262. {
  263. $link = '<!--[if ' . $attributes['conditionalStylesheet'] . ']> ' . $link . '<![endif]-->';
  264. }
  265. return $link;
  266. }
  267. /**
  268. * Render link elements as string
  269. *
  270. * @param string|int $indent
  271. * @return string
  272. */
  273. public function toString($indent = null)
  274. {
  275. $indent = (null !== $indent)
  276. ? $this->getWhitespace($indent)
  277. : $this->getIndent();
  278. $items = array();
  279. $this->getContainer()->ksort();
  280. foreach ($this as $item) {
  281. $items[] = $this->itemToString($item);
  282. }
  283. return $indent . implode($this->_escape($this->getSeparator()) . $indent, $items);
  284. }
  285. /**
  286. * Create data item for stack
  287. *
  288. * @param array $attributes
  289. * @return stdClass
  290. */
  291. public function createData(array $attributes)
  292. {
  293. $data = (object) $attributes;
  294. return $data;
  295. }
  296. /**
  297. * Create item for stylesheet link item
  298. *
  299. * @param array $args
  300. * @return stdClass|false Returns fals if stylesheet is a duplicate
  301. */
  302. public function createDataStylesheet(array $args)
  303. {
  304. $rel = 'stylesheet';
  305. $type = 'text/css';
  306. $media = 'screen';
  307. $conditionalStylesheet = false;
  308. $href = array_shift($args);
  309. if ($this->_isDuplicateStylesheet($href)) {
  310. return false;
  311. }
  312. if (0 < count($args)) {
  313. $media = array_shift($args);
  314. if(is_array($media)) {
  315. $media = implode(',', $media);
  316. } else {
  317. $media = (string) $media;
  318. }
  319. }
  320. if (0 < count($args)) {
  321. $conditionalStylesheet = array_shift($args);
  322. if(!empty($conditionalStylesheet) && is_string($conditionalStylesheet)) {
  323. $conditionalStylesheet = (string) $conditionalStylesheet;
  324. } else {
  325. $conditionalStylesheet = null;
  326. }
  327. }
  328. if(0 < count($args) && is_array($args[0])) {
  329. $extras = array_shift($args);
  330. $extras = (array) $extras;
  331. }
  332. $attributes = compact('rel', 'type', 'href', 'media', 'conditionalStylesheet', 'extras');
  333. return $this->createData($attributes);
  334. }
  335. /**
  336. * Is the linked stylesheet a duplicate?
  337. *
  338. * @param string $uri
  339. * @return bool
  340. */
  341. protected function _isDuplicateStylesheet($uri)
  342. {
  343. foreach ($this->getContainer() as $item) {
  344. if (($item->rel == 'stylesheet') && ($item->href == $uri)) {
  345. return true;
  346. }
  347. }
  348. return false;
  349. }
  350. /**
  351. * Create item for alternate link item
  352. *
  353. * @param array $args
  354. * @return stdClass
  355. */
  356. public function createDataAlternate(array $args)
  357. {
  358. if (3 > count($args)) {
  359. require_once 'Zend/View/Exception.php';
  360. throw new Zend_View_Exception(sprintf('Alternate tags require 3 arguments; %s provided', count($args)));
  361. }
  362. $rel = 'alternate';
  363. $href = array_shift($args);
  364. $type = array_shift($args);
  365. $title = array_shift($args);
  366. if(0 < count($args) && is_array($args[0])) {
  367. $extras = array_shift($args);
  368. $extras = (array) $extras;
  369. if(isset($extras['media']) && is_array($extras['media'])) {
  370. $extras['media'] = implode(',', $extras['media']);
  371. }
  372. }
  373. $href = (string) $href;
  374. $type = (string) $type;
  375. $title = (string) $title;
  376. $attributes = compact('rel', 'href', 'type', 'title', 'extras');
  377. return $this->createData($attributes);
  378. }
  379. }