PageRenderTime 43ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Tag/Cloud/Decorator/HtmlTag.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 306 lines | 127 code | 34 blank | 145 comment | 11 complexity | c7244c2d30719a4cd98294c824c78c99 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. * @version $Id: HtmlTag.php 23775 2011-03-01 17:25:24Z ralph $
  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-2011 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. * @var string Encoding to utilize
  46. */
  47. protected $_encoding = 'UTF-8';
  48. /**
  49. * Unit for the fontsize
  50. *
  51. * @var string
  52. */
  53. protected $_fontSizeUnit = 'px';
  54. /**
  55. * Allowed fontsize units
  56. *
  57. * @var array
  58. */
  59. protected $_alloweFontSizeUnits = array('em', 'ex', 'px', 'in', 'cm', 'mm', 'pt', 'pc', '%');
  60. /**
  61. * List of HTML tags
  62. *
  63. * @var array
  64. */
  65. protected $_htmlTags = array(
  66. 'li'
  67. );
  68. /**
  69. * Maximum fontsize
  70. *
  71. * @var integer
  72. */
  73. protected $_maxFontSize = 20;
  74. /**
  75. * Minimum fontsize
  76. *
  77. * @var integer
  78. */
  79. protected $_minFontSize = 10;
  80. /**
  81. * Set a list of classes to use instead of fontsizes
  82. *
  83. * @param array $classList
  84. * @throws Zend_Tag_Cloud_Decorator_Exception When the classlist is empty
  85. * @throws Zend_Tag_Cloud_Decorator_Exception When the classlist contains an invalid classname
  86. * @return Zend_Tag_Cloud_Decorator_HtmlTag
  87. */
  88. public function setClassList(array $classList = null)
  89. {
  90. if (is_array($classList)) {
  91. if (count($classList) === 0) {
  92. require_once 'Zend/Tag/Cloud/Decorator/Exception.php';
  93. throw new Zend_Tag_Cloud_Decorator_Exception('Classlist is empty');
  94. }
  95. foreach ($classList as $class) {
  96. if (!is_string($class)) {
  97. require_once 'Zend/Tag/Cloud/Decorator/Exception.php';
  98. throw new Zend_Tag_Cloud_Decorator_Exception('Classlist contains an invalid classname');
  99. }
  100. }
  101. }
  102. $this->_classList = $classList;
  103. return $this;
  104. }
  105. /**
  106. * Get class list
  107. *
  108. * @return array
  109. */
  110. public function getClassList()
  111. {
  112. return $this->_classList;
  113. }
  114. /**
  115. * Get encoding
  116. *
  117. * @return string
  118. */
  119. public function getEncoding()
  120. {
  121. return $this->_encoding;
  122. }
  123. /**
  124. * Set encoding
  125. *
  126. * @param string $value
  127. * @return Zend_Tag_Cloud_Decorator_HtmlTag
  128. */
  129. public function setEncoding($value)
  130. {
  131. $this->_encoding = (string) $value;
  132. return $this;
  133. }
  134. /**
  135. * Set the font size unit
  136. *
  137. * Possible values are: em, ex, px, in, cm, mm, pt, pc and %
  138. *
  139. * @param string $fontSizeUnit
  140. * @throws Zend_Tag_Cloud_Decorator_Exception When an invalid fontsize unit is specified
  141. * @return Zend_Tag_Cloud_Decorator_HtmlTag
  142. */
  143. public function setFontSizeUnit($fontSizeUnit)
  144. {
  145. if (!in_array($fontSizeUnit, $this->_alloweFontSizeUnits)) {
  146. require_once 'Zend/Tag/Cloud/Decorator/Exception.php';
  147. throw new Zend_Tag_Cloud_Decorator_Exception('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 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. require_once 'Zend/Tag/Cloud/Decorator/Exception.php';
  193. throw new Zend_Tag_Cloud_Decorator_Exception('Fontsize must be numeric');
  194. }
  195. $this->_maxFontSize = (int) $maxFontSize;
  196. $this->setClassList(null);
  197. return $this;
  198. }
  199. /**
  200. * Retrieve maximum font size
  201. *
  202. * @return int
  203. */
  204. public function getMaxFontSize()
  205. {
  206. return $this->_maxFontSize;
  207. }
  208. /**
  209. * Set minimum font size
  210. *
  211. * @param int $minFontSize
  212. * @throws Zend_Tag_Cloud_Decorator_Exception When fontsize is not numeric
  213. * @return Zend_Tag_Cloud_Decorator_HtmlTag
  214. */
  215. public function setMinFontSize($minFontSize)
  216. {
  217. if (!is_numeric($minFontSize)) {
  218. require_once 'Zend/Tag/Cloud/Decorator/Exception.php';
  219. throw new Zend_Tag_Cloud_Decorator_Exception('Fontsize must be numeric');
  220. }
  221. $this->_minFontSize = (int) $minFontSize;
  222. $this->setClassList(null);
  223. return $this;
  224. }
  225. /**
  226. * Retrieve minimum font size
  227. *
  228. * @return int
  229. */
  230. public function getMinFontSize()
  231. {
  232. return $this->_minFontSize;
  233. }
  234. /**
  235. * Defined by Zend_Tag_Cloud_Decorator_Tag
  236. *
  237. * @param Zend_Tag_ItemList $tags
  238. * @return array
  239. */
  240. public function render(Zend_Tag_ItemList $tags)
  241. {
  242. if (null === ($weightValues = $this->getClassList())) {
  243. $weightValues = range($this->getMinFontSize(), $this->getMaxFontSize());
  244. }
  245. $tags->spreadWeightValues($weightValues);
  246. $result = array();
  247. $enc = $this->getEncoding();
  248. foreach ($tags as $tag) {
  249. if (null === ($classList = $this->getClassList())) {
  250. $attribute = sprintf('style="font-size: %d%s;"', $tag->getParam('weightValue'), $this->getFontSizeUnit());
  251. } else {
  252. $attribute = sprintf('class="%s"', htmlspecialchars($tag->getParam('weightValue'), ENT_COMPAT, $enc));
  253. }
  254. $tagHtml = sprintf('<a href="%s" %s>%s</a>', htmlSpecialChars($tag->getParam('url'), ENT_COMPAT, $enc), $attribute, $tag->getTitle());
  255. foreach ($this->getHtmlTags() as $key => $data) {
  256. if (is_array($data)) {
  257. $htmlTag = $key;
  258. $attributes = '';
  259. foreach ($data as $param => $value) {
  260. $attributes .= ' ' . $param . '="' . htmlspecialchars($value, ENT_COMPAT, $enc) . '"';
  261. }
  262. } else {
  263. $htmlTag = $data;
  264. $attributes = '';
  265. }
  266. $tagHtml = sprintf('<%1$s%3$s>%2$s</%1$s>', $htmlTag, $tagHtml, $attributes);
  267. }
  268. $result[] = $tagHtml;
  269. }
  270. return $result;
  271. }
  272. }