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 / Form / Element.php
blob858e9caa46800d6dd0c61ee06c3e8f50f915bde3
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\Form;
12 use Traversable;
13 use Zend\Stdlib\ArrayUtils;
14 use Zend\Stdlib\InitializableInterface;
16 class Element implements
17 ElementAttributeRemovalInterface,
18 ElementInterface,
19 InitializableInterface
21 /**
22 * @var array
24 protected $attributes = array();
26 /**
27 * @var string
29 protected $label;
31 /**
32 * @var array
34 protected $labelAttributes;
36 /**
37 * @var array Validation error messages
39 protected $messages = array();
41 /**
42 * @var array custom options
44 protected $options = array();
46 /**
47 * @var mixed
49 protected $value;
51 /**
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())
58 if (null !== $name) {
59 $this->setName($name);
62 if (!empty($options)) {
63 $this->setOptions($options);
67 /**
68 * This function is automatically called when creating element with factory. It
69 * allows to perform various operations (add elements...)
71 * @return void
73 public function init()
77 /**
78 * Set value for name
80 * @param string $name
81 * @return Element|ElementInterface
83 public function setName($name)
85 $this->setAttribute('name', $name);
86 return $this;
89 /**
90 * Get value for name
92 * @return string|int
94 public function getName()
96 return $this->getAttribute('name');
99 /**
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;
128 return $this;
132 * Get defined options
134 * @return array
136 public function getOptions()
138 return $this->options;
142 * Return the specified option
144 * @param string $option
145 * @return NULL|mixed
147 public function getOption($option)
149 if (!isset($this->options[$option])) {
150 return null;
153 return $this->options[$option];
157 * Set a single element attribute
159 * @param string $key
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);
168 return $this;
170 $this->attributes[$key] = $value;
171 return $this;
175 * Retrieve a single element attribute
177 * @param $key
178 * @return mixed|null
180 public function getAttribute($key)
182 if (!array_key_exists($key, $this->attributes)) {
183 return null;
185 return $this->attributes[$key];
189 * Remove a single attribute
191 * @param string $key
192 * @return ElementInterface
194 public function removeAttribute($key)
196 unset($this->attributes[$key]);
197 return $this;
201 * Does the element has a specific attribute ?
203 * @param string $key
204 * @return bool
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"',
225 __METHOD__,
226 (is_object($arrayOrTraversable) ? get_class($arrayOrTraversable) : gettype($arrayOrTraversable))
229 foreach ($arrayOrTraversable as $key => $value) {
230 $this->setAttribute($key, $value);
232 return $this;
236 * Retrieve all attributes at once
238 * @return array|Traversable
240 public function getAttributes()
242 return $this->attributes;
246 * Remove many attributes at once
248 * @param array $keys
249 * @return ElementInterface
251 public function removeAttributes(array $keys)
253 foreach ($keys as $key) {
254 unset($this->attributes[$key]);
257 return $this;
261 * Clear all attributes
263 * @return Element|ElementInterface
265 public function clearAttributes()
267 $this->attributes = array();
268 return $this;
272 * Set the element value
274 * @param mixed $value
275 * @return Element
277 public function setValue($value)
279 $this->value = $value;
280 return $this;
284 * Retrieve the element value
286 * @return mixed
288 public function getValue()
290 return $this->value;
294 * Set the label used for this element
296 * @param $label
297 * @return Element|ElementInterface
299 public function setLabel($label)
301 if (is_string($label)) {
302 $this->label = $label;
305 return $this;
309 * Retrieve the label used for this element
311 * @return string
313 public function getLabel()
315 return $this->label;
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;
327 return $this;
331 * Get the attributes to use with the label
333 * @return array
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"',
352 __METHOD__,
353 (is_object($messages) ? get_class($messages) : gettype($messages))
357 $this->messages = $messages;
358 return $this;
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;