| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478 |
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_View
- * @subpackage Helper
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @version $Id$
- * @license http://framework.zend.com/license/new-bsd New BSD License
- */
- /** Zend_View_Helper_Placeholder_Container_Standalone */
- require_once 'Zend/View/Helper/Placeholder/Container/Standalone.php';
- /**
- * Zend_Layout_View_Helper_HeadLink
- *
- * @see http://www.w3.org/TR/xhtml1/dtds.html
- * @uses Zend_View_Helper_Placeholder_Container_Standalone
- * @package Zend_View
- * @subpackage Helper
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @method $this appendAlternate($href, $type, $title, $extras)
- * @method $this appendStylesheet($href, $media = 'screen', $conditionalStylesheet = false, array $extras = array())
- * @method $this offsetSetAlternate($index, $href, $type, $title, $extras)
- * @method $this offsetSetStylesheet($index, $href, $media = 'screen', $conditionalStylesheet = false, array $extras = array())
- * @method $this prependAlternate($href, $type, $title, $extras)
- * @method $this prependStylesheet($href, $media = 'screen', $conditionalStylesheet = false, array $extras = array())
- * @method $this setAlternate($href, $type, $title, $extras)
- * @method $this setStylesheet($href, $media = 'screen', $conditionalStylesheet = false, array $extras = array())
- */
- class Zend_View_Helper_HeadLink extends Zend_View_Helper_Placeholder_Container_Standalone
- {
- /**
- * $_validAttributes
- *
- * @var array
- */
- protected $_itemKeys = array(
- 'charset',
- 'href',
- 'hreflang',
- 'id',
- 'media',
- 'rel',
- 'rev',
- 'type',
- 'title',
- 'extras',
- 'sizes',
- );
- /**
- * @var string registry key
- */
- protected $_regKey = 'Zend_View_Helper_HeadLink';
- /**
- * Constructor
- *
- * Use PHP_EOL as separator
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- $this->setSeparator(PHP_EOL);
- }
- /**
- * headLink() - View Helper Method
- *
- * Returns current object instance. Optionally, allows passing array of
- * values to build link.
- *
- * @return Zend_View_Helper_HeadLink
- */
- public function headLink(array $attributes = null, $placement = Zend_View_Helper_Placeholder_Container_Abstract::APPEND)
- {
- if (null !== $attributes) {
- $item = $this->createData($attributes);
- switch ($placement) {
- case Zend_View_Helper_Placeholder_Container_Abstract::SET:
- $this->set($item);
- break;
- case Zend_View_Helper_Placeholder_Container_Abstract::PREPEND:
- $this->prepend($item);
- break;
- case Zend_View_Helper_Placeholder_Container_Abstract::APPEND:
- default:
- $this->append($item);
- break;
- }
- }
- return $this;
- }
- /**
- * Overload method access
- *
- * Creates the following virtual methods:
- * - appendStylesheet($href, $media, $conditionalStylesheet, $extras)
- * - offsetSetStylesheet($index, $href, $media, $conditionalStylesheet, $extras)
- * - prependStylesheet($href, $media, $conditionalStylesheet, $extras)
- * - setStylesheet($href, $media, $conditionalStylesheet, $extras)
- * - appendAlternate($href, $type, $title, $extras)
- * - offsetSetAlternate($index, $href, $type, $title, $extras)
- * - prependAlternate($href, $type, $title, $extras)
- * - setAlternate($href, $type, $title, $extras)
- *
- * Items that may be added in the future:
- * - Navigation? need to find docs on this
- * - public function appendStart()
- * - public function appendContents()
- * - public function appendPrev()
- * - public function appendNext()
- * - public function appendIndex()
- * - public function appendEnd()
- * - public function appendGlossary()
- * - public function appendAppendix()
- * - public function appendHelp()
- * - public function appendBookmark()
- * - Other?
- * - public function appendCopyright()
- * - public function appendChapter()
- * - public function appendSection()
- * - public function appendSubsection()
- *
- * @param mixed $method
- * @param mixed $args
- * @return void
- */
- public function __call($method, $args)
- {
- if (preg_match('/^(?P<action>set|(ap|pre)pend|offsetSet)(?P<type>Stylesheet|Alternate)$/', $method, $matches)) {
- $argc = count($args);
- $action = $matches['action'];
- $type = $matches['type'];
- $index = null;
- if ('offsetSet' == $action) {
- if (0 < $argc) {
- $index = array_shift($args);
- --$argc;
- }
- }
- if (1 > $argc) {
- require_once 'Zend/View/Exception.php';
- $e = new Zend_View_Exception(sprintf('%s requires at least one argument', $method));
- $e->setView($this->view);
- throw $e;
- }
- if (is_array($args[0])) {
- $item = $this->createData($args[0]);
- } else {
- $dataMethod = 'createData' . $type;
- $item = $this->$dataMethod($args);
- }
- if ($item) {
- if ('offsetSet' == $action) {
- $this->offsetSet($index, $item);
- } else {
- $this->$action($item);
- }
- }
- return $this;
- }
- return parent::__call($method, $args);
- }
- /**
- * Check if value is valid
- *
- * @param mixed $value
- * @return boolean
- */
- protected function _isValid($value)
- {
- if (!$value instanceof stdClass) {
- return false;
- }
- $vars = get_object_vars($value);
- $keys = array_keys($vars);
- $intersection = array_intersect($this->_itemKeys, $keys);
- if (empty($intersection)) {
- return false;
- }
- return true;
- }
- /**
- * append()
- *
- * @param array $value
- * @return void
- */
- public function append($value)
- {
- if (!$this->_isValid($value)) {
- require_once 'Zend/View/Exception.php';
- $e = new Zend_View_Exception('append() expects a data token; please use one of the custom append*() methods');
- $e->setView($this->view);
- throw $e;
- }
- return $this->getContainer()->append($value);
- }
- /**
- * offsetSet()
- *
- * @param string|int $index
- * @param array $value
- * @return void
- */
- public function offsetSet($index, $value)
- {
- if (!$this->_isValid($value)) {
- require_once 'Zend/View/Exception.php';
- $e = new Zend_View_Exception('offsetSet() expects a data token; please use one of the custom offsetSet*() methods');
- $e->setView($this->view);
- throw $e;
- }
- return $this->getContainer()->offsetSet($index, $value);
- }
- /**
- * prepend()
- *
- * @param array $value
- * @return Zend_Layout_ViewHelper_HeadLink
- */
- public function prepend($value)
- {
- if (!$this->_isValid($value)) {
- require_once 'Zend/View/Exception.php';
- $e = new Zend_View_Exception('prepend() expects a data token; please use one of the custom prepend*() methods');
- $e->setView($this->view);
- throw $e;
- }
- return $this->getContainer()->prepend($value);
- }
- /**
- * set()
- *
- * @param array $value
- * @return Zend_Layout_ViewHelper_HeadLink
- */
- public function set($value)
- {
- if (!$this->_isValid($value)) {
- require_once 'Zend/View/Exception.php';
- $e = new Zend_View_Exception('set() expects a data token; please use one of the custom set*() methods');
- $e->setView($this->view);
- throw $e;
- }
- return $this->getContainer()->set($value);
- }
- /**
- * Create HTML link element from data item
- *
- * @param stdClass $item
- * @return string
- */
- public function itemToString(stdClass $item)
- {
- $attributes = (array) $item;
- $link = '<link ';
- foreach ($this->_itemKeys as $itemKey) {
- if (isset($attributes[$itemKey])) {
- if(is_array($attributes[$itemKey])) {
- foreach($attributes[$itemKey] as $key => $value) {
- $link .= sprintf('%s="%s" ', $key, ($this->_autoEscape) ? $this->_escape($value) : $value);
- }
- } else {
- $link .= sprintf('%s="%s" ', $itemKey, ($this->_autoEscape) ? $this->_escape($attributes[$itemKey]) : $attributes[$itemKey]);
- }
- }
- }
- if ($this->view instanceof Zend_View_Abstract) {
- $link .= ($this->view->doctype()->isXhtml()) ? '/>' : '>';
- } else {
- $link .= '/>';
- }
- if (($link == '<link />') || ($link == '<link >')) {
- return '';
- }
- if (isset($attributes['conditionalStylesheet'])
- && !empty($attributes['conditionalStylesheet'])
- && is_string($attributes['conditionalStylesheet']))
- {
- if (str_replace(' ', '', $attributes['conditionalStylesheet']) === '!IE') {
- $link = '<!-->' . $link . '<!--';
- }
- $link = '<!--[if ' . $attributes['conditionalStylesheet'] . ']>' . $link . '<![endif]-->';
- }
- return $link;
- }
- /**
- * Render link elements as string
- *
- * @param string|int $indent
- * @return string
- */
- public function toString($indent = null)
- {
- $indent = (null !== $indent)
- ? $this->getWhitespace($indent)
- : $this->getIndent();
- $items = array();
- $this->getContainer()->ksort();
- foreach ($this as $item) {
- $items[] = $this->itemToString($item);
- }
- return $indent . implode($this->_escape($this->getSeparator()) . $indent, $items);
- }
- /**
- * Create data item for stack
- *
- * @param array $attributes
- * @return stdClass
- */
- public function createData(array $attributes)
- {
- $data = (object) $attributes;
- return $data;
- }
- /**
- * Create item for stylesheet link item
- *
- * @param array $args
- * @return stdClass|false Returns fals if stylesheet is a duplicate
- */
- public function createDataStylesheet(array $args)
- {
- $rel = 'stylesheet';
- $type = 'text/css';
- $media = 'screen';
- $conditionalStylesheet = false;
- $href = array_shift($args);
- if ($this->_isDuplicateStylesheet($href)) {
- return false;
- }
- if (0 < count($args)) {
- $media = array_shift($args);
- if(is_array($media)) {
- $media = implode(',', $media);
- } else {
- $media = (string) $media;
- }
- }
- if (0 < count($args)) {
- $conditionalStylesheet = array_shift($args);
- if(!empty($conditionalStylesheet) && is_string($conditionalStylesheet)) {
- $conditionalStylesheet = (string) $conditionalStylesheet;
- } else {
- $conditionalStylesheet = null;
- }
- }
- if(0 < count($args) && is_array($args[0])) {
- $extras = array_shift($args);
- $extras = (array) $extras;
- }
- $attributes = compact('rel', 'type', 'href', 'media', 'conditionalStylesheet', 'extras');
- return $this->createData($this->_applyExtras($attributes));
- }
- /**
- * Is the linked stylesheet a duplicate?
- *
- * @param string $uri
- * @return bool
- */
- protected function _isDuplicateStylesheet($uri)
- {
- foreach ($this->getContainer() as $item) {
- if (($item->rel == 'stylesheet') && ($item->href == $uri)) {
- return true;
- }
- }
- return false;
- }
- /**
- * Create item for alternate link item
- *
- * @param array $args
- * @return stdClass
- */
- public function createDataAlternate(array $args)
- {
- if (3 > count($args)) {
- require_once 'Zend/View/Exception.php';
- $e = new Zend_View_Exception(sprintf('Alternate tags require 3 arguments; %s provided', count($args)));
- $e->setView($this->view);
- throw $e;
- }
- $rel = 'alternate';
- $href = array_shift($args);
- $type = array_shift($args);
- $title = array_shift($args);
- if(0 < count($args) && is_array($args[0])) {
- $extras = array_shift($args);
- $extras = (array) $extras;
- if(isset($extras['media']) && is_array($extras['media'])) {
- $extras['media'] = implode(',', $extras['media']);
- }
- }
- $href = (string) $href;
- $type = (string) $type;
- $title = (string) $title;
- $attributes = compact('rel', 'href', 'type', 'title', 'extras');
- return $this->createData($this->_applyExtras($attributes));
- }
- /**
- * Apply any overrides specified in the 'extras' array
- * @param array $attributes
- * @return array
- */
- protected function _applyExtras($attributes)
- {
- if (isset($attributes['extras'])) {
- foreach ($attributes['extras'] as $eKey=>$eVal) {
- if (isset($attributes[$eKey])) {
- $attributes[$eKey] = $eVal;
- unset($attributes['extras'][$eKey]);
- }
- }
- }
- return $attributes;
- }
- }
|