PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/View/Helper/HeadStyle.php

https://github.com/mfairchild365/zf2
PHP | 426 lines | 229 code | 43 blank | 154 comment | 42 complexity | 5edc056ca3dbeb36ebf721a4825cf275 MD5 | raw file
  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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace Zend\View\Helper;
  25. use Zend\View;
  26. /**
  27. * Helper for setting and retrieving stylesheets
  28. *
  29. * @uses stdClass
  30. * @uses \Zend\View\Helper\Placeholder\Container\AbstractContainer
  31. * @uses \Zend\View\Helper\Placeholder\Container\Exception
  32. * @uses \Zend\View\Helper\Placeholder\Container\Standalone
  33. * @uses \Zend\View\Exception
  34. * @package Zend_View
  35. * @subpackage Helper
  36. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. class HeadStyle extends Placeholder\Container\Standalone
  40. {
  41. /**
  42. * Registry key for placeholder
  43. * @var string
  44. */
  45. protected $_regKey = 'Zend_View_Helper_HeadStyle';
  46. /**
  47. * Allowed optional attributes
  48. * @var array
  49. */
  50. protected $_optionalAttributes = array('lang', 'title', 'media', 'dir');
  51. /**
  52. * Allowed media types
  53. * @var array
  54. */
  55. protected $_mediaTypes = array(
  56. 'all', 'aural', 'braille', 'handheld', 'print',
  57. 'projection', 'screen', 'tty', 'tv'
  58. );
  59. /**
  60. * Capture type and/or attributes (used for hinting during capture)
  61. * @var string
  62. */
  63. protected $_captureAttrs = null;
  64. /**
  65. * Capture lock
  66. * @var bool
  67. */
  68. protected $_captureLock;
  69. /**
  70. * Capture type (append, prepend, set)
  71. * @var string
  72. */
  73. protected $_captureType;
  74. /**
  75. * Constructor
  76. *
  77. * Set separator to PHP_EOL.
  78. *
  79. * @return void
  80. */
  81. public function __construct()
  82. {
  83. parent::__construct();
  84. $this->setSeparator(PHP_EOL);
  85. }
  86. /**
  87. * Return headStyle object
  88. *
  89. * Returns headStyle helper object; optionally, allows specifying
  90. *
  91. * @param string $content Stylesheet contents
  92. * @param string $placement Append, prepend, or set
  93. * @param string|array $attributes Optional attributes to utilize
  94. * @return \Zend\View\Helper\HeadStyle
  95. */
  96. public function direct($content = null, $placement = 'APPEND', $attributes = array())
  97. {
  98. if ((null !== $content) && is_string($content)) {
  99. switch (strtoupper($placement)) {
  100. case 'SET':
  101. $action = 'setStyle';
  102. break;
  103. case 'PREPEND':
  104. $action = 'prependStyle';
  105. break;
  106. case 'APPEND':
  107. default:
  108. $action = 'appendStyle';
  109. break;
  110. }
  111. $this->$action($content, $attributes);
  112. }
  113. return $this;
  114. }
  115. /**
  116. * Overload method calls
  117. *
  118. * Allows the following method calls:
  119. * - appendStyle($content, $attributes = array())
  120. * - offsetSetStyle($index, $content, $attributes = array())
  121. * - prependStyle($content, $attributes = array())
  122. * - setStyle($content, $attributes = array())
  123. *
  124. * @param string $method
  125. * @param array $args
  126. * @return void
  127. * @throws \Zend\View\Exception When no $content provided or invalid method
  128. */
  129. public function __call($method, $args)
  130. {
  131. if (preg_match('/^(?P<action>set|(ap|pre)pend|offsetSet)(Style)$/', $method, $matches)) {
  132. $index = null;
  133. $argc = count($args);
  134. $action = $matches['action'];
  135. if ('offsetSet' == $action) {
  136. if (0 < $argc) {
  137. $index = array_shift($args);
  138. --$argc;
  139. }
  140. }
  141. if (1 > $argc) {
  142. $e = new View\Exception(sprintf('Method "%s" requires minimally content for the stylesheet', $method));
  143. $e->setView($this->view);
  144. throw $e;
  145. }
  146. $content = $args[0];
  147. $attrs = array();
  148. if (isset($args[1])) {
  149. $attrs = (array) $args[1];
  150. }
  151. $item = $this->createData($content, $attrs);
  152. if ('offsetSet' == $action) {
  153. $this->offsetSet($index, $item);
  154. } else {
  155. $this->$action($item);
  156. }
  157. return $this;
  158. }
  159. return parent::__call($method, $args);
  160. }
  161. /**
  162. * Determine if a value is a valid style tag
  163. *
  164. * @param mixed $value
  165. * @param string $method
  166. * @return boolean
  167. */
  168. protected function _isValid($value)
  169. {
  170. if ((!$value instanceof \stdClass)
  171. || !isset($value->content)
  172. || !isset($value->attributes))
  173. {
  174. return false;
  175. }
  176. return true;
  177. }
  178. /**
  179. * Override append to enforce style creation
  180. *
  181. * @param mixed $value
  182. * @return void
  183. */
  184. public function append($value)
  185. {
  186. if (!$this->_isValid($value)) {
  187. $e = new View\Exception('Invalid value passed to append; please use appendStyle()');
  188. $e->setView($this->view);
  189. throw $e;
  190. }
  191. return $this->getContainer()->append($value);
  192. }
  193. /**
  194. * Override offsetSet to enforce style creation
  195. *
  196. * @param string|int $index
  197. * @param mixed $value
  198. * @return void
  199. */
  200. public function offsetSet($index, $value)
  201. {
  202. if (!$this->_isValid($value)) {
  203. $e = new View\Exception('Invalid value passed to offsetSet; please use offsetSetStyle()');
  204. $e->setView($this->view);
  205. throw $e;
  206. }
  207. return $this->getContainer()->offsetSet($index, $value);
  208. }
  209. /**
  210. * Override prepend to enforce style creation
  211. *
  212. * @param mixed $value
  213. * @return void
  214. */
  215. public function prepend($value)
  216. {
  217. if (!$this->_isValid($value)) {
  218. $e = new View\Exception('Invalid value passed to prepend; please use prependStyle()');
  219. $e->setView($this->view);
  220. throw $e;
  221. }
  222. return $this->getContainer()->prepend($value);
  223. }
  224. /**
  225. * Override set to enforce style creation
  226. *
  227. * @param mixed $value
  228. * @return void
  229. */
  230. public function set($value)
  231. {
  232. if (!$this->_isValid($value)) {
  233. $e = new View\Exception('Invalid value passed to set; please use setStyle()');
  234. $e->setView($this->view);
  235. throw $e;
  236. }
  237. return $this->getContainer()->set($value);
  238. }
  239. /**
  240. * Start capture action
  241. *
  242. * @param mixed $captureType
  243. * @param string $typeOrAttrs
  244. * @return void
  245. */
  246. public function captureStart($type = Placeholder\Container\AbstractContainer::APPEND, $attrs = null)
  247. {
  248. if ($this->_captureLock) {
  249. $e = new Placeholder\Container\Exception('Cannot nest headStyle captures');
  250. $e->setView($this->view);
  251. throw $e;
  252. }
  253. $this->_captureLock = true;
  254. $this->_captureAttrs = $attrs;
  255. $this->_captureType = $type;
  256. ob_start();
  257. }
  258. /**
  259. * End capture action and store
  260. *
  261. * @return void
  262. */
  263. public function captureEnd()
  264. {
  265. $content = ob_get_clean();
  266. $attrs = $this->_captureAttrs;
  267. $this->_captureAttrs = null;
  268. $this->_captureLock = false;
  269. switch ($this->_captureType) {
  270. case Placeholder\Container\AbstractContainer::SET:
  271. $this->setStyle($content, $attrs);
  272. break;
  273. case Placeholder\Container\AbstractContainer::PREPEND:
  274. $this->prependStyle($content, $attrs);
  275. break;
  276. case Placeholder\Container\AbstractContainer::APPEND:
  277. default:
  278. $this->appendStyle($content, $attrs);
  279. break;
  280. }
  281. }
  282. /**
  283. * Convert content and attributes into valid style tag
  284. *
  285. * @param stdClass $item Item to render
  286. * @param string $indent Indentation to use
  287. * @return string
  288. */
  289. public function itemToString(\stdClass $item, $indent)
  290. {
  291. $attrString = '';
  292. if (!empty($item->attributes)) {
  293. $enc = 'UTF-8';
  294. if ($this->view instanceof View\Renderer
  295. && method_exists($this->view, 'getEncoding')
  296. ) {
  297. $enc = $this->view->getEncoding();
  298. }
  299. foreach ($item->attributes as $key => $value) {
  300. if (!in_array($key, $this->_optionalAttributes)) {
  301. continue;
  302. }
  303. if ('media' == $key) {
  304. if(false === strpos($value, ',')) {
  305. if (!in_array($value, $this->_mediaTypes)) {
  306. continue;
  307. }
  308. } else {
  309. $media_types = explode(',', $value);
  310. $value = '';
  311. foreach($media_types as $type) {
  312. $type = trim($type);
  313. if (!in_array($type, $this->_mediaTypes)) {
  314. continue;
  315. }
  316. $value .= $type .',';
  317. }
  318. $value = substr($value, 0, -1);
  319. }
  320. }
  321. $attrString .= sprintf(' %s="%s"', $key, htmlspecialchars($value, ENT_COMPAT, $enc));
  322. }
  323. }
  324. $escapeStart = $indent . '<!--'. PHP_EOL;
  325. $escapeEnd = $indent . '-->'. PHP_EOL;
  326. if (isset($item->attributes['conditional'])
  327. && !empty($item->attributes['conditional'])
  328. && is_string($item->attributes['conditional'])
  329. ) {
  330. $escapeStart = null;
  331. $escapeEnd = null;
  332. }
  333. $html = '<style type="text/css"' . $attrString . '>' . PHP_EOL
  334. . $escapeStart . $indent . $item->content . PHP_EOL . $escapeEnd
  335. . '</style>';
  336. if (null == $escapeStart && null == $escapeEnd) {
  337. $html = '<!--[if ' . $item->attributes['conditional'] . ']> ' . $html . '<![endif]-->';
  338. }
  339. return $html;
  340. }
  341. /**
  342. * Create string representation of placeholder
  343. *
  344. * @param string|int $indent
  345. * @return string
  346. */
  347. public function toString($indent = null)
  348. {
  349. $indent = (null !== $indent)
  350. ? $this->getWhitespace($indent)
  351. : $this->getIndent();
  352. $items = array();
  353. $this->getContainer()->ksort();
  354. foreach ($this as $item) {
  355. if (!$this->_isValid($item)) {
  356. continue;
  357. }
  358. $items[] = $this->itemToString($item, $indent);
  359. }
  360. $return = $indent . implode($this->getSeparator() . $indent, $items);
  361. $return = preg_replace("/(\r\n?|\n)/", '$1' . $indent, $return);
  362. return $return;
  363. }
  364. /**
  365. * Create data item for use in stack
  366. *
  367. * @param string $content
  368. * @param array $attributes
  369. * @return stdClass
  370. */
  371. public function createData($content, array $attributes)
  372. {
  373. if (!isset($attributes['media'])) {
  374. $attributes['media'] = 'screen';
  375. } else if(is_array($attributes['media'])) {
  376. $attributes['media'] = implode(',', $attributes['media']);
  377. }
  378. $data = new \stdClass();
  379. $data->content = $content;
  380. $data->attributes = $attributes;
  381. return $data;
  382. }
  383. }