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.
18 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
19 * @version $Id: HeadStyle.php 16971 2009-07-22 18:05:45Z mikaelkael $
20 * @license http://framework.zend.com/license/new-bsd New BSD License
23 /** Zend_View_Helper_Placeholder_Container_Standalone */
24 require_once 'Zend/View/Helper/Placeholder/Container/Standalone.php';
27 * Helper for setting and retrieving stylesheets
29 * @uses Zend_View_Helper_Placeholder_Container_Standalone
32 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
33 * @license http://framework.zend.com/license/new-bsd New BSD License
35 class Zend_View_Helper_HeadStyle
extends Zend_View_Helper_Placeholder_Container_Standalone
38 * Registry key for placeholder
41 protected $_regKey = 'Zend_View_Helper_HeadStyle';
44 * Allowed optional attributes
47 protected $_optionalAttributes = array('lang', 'title', 'media', 'dir');
53 protected $_mediaTypes = array(
54 'all', 'aural', 'braille', 'handheld', 'print',
55 'projection', 'screen', 'tty', 'tv'
59 * Capture type and/or attributes (used for hinting during capture)
62 protected $_captureAttrs = null;
68 protected $_captureLock;
71 * Capture type (append, prepend, set)
74 protected $_captureType;
79 * Set separator to PHP_EOL.
83 public function __construct()
85 parent
::__construct();
86 $this->setSeparator(PHP_EOL
);
90 * Return headStyle object
92 * Returns headStyle helper object; optionally, allows specifying
94 * @param string $content Stylesheet contents
95 * @param string $placement Append, prepend, or set
96 * @param string|array $attributes Optional attributes to utilize
97 * @return Zend_View_Helper_HeadStyle
99 public function headStyle($content = null, $placement = 'APPEND', $attributes = array())
101 if ((null !== $content) && is_string($content)) {
102 switch (strtoupper($placement)) {
104 $action = 'setStyle';
107 $action = 'prependStyle';
111 $action = 'appendStyle';
114 $this->$action($content, $attributes);
121 * Overload method calls
123 * Allows the following method calls:
124 * - appendStyle($content, $attributes = array())
125 * - offsetSetStyle($index, $content, $attributes = array())
126 * - prependStyle($content, $attributes = array())
127 * - setStyle($content, $attributes = array())
129 * @param string $method
132 * @throws Zend_View_Exception When no $content provided or invalid method
134 public function __call($method, $args)
136 if (preg_match('/^(?P<action>set|(ap|pre)pend|offsetSet)(Style)$/', $method, $matches)) {
138 $argc = count($args);
139 $action = $matches['action'];
141 if ('offsetSet' == $action) {
143 $index = array_shift($args);
149 require_once 'Zend/View/Exception.php';
150 throw new Zend_View_Exception(sprintf('Method "%s" requires minimally content for the stylesheet', $method));
155 if (isset($args[1])) {
156 $attrs = (array) $args[1];
159 $item = $this->createData($content, $attrs);
161 if ('offsetSet' == $action) {
162 $this->offsetSet($index, $item);
164 $this->$action($item);
170 return parent
::__call($method, $args);
174 * Determine if a value is a valid style tag
176 * @param mixed $value
177 * @param string $method
180 protected function _isValid($value)
182 if ((!$value instanceof stdClass
)
183 ||
!isset($value->content
)
184 ||
!isset($value->attributes
))
193 * Override append to enforce style creation
195 * @param mixed $value
198 public function append($value)
200 if (!$this->_isValid($value)) {
201 require_once 'Zend/View/Exception.php';
202 throw new Zend_View_Exception('Invalid value passed to append; please use appendStyle()');
205 return $this->getContainer()->append($value);
209 * Override offsetSet to enforce style creation
211 * @param string|int $index
212 * @param mixed $value
215 public function offsetSet($index, $value)
217 if (!$this->_isValid($value)) {
218 require_once 'Zend/View/Exception.php';
219 throw new Zend_View_Exception('Invalid value passed to offsetSet; please use offsetSetStyle()');
222 return $this->getContainer()->offsetSet($index, $value);
226 * Override prepend to enforce style creation
228 * @param mixed $value
231 public function prepend($value)
233 if (!$this->_isValid($value)) {
234 require_once 'Zend/View/Exception.php';
235 throw new Zend_View_Exception('Invalid value passed to prepend; please use prependStyle()');
238 return $this->getContainer()->prepend($value);
242 * Override set to enforce style creation
244 * @param mixed $value
247 public function set($value)
249 if (!$this->_isValid($value)) {
250 require_once 'Zend/View/Exception.php';
251 throw new Zend_View_Exception('Invalid value passed to set; please use setStyle()');
254 return $this->getContainer()->set($value);
258 * Start capture action
260 * @param mixed $captureType
261 * @param string $typeOrAttrs
264 public function captureStart($type = Zend_View_Helper_Placeholder_Container_Abstract
::APPEND
, $attrs = null)
266 if ($this->_captureLock
) {
267 require_once 'Zend/View/Helper/Placeholder/Container/Exception.php';
268 throw new Zend_View_Helper_Placeholder_Container_Exception('Cannot nest headStyle captures');
271 $this->_captureLock
= true;
272 $this->_captureAttrs
= $attrs;
273 $this->_captureType
= $type;
278 * End capture action and store
282 public function captureEnd()
284 $content = ob_get_clean();
285 $attrs = $this->_captureAttrs
;
286 $this->_captureAttrs
= null;
287 $this->_captureLock
= false;
289 switch ($this->_captureType
) {
290 case Zend_View_Helper_Placeholder_Container_Abstract
::SET
:
291 $this->setStyle($content, $attrs);
293 case Zend_View_Helper_Placeholder_Container_Abstract
::PREPEND
:
294 $this->prependStyle($content, $attrs);
296 case Zend_View_Helper_Placeholder_Container_Abstract
::APPEND
:
298 $this->appendStyle($content, $attrs);
304 * Convert content and attributes into valid style tag
306 * @param stdClass $item Item to render
307 * @param string $indent Indentation to use
310 public function itemToString(stdClass
$item, $indent)
313 if (!empty($item->attributes
)) {
314 foreach ($item->attributes
as $key => $value) {
315 if (!in_array($key, $this->_optionalAttributes
)) {
318 if ('media' == $key) {
319 if(false === strpos($value, ',')) {
320 if (!in_array($value, $this->_mediaTypes
)) {
324 $media_types = explode(',', $value);
326 foreach($media_types as $type) {
327 if (!in_array($type, $this->_mediaTypes
)) {
330 $value .= $type .',';
332 $value = substr($value, 0, -1);
335 $attrString .= sprintf(' %s="%s"', $key, htmlspecialchars($value));
339 $html = '<style type="text/css"' . $attrString . '>' . PHP_EOL
340 . $indent . '<!--' . PHP_EOL
. $indent . $item->content
. PHP_EOL
. $indent . '-->' . PHP_EOL
343 if (isset($item->attributes
['conditional'])
344 && !empty($item->attributes
['conditional'])
345 && is_string($item->attributes
['conditional']))
347 $html = '<!--[if ' . $item->attributes
['conditional'] . ']> ' . $html . '<![endif]-->';
354 * Create string representation of placeholder
356 * @param string|int $indent
359 public function toString($indent = null)
361 $indent = (null !== $indent)
362 ?
$this->getWhitespace($indent)
363 : $this->getIndent();
366 $this->getContainer()->ksort();
367 foreach ($this as $item) {
368 if (!$this->_isValid($item)) {
371 $items[] = $this->itemToString($item, $indent);
374 $return = $indent . implode($this->getSeparator() . $indent, $items);
375 $return = preg_replace("/(\r\n?|\n)/", '$1' . $indent, $return);
380 * Create data item for use in stack
382 * @param string $content
383 * @param array $attributes
386 public function createData($content, array $attributes)
388 if (!isset($attributes['media'])) {
389 $attributes['media'] = 'screen';
390 } else if(is_array($attributes['media'])) {
391 $attributes['media'] = implode(',', $attributes['media']);
394 $data = new stdClass();
395 $data->content
= $content;
396 $data->attributes
= $attributes;