Source of file HtmlTag.php
Size: 6,061 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Tag/Cloud/Decorator/HtmlTag.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\Tag\Cloud\Decorator; use Zend\Tag\Cloud\Decorator\Exception\InvalidArgumentException; use Zend\Tag\ItemList; /** * Simple HTML decorator for tags */ class HtmlTag extends AbstractTag { /** * List of tags which get assigned to the inner element instead of * font-sizes. * * @var array */ protected $classList = null; /** * Unit for the fontsize * * @var string */ protected $fontSizeUnit = 'px'; /** * Allowed fontsize units * * @var array */ protected $allowedFontSizeUnits = array('em', 'ex', 'px', 'in', 'cm', 'mm', 'pt', 'pc', '%'); /** * List of HTML tags * * @var array */ protected $htmlTags = array( 'li' ); /** * Maximum fontsize * * @var int */ protected $maxFontSize = 20; /** * Minimum fontsize * * @var int */ protected $minFontSize = 10; /** * Set a list of classes to use instead of fontsizes * * @param array $classList * @throws InvalidArgumentException When the classlist is empty * @throws InvalidArgumentException When the classlist contains an invalid classname * @return HTMLTag */ public function setClassList(array $classList = null) { if (is_array($classList)) { if (count($classList) === 0) { throw new InvalidArgumentException('Classlist is empty'); } foreach ($classList as $class) { if (!is_string($class)) { throw new InvalidArgumentException('Classlist contains an invalid classname'); } } } $this->classList = $classList; return $this; } /** * Get class list * * @return array */ public function getClassList() { return $this->classList; } /** * Set the font size unit * * Possible values are: em, ex, px, in, cm, mm, pt, pc and % * * @param string $fontSizeUnit * @throws InvalidArgumentException When an invalid fontsize unit is specified * @return HTMLTag */ public function setFontSizeUnit($fontSizeUnit) { if (!in_array($fontSizeUnit, $this->allowedFontSizeUnits)) { throw new InvalidArgumentException('Invalid fontsize unit specified'); } $this->fontSizeUnit = (string) $fontSizeUnit; $this->setClassList(null); return $this; } /** * Retrieve font size unit * * @return string */ public function getFontSizeUnit() { return $this->fontSizeUnit; } /** * Set the HTML tags surrounding the <a> element * * @param array $htmlTags * @return HTMLTag */ public function setHTMLTags(array $htmlTags) { $this->htmlTags = $htmlTags; return $this; } /** * Get HTML tags map * * @return array */ public function getHTMLTags() { return $this->htmlTags; } /** * Set maximum font size * * @param int $maxFontSize * @throws InvalidArgumentException When fontsize is not numeric * @return HTMLTag */ public function setMaxFontSize($maxFontSize) { if (!is_numeric($maxFontSize)) { throw new InvalidArgumentException('Fontsize must be numeric'); } $this->maxFontSize = (int) $maxFontSize; $this->setClassList(null); return $this; } /** * Retrieve maximum font size * * @return int */ public function getMaxFontSize() { return $this->maxFontSize; } /** * Set minimum font size * * @param int $minFontSize * @throws InvalidArgumentException When fontsize is not numeric * @return HTMLTag */ public function setMinFontSize($minFontSize) { if (!is_numeric($minFontSize)) { throw new InvalidArgumentException('Fontsize must be numeric'); } $this->minFontSize = (int) $minFontSize; $this->setClassList(null); return $this; } /** * Retrieve minimum font size * * @return int */ public function getMinFontSize() { return $this->minFontSize; } /** * Defined by Tag * * @param ItemList $tags * @throws InvalidArgumentException * @return array */ public function render($tags) { if (!$tags instanceof ItemList) { throw new InvalidArgumentException(sprintf( 'HtmlTag::render() expects a Zend\Tag\ItemList argument; received "%s"', (is_object($tags) ? get_class($tags) : gettype($tags)) )); } if (null === ($weightValues = $this->getClassList())) { $weightValues = range($this->getMinFontSize(), $this->getMaxFontSize()); } $tags->spreadWeightValues($weightValues); $result = array(); $escaper = $this->getEscaper(); foreach ($tags as $tag) { if (null === ($classList = $this->getClassList())) { $attribute = sprintf('style="font-size: %d%s;"', $tag->getParam('weightValue'), $this->getFontSizeUnit()); } else { $attribute = sprintf('class="%s"', $escaper->escapeHtmlAttr($tag->getParam('weightValue'))); } $tagHTML = sprintf('<a href="%s" %s>%s</a>', $escaper->escapeHtml($tag->getParam('url')), $attribute, $escaper->escapeHtml($tag->getTitle())); $tagHTML = $this->wrapTag($tagHTML); $result[] = $tagHTML; } return $result; } } |