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
;
15 class Item
implements TaggableInterface
22 protected $title = null;
29 protected $weight = null;
36 protected $params = array();
39 * Option keys to skip when calling setOptions()
43 protected $skipOptions = array(
49 * Create a new tag according to the options
51 * @param array|Traversable $options
52 * @throws \Zend\Tag\Exception\InvalidArgumentException When invalid options are provided
53 * @throws \Zend\Tag\Exception\InvalidArgumentException When title was not set
54 * @throws \Zend\Tag\Exception\InvalidArgumentException When weight was not set
56 public function __construct($options)
58 if ($options instanceof Traversable
) {
59 $options = ArrayUtils
::iteratorToArray($options);
62 if (!is_array($options)) {
63 throw new Exception\
InvalidArgumentException('Invalid options provided to constructor');
66 $this->setOptions($options);
68 if ($this->title
=== null) {
69 throw new Exception\
InvalidArgumentException('Title was not set');
72 if ($this->weight
=== null) {
73 throw new Exception\
InvalidArgumentException('Weight was not set');
78 * Set options of the tag
80 * @param array $options
81 * @return \Zend\Tag\Item
83 public function setOptions(array $options)
85 foreach ($options as $key => $value) {
86 if (in_array(strtolower($key), $this->skipOptions
)) {
90 $method = 'set' . $key;
91 if (method_exists($this, $method)) {
92 $this->$method($value);
100 * Defined by Zend\Tag\TaggableInterface
104 public function getTitle()
112 * @param string $title
113 * @throws \Zend\Tag\Exception\InvalidArgumentException When title is no string
114 * @return \Zend\Tag\Item
116 public function setTitle($title)
118 if (!is_string($title)) {
119 throw new Exception\
InvalidArgumentException('Title must be a string');
122 $this->title
= (string) $title;
127 * Defined by Zend\Tag\TaggableInterface
131 public function getWeight()
133 return $this->weight
;
139 * @param float $weight
140 * @throws \Zend\Tag\Exception\InvalidArgumentException When weight is not numeric
141 * @return \Zend\Tag\Item
143 public function setWeight($weight)
145 if (!is_numeric($weight)) {
146 throw new Exception\
InvalidArgumentException('Weight must be numeric');
149 $this->weight
= (float) $weight;
154 * Set multiple params at once
156 * @param array $params
157 * @return \Zend\Tag\Item
159 public function setParams(array $params)
161 foreach ($params as $name => $value) {
162 $this->setParam($name, $value);
169 * Defined by Zend\Tag\TaggableInterface
171 * @param string $name
172 * @param mixed $value
173 * @return \Zend\Tag\Item
175 public function setParam($name, $value)
177 $this->params
[$name] = $value;
182 * Defined by Zend\Tag\TaggableInterface
184 * @param string $name
187 public function getParam($name)
189 if (isset($this->params
[$name])) {
190 return $this->params
[$name];