Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Tag / Cloud.php
blob33c4a7c54be9bac79bbaba112df684f7285730ec
1 <?php
2 /**
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
8 */
10 namespace Zend\Tag;
12 use Traversable;
13 use Zend\Stdlib\ArrayUtils;
15 class Cloud
17 /**
18 * DecoratorInterface for the cloud
20 * @var Cloud\Decorator\AbstractCloud
22 protected $cloudDecorator = null;
24 /**
25 * DecoratorInterface for the tags
27 * @var Cloud\Decorator\AbstractTag
29 protected $tagDecorator = null;
31 /**
32 * List of all tags
34 * @var ItemList
36 protected $tags = null;
38 /**
39 * Plugin manager for decorators
41 * @var Cloud\DecoratorPluginManager
43 protected $decorators = null;
45 /**
46 * Option keys to skip when calling setOptions()
48 * @var array
50 protected $skipOptions = array(
51 'options',
52 'config',
55 /**
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);
70 /**
71 * Set options from array
73 * @param array $options Configuration for Cloud
74 * @return Cloud
76 public function setOptions(array $options)
78 foreach ($options as $key => $value) {
79 if (in_array(strtolower($key), $this->skipOptions)) {
80 continue;
83 $method = 'set' . $key;
84 if (method_exists($this, $method)) {
85 $this->$method($value);
89 return $this;
92 /**
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
99 * decorators.
101 * @param array $tags
102 * @throws Exception\InvalidArgumentException
103 * @return Cloud
105 public function setTags(array $tags)
107 foreach ($tags as $tag) {
108 $this->appendTag($tag);
110 return $this;
114 * Append a single tag to the cloud
116 * @param TaggableInterface|array $tag
117 * @throws Exception\InvalidArgumentException
118 * @return Cloud
120 public function appendTag($tag)
122 $tags = $this->getItemList();
124 if ($tag instanceof TaggableInterface) {
125 $tags[] = $tag;
126 return $this;
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"',
132 __NAMESPACE__,
133 (is_object($tag) ? get_class($tag) : gettype($tag))
137 $tags[] = new Item($tag);
139 return $this;
143 * Set the item list
145 * @param ItemList $itemList
146 * @return Cloud
148 public function setItemList(ItemList $itemList)
150 $this->tags = $itemList;
151 return $this;
155 * Retrieve the item list
157 * If item list is undefined, creates one.
159 * @return ItemList
161 public function getItemList()
163 if (null === $this->tags) {
164 $this->setItemList(new ItemList());
166 return $this->tags;
170 * Set the decorator for the cloud
172 * @param mixed $decorator
173 * @throws Exception\InvalidArgumentException
174 * @return Cloud
176 public function setCloudDecorator($decorator)
178 $options = null;
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;
200 return $this;
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
221 * @return Cloud
223 public function setTagDecorator($decorator)
225 $options = null;
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;
247 return $this;
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
267 * @return Cloud
269 public function setDecoratorPluginManager(Cloud\DecoratorPluginManager $decorators)
271 $this->decorators = $decorators;
272 return $this;
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
292 * @return string
294 public function render()
296 $tags = $this->getItemList();
298 if (count($tags) === 0) {
299 return '';
302 $tagsResult = $this->getTagDecorator()->render($tags);
303 $cloudResult = $this->getCloudDecorator()->render($tagsResult);
305 return $cloudResult;
309 * Render the tag cloud
311 * @return string
313 public function __toString()
315 try {
316 $result = $this->render();
317 return $result;
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);
322 return '';