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
13 use Zend\Stdlib\ArrayUtils
;
18 * DecoratorInterface for the cloud
20 * @var Cloud\Decorator\AbstractCloud
22 protected $cloudDecorator = null;
25 * DecoratorInterface for the tags
27 * @var Cloud\Decorator\AbstractTag
29 protected $tagDecorator = null;
36 protected $tags = null;
39 * Plugin manager for decorators
41 * @var Cloud\DecoratorPluginManager
43 protected $decorators = null;
46 * Option keys to skip when calling setOptions()
50 protected $skipOptions = array(
56 * Create a new tag cloud with options
58 * @param array|Traversable $options
60 public function __construct($options = null)
62 if ($options instanceof Traversable
) {
63 $options = ArrayUtils
::iteratorToArray($options);
65 if (is_array($options)) {
66 $this->setOptions($options);
71 * Set options from array
73 * @param array $options Configuration for Cloud
76 public function setOptions(array $options)
78 foreach ($options as $key => $value) {
79 if (in_array(strtolower($key), $this->skipOptions
)) {
83 $method = 'set' . $key;
84 if (method_exists($this, $method)) {
85 $this->$method($value);
93 * Set the tags for the tag cloud.
95 * $tags should be an array containing single tags as array. Each tag
96 * array should at least contain the keys 'title' and 'weight'. Optionally
97 * you may supply the key 'url', to which the tag links to. Any additional
98 * parameter in the array is silently ignored and can be used by custom
102 * @throws Exception\InvalidArgumentException
105 public function setTags(array $tags)
107 foreach ($tags as $tag) {
108 $this->appendTag($tag);
114 * Append a single tag to the cloud
116 * @param TaggableInterface|array $tag
117 * @throws Exception\InvalidArgumentException
120 public function appendTag($tag)
122 $tags = $this->getItemList();
124 if ($tag instanceof TaggableInterface
) {
129 if (!is_array($tag)) {
130 throw new Exception\
InvalidArgumentException(sprintf(
131 'Tag must be an instance of %s\TaggableInterface or an array; received "%s"',
133 (is_object($tag) ?
get_class($tag) : gettype($tag))
137 $tags[] = new Item($tag);
145 * @param ItemList $itemList
148 public function setItemList(ItemList
$itemList)
150 $this->tags
= $itemList;
155 * Retrieve the item list
157 * If item list is undefined, creates one.
161 public function getItemList()
163 if (null === $this->tags
) {
164 $this->setItemList(new ItemList());
170 * Set the decorator for the cloud
172 * @param mixed $decorator
173 * @throws Exception\InvalidArgumentException
176 public function setCloudDecorator($decorator)
180 if (is_array($decorator)) {
181 if (isset($decorator['options'])) {
182 $options = $decorator['options'];
185 if (isset($decorator['decorator'])) {
186 $decorator = $decorator['decorator'];
190 if (is_string($decorator)) {
191 $decorator = $this->getDecoratorPluginManager()->get($decorator, $options);
194 if (!($decorator instanceof Cloud\Decorator\AbstractCloud
)) {
195 throw new Exception\
InvalidArgumentException('DecoratorInterface is no instance of Cloud\Decorator\AbstractCloud');
198 $this->cloudDecorator
= $decorator;
204 * Get the decorator for the cloud
206 * @return Cloud\Decorator\AbstractCloud
208 public function getCloudDecorator()
210 if (null === $this->cloudDecorator
) {
211 $this->setCloudDecorator('htmlCloud');
213 return $this->cloudDecorator
;
217 * Set the decorator for the tags
219 * @param mixed $decorator
220 * @throws Exception\InvalidArgumentException
223 public function setTagDecorator($decorator)
227 if (is_array($decorator)) {
228 if (isset($decorator['options'])) {
229 $options = $decorator['options'];
232 if (isset($decorator['decorator'])) {
233 $decorator = $decorator['decorator'];
237 if (is_string($decorator)) {
238 $decorator = $this->getDecoratorPluginManager()->get($decorator, $options);
241 if (!($decorator instanceof Cloud\Decorator\AbstractTag
)) {
242 throw new Exception\
InvalidArgumentException('DecoratorInterface is no instance of Cloud\Decorator\AbstractTag');
245 $this->tagDecorator
= $decorator;
251 * Get the decorator for the tags
253 * @return Cloud\Decorator\AbstractTag
255 public function getTagDecorator()
257 if (null === $this->tagDecorator
) {
258 $this->setTagDecorator('htmlTag');
260 return $this->tagDecorator
;
264 * Set plugin manager for use with decorators
266 * @param Cloud\DecoratorPluginManager $decorators
269 public function setDecoratorPluginManager(Cloud\DecoratorPluginManager
$decorators)
271 $this->decorators
= $decorators;
276 * Get the plugin manager for decorators
278 * @return Cloud\DecoratorPluginManager
280 public function getDecoratorPluginManager()
282 if ($this->decorators
=== null) {
283 $this->decorators
= new Cloud\
DecoratorPluginManager();
286 return $this->decorators
;
290 * Render the tag cloud
294 public function render()
296 $tags = $this->getItemList();
298 if (count($tags) === 0) {
302 $tagsResult = $this->getTagDecorator()->render($tags);
303 $cloudResult = $this->getCloudDecorator()->render($tagsResult);
309 * Render the tag cloud
313 public function __toString()
316 $result = $this->render();
318 } catch (\Exception
$e) {
319 $message = "Exception caught by tag cloud: " . $e->getMessage()
320 . "\nStack Trace:\n" . $e->getTraceAsString();
321 trigger_error($message, E_USER_WARNING
);