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
;
12 use Zend\Tag\Cloud\Decorator\Exception\InvalidArgumentException
;
13 use Zend\Tag\ItemList
;
16 * Simple HTML decorator for tags
18 class HtmlTag
extends AbstractTag
21 * List of tags which get assigned to the inner element instead of
26 protected $classList = null;
29 * Unit for the fontsize
33 protected $fontSizeUnit = 'px';
36 * Allowed fontsize units
40 protected $allowedFontSizeUnits = array('em', 'ex', 'px', 'in', 'cm', 'mm', 'pt', 'pc', '%');
47 protected $htmlTags = array(
56 protected $maxFontSize = 20;
63 protected $minFontSize = 10;
66 * Set a list of classes to use instead of fontsizes
68 * @param array $classList
69 * @throws InvalidArgumentException When the classlist is empty
70 * @throws InvalidArgumentException When the classlist contains an invalid classname
73 public function setClassList(array $classList = null)
75 if (is_array($classList)) {
76 if (count($classList) === 0) {
77 throw new InvalidArgumentException('Classlist is empty');
80 foreach ($classList as $class) {
81 if (!is_string($class)) {
82 throw new InvalidArgumentException('Classlist contains an invalid classname');
87 $this->classList
= $classList;
96 public function getClassList()
98 return $this->classList
;
102 * Set the font size unit
104 * Possible values are: em, ex, px, in, cm, mm, pt, pc and %
106 * @param string $fontSizeUnit
107 * @throws InvalidArgumentException When an invalid fontsize unit is specified
110 public function setFontSizeUnit($fontSizeUnit)
112 if (!in_array($fontSizeUnit, $this->allowedFontSizeUnits
)) {
113 throw new InvalidArgumentException('Invalid fontsize unit specified');
116 $this->fontSizeUnit
= (string) $fontSizeUnit;
117 $this->setClassList(null);
122 * Retrieve font size unit
126 public function getFontSizeUnit()
128 return $this->fontSizeUnit
;
131 * Set the HTML tags surrounding the <a> element
133 * @param array $htmlTags
136 public function setHTMLTags(array $htmlTags)
138 $this->htmlTags
= $htmlTags;
147 public function getHTMLTags()
149 return $this->htmlTags
;
153 * Set maximum font size
155 * @param int $maxFontSize
156 * @throws InvalidArgumentException When fontsize is not numeric
159 public function setMaxFontSize($maxFontSize)
161 if (!is_numeric($maxFontSize)) {
162 throw new InvalidArgumentException('Fontsize must be numeric');
165 $this->maxFontSize
= (int) $maxFontSize;
166 $this->setClassList(null);
171 * Retrieve maximum font size
175 public function getMaxFontSize()
177 return $this->maxFontSize
;
181 * Set minimum font size
183 * @param int $minFontSize
184 * @throws InvalidArgumentException When fontsize is not numeric
187 public function setMinFontSize($minFontSize)
189 if (!is_numeric($minFontSize)) {
190 throw new InvalidArgumentException('Fontsize must be numeric');
193 $this->minFontSize
= (int) $minFontSize;
194 $this->setClassList(null);
199 * Retrieve minimum font size
203 public function getMinFontSize()
205 return $this->minFontSize
;
211 * @param ItemList $tags
212 * @throws InvalidArgumentException
215 public function render($tags)
217 if (!$tags instanceof ItemList
) {
218 throw new InvalidArgumentException(sprintf(
219 'HtmlTag::render() expects a Zend\Tag\ItemList argument; received "%s"',
220 (is_object($tags) ?
get_class($tags) : gettype($tags))
223 if (null === ($weightValues = $this->getClassList())) {
224 $weightValues = range($this->getMinFontSize(), $this->getMaxFontSize());
227 $tags->spreadWeightValues($weightValues);
231 $escaper = $this->getEscaper();
232 foreach ($tags as $tag) {
233 if (null === ($classList = $this->getClassList())) {
234 $attribute = sprintf('style="font-size: %d%s;"', $tag->getParam('weightValue'), $this->getFontSizeUnit());
236 $attribute = sprintf('class="%s"', $escaper->escapeHtmlAttr($tag->getParam('weightValue')));
239 $tagHTML = sprintf('<a href="%s" %s>%s</a>', $escaper->escapeHtml($tag->getParam('url')), $attribute, $escaper->escapeHtml($tag->getTitle()));
240 $tagHTML = $this->wrapTag($tagHTML);
241 $result[] = $tagHTML;