/library/Zend/Tag/Cloud/Decorator/HtmlTag.php

https://github.com/leerbag/zf2 · PHP · 311 lines · 130 code · 35 blank · 146 comment · 12 complexity · ece6b34655110eb8484fd343a05f339d 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_Tag
  17. * @subpackage Cloud
  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\Tag\Cloud\Decorator;
  25. use Zend\Tag\Cloud\Decorator\Exception\InvalidArgumentException,
  26. Zend\Tag\ItemList;
  27. /**
  28. * Simple HTML decorator for tags
  29. *
  30. * @uses \Zend\Tag\Cloud\Decorator\Exception\InvalidArgumentException
  31. * @uses \Zend\Tag\Cloud\Decorator\Tag
  32. * @category Zend
  33. * @package Zend_Tag
  34. * @uses \Zend\Tag\Cloud\Decorator\Tag
  35. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. */
  38. class HtmlTag extends Tag
  39. {
  40. /**
  41. * List of tags which get assigned to the inner element instead of
  42. * font-sizes.
  43. *
  44. * @var array
  45. */
  46. protected $_classList = null;
  47. /**
  48. * @var string Encoding to utilize
  49. */
  50. protected $_encoding = 'UTF-8';
  51. /**
  52. * Unit for the fontsize
  53. *
  54. * @var string
  55. */
  56. protected $_fontSizeUnit = 'px';
  57. /**
  58. * Allowed fontsize units
  59. *
  60. * @var array
  61. */
  62. protected $_alloweFontSizeUnits = array('em', 'ex', 'px', 'in', 'cm', 'mm', 'pt', 'pc', '%');
  63. /**
  64. * List of HTML tags
  65. *
  66. * @var array
  67. */
  68. protected $_htmlTags = array(
  69. 'li'
  70. );
  71. /**
  72. * Maximum fontsize
  73. *
  74. * @var integer
  75. */
  76. protected $_maxFontSize = 20;
  77. /**
  78. * Minimum fontsize
  79. *
  80. * @var integer
  81. */
  82. protected $_minFontSize = 10;
  83. /**
  84. * Set a list of classes to use instead of fontsizes
  85. *
  86. * @param array $classList
  87. * @throws \Zend\Tag\Cloud\Decorator\Exception\InvalidArgumentException When the classlist is empty
  88. * @throws \Zend\Tag\Cloud\Decorator\Exception\InvalidArgumentException When the classlist contains an invalid classname
  89. * @return \Zend\Tag\Cloud\Decorator\HTMLTag
  90. */
  91. public function setClassList(array $classList = null)
  92. {
  93. if (is_array($classList)) {
  94. if (count($classList) === 0) {
  95. throw new InvalidArgumentException('Classlist is empty');
  96. }
  97. foreach ($classList as $class) {
  98. if (!is_string($class)) {
  99. throw new InvalidArgumentException('Classlist contains an invalid classname');
  100. }
  101. }
  102. }
  103. $this->_classList = $classList;
  104. return $this;
  105. }
  106. /**
  107. * Get class list
  108. *
  109. * @return array
  110. */
  111. public function getClassList()
  112. {
  113. return $this->_classList;
  114. }
  115. /**
  116. * Get encoding
  117. *
  118. * @return string
  119. */
  120. public function getEncoding()
  121. {
  122. return $this->_encoding;
  123. }
  124. /**
  125. * Set encoding
  126. *
  127. * @param string $value
  128. * @return \Zend\Tag\Cloud\Decorator\HTMLTag
  129. */
  130. public function setEncoding($value)
  131. {
  132. $this->_encoding = (string) $value;
  133. return $this;
  134. }
  135. /**
  136. * Set the font size unit
  137. *
  138. * Possible values are: em, ex, px, in, cm, mm, pt, pc and %
  139. *
  140. * @param string $fontSizeUnit
  141. * @throws \Zend\Tag\Cloud\Decorator\Exception\InvalidArgumentException When an invalid fontsize unit is specified
  142. * @return \Zend\Tag\Cloud\Decorator\HTMLTag
  143. */
  144. public function setFontSizeUnit($fontSizeUnit)
  145. {
  146. if (!in_array($fontSizeUnit, $this->_alloweFontSizeUnits)) {
  147. throw new InvalidArgumentException('Invalid fontsize unit specified');
  148. }
  149. $this->_fontSizeUnit = (string) $fontSizeUnit;
  150. $this->setClassList(null);
  151. return $this;
  152. }
  153. /**
  154. * Retrieve font size unit
  155. *
  156. * @return string
  157. */
  158. public function getFontSizeUnit()
  159. {
  160. return $this->_fontSizeUnit;
  161. }
  162. /**
  163. * Set the HTML tags surrounding the <a> element
  164. *
  165. * @param array $htmlTags
  166. * @return \Zend\Tag\Cloud\Decorator\HTMLTag
  167. */
  168. public function setHTMLTags(array $htmlTags)
  169. {
  170. $this->_htmlTags = $htmlTags;
  171. return $this;
  172. }
  173. /**
  174. * Get HTML tags map
  175. *
  176. * @return array
  177. */
  178. public function getHTMLTags()
  179. {
  180. return $this->_htmlTags;
  181. }
  182. /**
  183. * Set maximum font size
  184. *
  185. * @param integer $maxFontSize
  186. * @throws \Zend\Tag\Cloud\Decorator\Exception\InvalidArgumentException When fontsize is not numeric
  187. * @return \Zend\Tag\Cloud\Decorator\HTMLTag
  188. */
  189. public function setMaxFontSize($maxFontSize)
  190. {
  191. if (!is_numeric($maxFontSize)) {
  192. throw new InvalidArgumentException('Fontsize must be numeric');
  193. }
  194. $this->_maxFontSize = (int) $maxFontSize;
  195. $this->setClassList(null);
  196. return $this;
  197. }
  198. /**
  199. * Retrieve maximum font size
  200. *
  201. * @return int
  202. */
  203. public function getMaxFontSize()
  204. {
  205. return $this->_maxFontSize;
  206. }
  207. /**
  208. * Set minimum font size
  209. *
  210. * @param int $minFontSize
  211. * @throws \Zend\Tag\Cloud\Decorator\Exception\InvalidArgumentException When fontsize is not numeric
  212. * @return \Zend\Tag\Cloud\Decorator\HTMLTag
  213. */
  214. public function setMinFontSize($minFontSize)
  215. {
  216. if (!is_numeric($minFontSize)) {
  217. throw new InvalidArgumentException('Fontsize must be numeric');
  218. }
  219. $this->_minFontSize = (int) $minFontSize;
  220. $this->setClassList(null);
  221. return $this;
  222. }
  223. /**
  224. * Retrieve minimum font size
  225. *
  226. * @return int
  227. */
  228. public function getMinFontSize()
  229. {
  230. return $this->_minFontSize;
  231. }
  232. /**
  233. * Defined by Zend\Tag\Cloud\Decorator\Tag
  234. *
  235. * @param \Zend\Tag\ItemList $tags
  236. * @return array
  237. */
  238. public function render($tags)
  239. {
  240. if (!$tags instanceof ItemList) {
  241. throw new Exception(sprintf(
  242. 'HtmlTag::render() expects a Zend\Tag\ItemList argument; received "%s"',
  243. (is_object($tags) ? get_class($tags) : gettype($tags))
  244. ));
  245. }
  246. if (null === ($weightValues = $this->getClassList())) {
  247. $weightValues = range($this->getMinFontSize(), $this->getMaxFontSize());
  248. }
  249. $tags->spreadWeightValues($weightValues);
  250. $result = array();
  251. $enc = $this->getEncoding();
  252. foreach ($tags as $tag) {
  253. if (null === ($classList = $this->getClassList())) {
  254. $attribute = sprintf('style="font-size: %d%s;"', $tag->getParam('weightValue'), $this->getFontSizeUnit());
  255. } else {
  256. $attribute = sprintf('class="%s"', htmlspecialchars($tag->getParam('weightValue'), ENT_COMPAT, $enc));
  257. }
  258. $tagHTML = sprintf('<a href="%s" %s>%s</a>', htmlSpecialChars($tag->getParam('url'), ENT_COMPAT, $enc), $attribute, $tag->getTitle());
  259. foreach ($this->getHTMLTags() as $key => $data) {
  260. if (is_array($data)) {
  261. $htmlTag = $key;
  262. $attributes = '';
  263. foreach ($data as $param => $value) {
  264. $attributes .= ' ' . $param . '="' . htmlspecialchars($value, ENT_COMPAT, $enc) . '"';
  265. }
  266. } else {
  267. $htmlTag = $data;
  268. $attributes = '';
  269. }
  270. $tagHTML = sprintf('<%1$s%3$s>%2$s</%1$s>', $htmlTag, $tagHTML, $attributes);
  271. }
  272. $result[] = $tagHTML;
  273. }
  274. return $result;
  275. }
  276. }