3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Tag\Cloud\Decorator
;
13 use Zend\Escaper\Escaper
;
14 use Zend\Stdlib\ArrayUtils
;
15 use Zend\Tag\Cloud\Decorator\DecoratorInterface
as Decorator
;
16 use Zend\Tag\Exception
;
19 * Abstract class for decorators
21 abstract class AbstractDecorator
implements Decorator
24 * @var string Encoding to use
26 protected $encoding = 'UTF-8';
34 * Option keys to skip when calling setOptions()
38 protected $skipOptions = array(
44 * Create a new decorator with options
46 * @param array|Traversable $options
48 public function __construct($options = null)
50 if ($options instanceof Traversable
) {
51 $options = ArrayUtils
::iteratorToArray($options);
53 if (is_array($options)) {
54 $this->setOptions($options);
59 * Set options from array
61 * @param array $options Configuration for the decorator
64 public function setOptions(array $options)
66 foreach ($options as $key => $value) {
67 if (in_array(strtolower($key), $this->skipOptions
)) {
71 $method = 'set' . $key;
72 if (method_exists($this, $method)) {
73 $this->$method($value);
85 public function getEncoding()
87 return $this->encoding
;
96 public function setEncoding($value)
98 $this->encoding
= (string) $value;
103 * Set Escaper instance
105 * @param Escaper $escaper
108 public function setEscaper($escaper)
110 $this->escaper
= $escaper;
115 * Retrieve Escaper instance
117 * If none registered, instantiates and registers one using current encoding.
121 public function getEscaper()
123 if (null === $this->escaper
) {
124 $this->setEscaper(new Escaper($this->getEncoding()));
126 return $this->escaper
;
130 * Validate an HTML element name
132 * @param string $name
133 * @throws Exception\InvalidElementNameException
135 protected function validateElementName($name)
137 if (!preg_match('/^[a-z0-9]+$/i', $name)) {
138 throw new Exception\
InvalidElementNameException(sprintf(
139 '%s: Invalid element name "%s" provided; please provide valid HTML element names',
141 $this->getEscaper()->escapeHtml($name)
147 * Validate an HTML attribute name
149 * @param string $name
150 * @throws Exception\InvalidAttributeNameException
152 protected function validateAttributeName($name)
154 if (!preg_match('/^[a-z_:][-a-z0-9_:.]*$/i', $name)) {
155 throw new Exception\
InvalidAttributeNameException(sprintf(
156 '%s: Invalid HTML attribute name "%s" provided; please provide valid HTML attribute names',
158 $this->getEscaper()->escapeHtml($name)
163 protected function wrapTag($html)
165 $escaper = $this->getEscaper();
166 foreach ($this->getHTMLTags() as $key => $data) {
167 if (is_array($data)) {
170 $this->validateElementName($htmlTag);
172 foreach ($data as $param => $value) {
173 $this->validateAttributeName($param);
174 $attributes .= ' ' . $param . '="' . $escaper->escapeHtmlAttr($value) . '"';
179 $this->validateElementName($htmlTag);
182 $html = sprintf('<%1$s%3$s>%2$s</%1$s>', $htmlTag, $html, $attributes);