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
;
14 use Zend\Stdlib\InitializableInterface
;
16 class Element
implements
17 ElementAttributeRemovalInterface
,
19 InitializableInterface
24 protected $attributes = array();
34 protected $labelAttributes;
37 * @var array Validation error messages
39 protected $messages = array();
42 * @var array custom options
44 protected $options = array();
52 * @param null|int|string $name Optional name for the element
53 * @param array $options Optional options for the element
54 * @throws Exception\InvalidArgumentException
56 public function __construct($name = null, $options = array())
59 $this->setName($name);
62 if (!empty($options)) {
63 $this->setOptions($options);
68 * This function is automatically called when creating element with factory. It
69 * allows to perform various operations (add elements...)
73 public function init()
81 * @return Element|ElementInterface
83 public function setName($name)
85 $this->setAttribute('name', $name);
94 public function getName()
96 return $this->getAttribute('name');
100 * Set options for an element. Accepted options are:
101 * - label: label to associate with the element
102 * - label_attributes: attributes to use when the label is rendered
104 * @param array|Traversable $options
105 * @return Element|ElementInterface
106 * @throws Exception\InvalidArgumentException
108 public function setOptions($options)
110 if ($options instanceof Traversable
) {
111 $options = ArrayUtils
::iteratorToArray($options);
112 } elseif (!is_array($options)) {
113 throw new Exception\
InvalidArgumentException(
114 'The options parameter must be an array or a Traversable'
118 if (isset($options['label'])) {
119 $this->setLabel($options['label']);
122 if (isset($options['label_attributes'])) {
123 $this->setLabelAttributes($options['label_attributes']);
126 $this->options
= $options;
132 * Get defined options
136 public function getOptions()
138 return $this->options
;
142 * Return the specified option
144 * @param string $option
147 public function getOption($option)
149 if (!isset($this->options
[$option])) {
153 return $this->options
[$option];
157 * Set a single element attribute
160 * @param mixed $value
161 * @return Element|ElementInterface
163 public function setAttribute($key, $value)
165 // Do not include the value in the list of attributes
166 if ($key === 'value') {
167 $this->setValue($value);
170 $this->attributes
[$key] = $value;
175 * Retrieve a single element attribute
180 public function getAttribute($key)
182 if (!array_key_exists($key, $this->attributes
)) {
185 return $this->attributes
[$key];
189 * Remove a single attribute
192 * @return ElementInterface
194 public function removeAttribute($key)
196 unset($this->attributes
[$key]);
201 * Does the element has a specific attribute ?
206 public function hasAttribute($key)
208 return array_key_exists($key, $this->attributes
);
212 * Set many attributes at once
214 * Implementation will decide if this will overwrite or merge.
216 * @param array|Traversable $arrayOrTraversable
217 * @return Element|ElementInterface
218 * @throws Exception\InvalidArgumentException
220 public function setAttributes($arrayOrTraversable)
222 if (!is_array($arrayOrTraversable) && !$arrayOrTraversable instanceof Traversable
) {
223 throw new Exception\
InvalidArgumentException(sprintf(
224 '%s expects an array or Traversable argument; received "%s"',
226 (is_object($arrayOrTraversable) ?
get_class($arrayOrTraversable) : gettype($arrayOrTraversable))
229 foreach ($arrayOrTraversable as $key => $value) {
230 $this->setAttribute($key, $value);
236 * Retrieve all attributes at once
238 * @return array|Traversable
240 public function getAttributes()
242 return $this->attributes
;
246 * Remove many attributes at once
249 * @return ElementInterface
251 public function removeAttributes(array $keys)
253 foreach ($keys as $key) {
254 unset($this->attributes
[$key]);
261 * Clear all attributes
263 * @return Element|ElementInterface
265 public function clearAttributes()
267 $this->attributes
= array();
272 * Set the element value
274 * @param mixed $value
277 public function setValue($value)
279 $this->value
= $value;
284 * Retrieve the element value
288 public function getValue()
294 * Set the label used for this element
297 * @return Element|ElementInterface
299 public function setLabel($label)
301 if (is_string($label)) {
302 $this->label
= $label;
309 * Retrieve the label used for this element
313 public function getLabel()
319 * Set the attributes to use with the label
321 * @param array $labelAttributes
322 * @return Element|ElementInterface
324 public function setLabelAttributes(array $labelAttributes)
326 $this->labelAttributes
= $labelAttributes;
331 * Get the attributes to use with the label
335 public function getLabelAttributes()
337 return $this->labelAttributes
;
341 * Set a list of messages to report when validation fails
343 * @param array|Traversable $messages
344 * @return Element|ElementInterface
345 * @throws Exception\InvalidArgumentException
347 public function setMessages($messages)
349 if (!is_array($messages) && !$messages instanceof Traversable
) {
350 throw new Exception\
InvalidArgumentException(sprintf(
351 '%s expects an array or Traversable object of validation error messages; received "%s"',
353 (is_object($messages) ?
get_class($messages) : gettype($messages))
357 $this->messages
= $messages;
362 * Get validation error messages, if any.
364 * Returns a list of validation failure messages, if any.
366 * @return array|Traversable
368 public function getMessages()
370 return $this->messages
;