Source of file Cloud.php
Size: 7,868 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Tag/Cloud.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\Tag; use Traversable; use Zend\Stdlib\ArrayUtils; class Cloud { /** * DecoratorInterface for the cloud * * @var Cloud\Decorator\AbstractCloud */ protected $cloudDecorator = null; /** * DecoratorInterface for the tags * * @var Cloud\Decorator\AbstractTag */ protected $tagDecorator = null; /** * List of all tags * * @var ItemList */ protected $tags = null; /** * Plugin manager for decorators * * @var Cloud\DecoratorPluginManager */ protected $decorators = null; /** * Option keys to skip when calling setOptions() * * @var array */ protected $skipOptions = array( 'options', 'config', ); /** * Create a new tag cloud with options * * @param array|Traversable $options */ public function __construct($options = null) { if ($options instanceof Traversable) { $options = ArrayUtils::iteratorToArray($options); } if (is_array($options)) { $this->setOptions($options); } } /** * Set options from array * * @param array $options Configuration for Cloud * @return Cloud */ public function setOptions(array $options) { foreach ($options as $key => $value) { if (in_array(strtolower($key), $this->skipOptions)) { continue; } $method = 'set' . $key; if (method_exists($this, $method)) { $this->$method($value); } } return $this; } /** * Set the tags for the tag cloud. * * $tags should be an array containing single tags as array. Each tag * array should at least contain the keys 'title' and 'weight'. Optionally * you may supply the key 'url', to which the tag links to. Any additional * parameter in the array is silently ignored and can be used by custom * decorators. * * @param array $tags * @throws Exception\InvalidArgumentException * @return Cloud */ public function setTags(array $tags) { foreach ($tags as $tag) { $this->appendTag($tag); } return $this; } /** * Append a single tag to the cloud * * @param TaggableInterface|array $tag * @throws Exception\InvalidArgumentException * @return Cloud */ public function appendTag($tag) { $tags = $this->getItemList(); if ($tag instanceof TaggableInterface) { $tags[] = $tag; return $this; } if (!is_array($tag)) { throw new Exception\InvalidArgumentException(sprintf( 'Tag must be an instance of %s\TaggableInterface or an array; received "%s"', __NAMESPACE__, (is_object($tag) ? get_class($tag) : gettype($tag)) )); } $tags[] = new Item($tag); return $this; } /** * Set the item list * * @param ItemList $itemList * @return Cloud */ public function setItemList(ItemList $itemList) { $this->tags = $itemList; return $this; } /** * Retrieve the item list * * If item list is undefined, creates one. * * @return ItemList */ public function getItemList() { if (null === $this->tags) { $this->setItemList(new ItemList()); } return $this->tags; } /** * Set the decorator for the cloud * * @param mixed $decorator * @throws Exception\InvalidArgumentException * @return Cloud */ public function setCloudDecorator($decorator) { $options = null; if (is_array($decorator)) { if (isset($decorator['options'])) { $options = $decorator['options']; } if (isset($decorator['decorator'])) { $decorator = $decorator['decorator']; } } if (is_string($decorator)) { $decorator = $this->getDecoratorPluginManager()->get($decorator, $options); } if (!($decorator instanceof Cloud\Decorator\AbstractCloud)) { throw new Exception\InvalidArgumentException('DecoratorInterface is no instance of Cloud\Decorator\AbstractCloud'); } $this->cloudDecorator = $decorator; return $this; } /** * Get the decorator for the cloud * * @return Cloud\Decorator\AbstractCloud */ public function getCloudDecorator() { if (null === $this->cloudDecorator) { $this->setCloudDecorator('htmlCloud'); } return $this->cloudDecorator; } /** * Set the decorator for the tags * * @param mixed $decorator * @throws Exception\InvalidArgumentException * @return Cloud */ public function setTagDecorator($decorator) { $options = null; if (is_array($decorator)) { if (isset($decorator['options'])) { $options = $decorator['options']; } if (isset($decorator['decorator'])) { $decorator = $decorator['decorator']; } } if (is_string($decorator)) { $decorator = $this->getDecoratorPluginManager()->get($decorator, $options); } if (!($decorator instanceof Cloud\Decorator\AbstractTag)) { throw new Exception\InvalidArgumentException('DecoratorInterface is no instance of Cloud\Decorator\AbstractTag'); } $this->tagDecorator = $decorator; return $this; } /** * Get the decorator for the tags * * @return Cloud\Decorator\AbstractTag */ public function getTagDecorator() { if (null === $this->tagDecorator) { $this->setTagDecorator('htmlTag'); } return $this->tagDecorator; } /** * Set plugin manager for use with decorators * * @param Cloud\DecoratorPluginManager $decorators * @return Cloud */ public function setDecoratorPluginManager(Cloud\DecoratorPluginManager $decorators) { $this->decorators = $decorators; return $this; } /** * Get the plugin manager for decorators * * @return Cloud\DecoratorPluginManager */ public function getDecoratorPluginManager() { if ($this->decorators === null) { $this->decorators = new Cloud\DecoratorPluginManager(); } return $this->decorators; } /** * Render the tag cloud * * @return string */ public function render() { $tags = $this->getItemList(); if (count($tags) === 0) { return ''; } $tagsResult = $this->getTagDecorator()->render($tags); $cloudResult = $this->getCloudDecorator()->render($tagsResult); return $cloudResult; } /** * Render the tag cloud * * @return string */ public function __toString() { try { $result = $this->render(); return $result; } catch (\Exception $e) { $message = "Exception caught by tag cloud: " . $e->getMessage() . "\nStack Trace:\n" . $e->getTraceAsString(); trigger_error($message, E_USER_WARNING); return ''; } } } |