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

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