Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Tag / Cloud / Decorator / AbstractDecorator.php
blobbc4c38292ac2c3cfc80691079396c4ad8e0758ff
1 <?php
2 /**
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
8 */
10 namespace Zend\Tag\Cloud\Decorator;
12 use Traversable;
13 use Zend\Escaper\Escaper;
14 use Zend\Stdlib\ArrayUtils;
15 use Zend\Tag\Cloud\Decorator\DecoratorInterface as Decorator;
16 use Zend\Tag\Exception;
18 /**
19 * Abstract class for decorators
21 abstract class AbstractDecorator implements Decorator
23 /**
24 * @var string Encoding to use
26 protected $encoding = 'UTF-8';
28 /**
29 * @var Escaper
31 protected $escaper;
33 /**
34 * Option keys to skip when calling setOptions()
36 * @var array
38 protected $skipOptions = array(
39 'options',
40 'config',
43 /**
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);
58 /**
59 * Set options from array
61 * @param array $options Configuration for the decorator
62 * @return AbstractTag
64 public function setOptions(array $options)
66 foreach ($options as $key => $value) {
67 if (in_array(strtolower($key), $this->skipOptions)) {
68 continue;
71 $method = 'set' . $key;
72 if (method_exists($this, $method)) {
73 $this->$method($value);
77 return $this;
80 /**
81 * Get encoding
83 * @return string
85 public function getEncoding()
87 return $this->encoding;
90 /**
91 * Set encoding
93 * @param string
94 * @return HTMLCloud
96 public function setEncoding($value)
98 $this->encoding = (string) $value;
99 return $this;
103 * Set Escaper instance
105 * @param Escaper $escaper
106 * @return HtmlCloud
108 public function setEscaper($escaper)
110 $this->escaper = $escaper;
111 return $this;
115 * Retrieve Escaper instance
117 * If none registered, instantiates and registers one using current encoding.
119 * @return Escaper
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',
140 __METHOD__,
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',
157 __METHOD__,
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)) {
168 $attributes = '';
169 $htmlTag = $key;
170 $this->validateElementName($htmlTag);
172 foreach ($data as $param => $value) {
173 $this->validateAttributeName($param);
174 $attributes .= ' ' . $param . '="' . $escaper->escapeHtmlAttr($value) . '"';
176 } else {
177 $attributes = '';
178 $htmlTag = $data;
179 $this->validateElementName($htmlTag);
182 $html = sprintf('<%1$s%3$s>%2$s</%1$s>', $htmlTag, $html, $attributes);
184 return $html;