PageRenderTime 25ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://firephp.googlecode.com/
PHP | 396 lines | 198 code | 43 blank | 155 comment | 37 complexity | 4b29bd56fce9087665288b9724f67d8c MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT, Apache-2.0
  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');
  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)
  89. * - offsetSetStylesheet($index, $href, $media, $conditionalStylesheet)
  90. * - prependStylesheet($href, $media, $conditionalStylesheet)
  91. * - setStylesheet($href, $media, $conditionalStylesheet)
  92. * - appendAlternate($href, $type, $title)
  93. * - offsetSetAlternate($index, $href, $type, $title)
  94. * - prependAlternate($href, $type, $title)
  95. * - setAlternate($href, $type, $title)
  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. $link .= sprintf('%s="%s" ', $itemKey, ($this->_autoEscape) ? $this->_escape($attributes[$itemKey]) : $attributes[$itemKey]);
  242. }
  243. }
  244. if ($this->view instanceof Zend_View_Abstract) {
  245. $link .= ($this->view->doctype()->isXhtml()) ? '/>' : '>';
  246. } else {
  247. $link .= '/>';
  248. }
  249. if (($link == '<link />') || ($link == '<link >')) {
  250. return '';
  251. }
  252. if (isset($attributes['conditionalStylesheet'])
  253. && (false !== $attributes['conditionalStylesheet']))
  254. {
  255. $link = '<!--[if ' . $attributes['conditionalStylesheet'] . ']> ' . $link . '<![endif]-->';
  256. }
  257. return $link;
  258. }
  259. /**
  260. * Render link elements as string
  261. *
  262. * @param string|int $indent
  263. * @return string
  264. */
  265. public function toString($indent = null)
  266. {
  267. $indent = (null !== $indent)
  268. ? $this->getWhitespace($indent)
  269. : $this->getIndent();
  270. $items = array();
  271. foreach ($this as $item) {
  272. $items[] = $this->itemToString($item);
  273. }
  274. return $indent . implode($this->_escape($this->getSeparator()) . $indent, $items);
  275. }
  276. /**
  277. * Create data item for stack
  278. *
  279. * @param array $attributes
  280. * @return stdClass
  281. */
  282. public function createData(array $attributes)
  283. {
  284. $data = (object) $attributes;
  285. return $data;
  286. }
  287. /**
  288. * Create item for stylesheet link item
  289. *
  290. * @param array $args
  291. * @return stdClass|false Returns fals if stylesheet is a duplicate
  292. */
  293. public function createDataStylesheet(array $args)
  294. {
  295. $rel = 'stylesheet';
  296. $type = 'text/css';
  297. $media = 'screen';
  298. $conditionalStylesheet = false;
  299. $href = array_shift($args);
  300. if ($this->_isDuplicateStylesheet($href)) {
  301. return false;
  302. }
  303. if (0 < count($args)) {
  304. $media = array_shift($args);
  305. $media = (string) $media;
  306. }
  307. if (0 < count($args)) {
  308. $conditionalStylesheet = array_shift($args);
  309. if (false !== $conditionalStylesheet) {
  310. $conditionalStylesheet = (string) $conditionalStylesheet;
  311. }
  312. }
  313. $attributes = compact('rel', 'type', 'href', 'media', 'conditionalStylesheet');
  314. return $this->createData($attributes);
  315. }
  316. /**
  317. * Is the linked stylesheet a duplicate?
  318. *
  319. * @param string $uri
  320. * @return bool
  321. */
  322. protected function _isDuplicateStylesheet($uri)
  323. {
  324. foreach ($this->getContainer() as $item) {
  325. if (($item->rel == 'stylesheet') && ($item->href == $uri)) {
  326. return true;
  327. }
  328. }
  329. return false;
  330. }
  331. /**
  332. * Create item for alternate link item
  333. *
  334. * @param array $args
  335. * @return stdClass
  336. */
  337. public function createDataAlternate(array $args)
  338. {
  339. if (3 > count($args)) {
  340. require_once 'Zend/View/Exception.php';
  341. throw new Zend_View_Exception(sprintf('Alternate tags require 3 arguments; %s provided', count($args)));
  342. }
  343. $rel = 'alternate';
  344. $href = array_shift($args);
  345. $type = array_shift($args);
  346. $title = array_shift($args);
  347. $href = (string) $href;
  348. $type = (string) $type;
  349. $title = (string) $title;
  350. $attributes = compact('rel', 'href', 'type', 'title');
  351. return $this->createData($attributes);
  352. }
  353. }