/vendor/zendframework/zend-form/src/Element.php

https://gitlab.com/yousafsyed/easternglamor · PHP · 510 lines · 228 code · 54 blank · 228 comment · 17 complexity · 57d1f22beabf6523f57f1348bce0dadb MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Form;
  10. use Traversable;
  11. use Zend\Stdlib\ArrayUtils;
  12. use Zend\Stdlib\InitializableInterface;
  13. class Element implements
  14. ElementAttributeRemovalInterface,
  15. ElementInterface,
  16. InitializableInterface,
  17. LabelAwareInterface
  18. {
  19. /**
  20. * @var array
  21. */
  22. protected $attributes = array();
  23. /**
  24. * @var null|string
  25. */
  26. protected $label;
  27. /**
  28. * @var array
  29. */
  30. protected $labelAttributes = array();
  31. /**
  32. * Label specific options
  33. *
  34. * @var array
  35. */
  36. protected $labelOptions = array();
  37. /**
  38. * @var array Validation error messages
  39. */
  40. protected $messages = array();
  41. /**
  42. * @var array custom options
  43. */
  44. protected $options = array();
  45. /**
  46. * @var mixed
  47. */
  48. protected $value;
  49. /**
  50. * @param null|int|string $name Optional name for the element
  51. * @param array $options Optional options for the element
  52. * @throws Exception\InvalidArgumentException
  53. */
  54. public function __construct($name = null, $options = array())
  55. {
  56. if (null !== $name) {
  57. $this->setName($name);
  58. }
  59. if (!empty($options)) {
  60. $this->setOptions($options);
  61. }
  62. }
  63. /**
  64. * This function is automatically called when creating element with factory. It
  65. * allows to perform various operations (add elements...)
  66. *
  67. * @return void
  68. */
  69. public function init()
  70. {
  71. }
  72. /**
  73. * Set value for name
  74. *
  75. * @param string $name
  76. * @return Element|ElementInterface
  77. */
  78. public function setName($name)
  79. {
  80. $this->setAttribute('name', $name);
  81. return $this;
  82. }
  83. /**
  84. * Get value for name
  85. *
  86. * @return string|int
  87. */
  88. public function getName()
  89. {
  90. return $this->getAttribute('name');
  91. }
  92. /**
  93. * Set options for an element. Accepted options are:
  94. * - label: label to associate with the element
  95. * - label_attributes: attributes to use when the label is rendered
  96. * - label_options: label specific options
  97. *
  98. * @param array|Traversable $options
  99. * @return Element|ElementInterface
  100. * @throws Exception\InvalidArgumentException
  101. */
  102. public function setOptions($options)
  103. {
  104. if ($options instanceof Traversable) {
  105. $options = ArrayUtils::iteratorToArray($options);
  106. } elseif (!is_array($options)) {
  107. throw new Exception\InvalidArgumentException(
  108. 'The options parameter must be an array or a Traversable'
  109. );
  110. }
  111. if (isset($options['label'])) {
  112. $this->setLabel($options['label']);
  113. }
  114. if (isset($options['label_attributes'])) {
  115. $this->setLabelAttributes($options['label_attributes']);
  116. }
  117. if (isset($options['label_options'])) {
  118. $this->setLabelOptions($options['label_options']);
  119. }
  120. $this->options = $options;
  121. return $this;
  122. }
  123. /**
  124. * Get defined options
  125. *
  126. * @return array
  127. */
  128. public function getOptions()
  129. {
  130. return $this->options;
  131. }
  132. /**
  133. * Return the specified option
  134. *
  135. * @param string $option
  136. * @return NULL|mixed
  137. */
  138. public function getOption($option)
  139. {
  140. if (!isset($this->options[$option])) {
  141. return;
  142. }
  143. return $this->options[$option];
  144. }
  145. /**
  146. * Set a single option for an element
  147. *
  148. * @param string $key
  149. * @param mixed $value
  150. * @return self
  151. */
  152. public function setOption($key, $value)
  153. {
  154. $this->options[$key] = $value;
  155. return $this;
  156. }
  157. /**
  158. * Set a single element attribute
  159. *
  160. * @param string $key
  161. * @param mixed $value
  162. * @return Element|ElementInterface
  163. */
  164. public function setAttribute($key, $value)
  165. {
  166. // Do not include the value in the list of attributes
  167. if ($key === 'value') {
  168. $this->setValue($value);
  169. return $this;
  170. }
  171. $this->attributes[$key] = $value;
  172. return $this;
  173. }
  174. /**
  175. * Retrieve a single element attribute
  176. *
  177. * @param $key
  178. * @return mixed|null
  179. */
  180. public function getAttribute($key)
  181. {
  182. if (!isset($this->attributes[$key])) {
  183. return;
  184. }
  185. return $this->attributes[$key];
  186. }
  187. /**
  188. * Remove a single attribute
  189. *
  190. * @param string $key
  191. * @return ElementInterface
  192. */
  193. public function removeAttribute($key)
  194. {
  195. unset($this->attributes[$key]);
  196. return $this;
  197. }
  198. /**
  199. * Does the element has a specific attribute ?
  200. *
  201. * @param string $key
  202. * @return bool
  203. */
  204. public function hasAttribute($key)
  205. {
  206. return array_key_exists($key, $this->attributes);
  207. }
  208. /**
  209. * Set many attributes at once
  210. *
  211. * Implementation will decide if this will overwrite or merge.
  212. *
  213. * @param array|Traversable $arrayOrTraversable
  214. * @return Element|ElementInterface
  215. * @throws Exception\InvalidArgumentException
  216. */
  217. public function setAttributes($arrayOrTraversable)
  218. {
  219. if (!is_array($arrayOrTraversable) && !$arrayOrTraversable instanceof Traversable) {
  220. throw new Exception\InvalidArgumentException(sprintf(
  221. '%s expects an array or Traversable argument; received "%s"',
  222. __METHOD__,
  223. (is_object($arrayOrTraversable) ? get_class($arrayOrTraversable) : gettype($arrayOrTraversable))
  224. ));
  225. }
  226. foreach ($arrayOrTraversable as $key => $value) {
  227. $this->setAttribute($key, $value);
  228. }
  229. return $this;
  230. }
  231. /**
  232. * Retrieve all attributes at once
  233. *
  234. * @return array|Traversable
  235. */
  236. public function getAttributes()
  237. {
  238. return $this->attributes;
  239. }
  240. /**
  241. * Remove many attributes at once
  242. *
  243. * @param array $keys
  244. * @return ElementInterface
  245. */
  246. public function removeAttributes(array $keys)
  247. {
  248. foreach ($keys as $key) {
  249. unset($this->attributes[$key]);
  250. }
  251. return $this;
  252. }
  253. /**
  254. * Clear all attributes
  255. *
  256. * @return Element|ElementInterface
  257. */
  258. public function clearAttributes()
  259. {
  260. $this->attributes = array();
  261. return $this;
  262. }
  263. /**
  264. * Set the element value
  265. *
  266. * @param mixed $value
  267. * @return Element
  268. */
  269. public function setValue($value)
  270. {
  271. $this->value = $value;
  272. return $this;
  273. }
  274. /**
  275. * Retrieve the element value
  276. *
  277. * @return mixed
  278. */
  279. public function getValue()
  280. {
  281. return $this->value;
  282. }
  283. /**
  284. * Set the label used for this element
  285. *
  286. * @param $label
  287. * @return Element|ElementInterface
  288. */
  289. public function setLabel($label)
  290. {
  291. if (is_string($label)) {
  292. $this->label = $label;
  293. }
  294. return $this;
  295. }
  296. /**
  297. * Retrieve the label used for this element
  298. *
  299. * @return null|string
  300. */
  301. public function getLabel()
  302. {
  303. return $this->label;
  304. }
  305. /**
  306. * Set the attributes to use with the label
  307. *
  308. * @param array $labelAttributes
  309. * @return Element|ElementInterface
  310. */
  311. public function setLabelAttributes(array $labelAttributes)
  312. {
  313. $this->labelAttributes = $labelAttributes;
  314. return $this;
  315. }
  316. /**
  317. * Get the attributes to use with the label
  318. *
  319. * @return array
  320. */
  321. public function getLabelAttributes()
  322. {
  323. return $this->labelAttributes;
  324. }
  325. /**
  326. * Set many label options at once
  327. *
  328. * Implementation will decide if this will overwrite or merge.
  329. *
  330. * @param array|Traversable $arrayOrTraversable
  331. * @return Element|ElementInterface
  332. * @throws Exception\InvalidArgumentException
  333. */
  334. public function setLabelOptions($arrayOrTraversable)
  335. {
  336. if (!is_array($arrayOrTraversable) && !$arrayOrTraversable instanceof Traversable) {
  337. throw new Exception\InvalidArgumentException(sprintf(
  338. '%s expects an array or Traversable argument; received "%s"',
  339. __METHOD__,
  340. (is_object($arrayOrTraversable) ? get_class($arrayOrTraversable) : gettype($arrayOrTraversable))
  341. ));
  342. }
  343. foreach ($arrayOrTraversable as $key => $value) {
  344. $this->setLabelOption($key, $value);
  345. }
  346. return $this;
  347. }
  348. /**
  349. * Get label specific options
  350. *
  351. * @return array
  352. */
  353. public function getLabelOptions()
  354. {
  355. return $this->labelOptions;
  356. }
  357. /**
  358. * Clear all label options
  359. *
  360. * @return Element|ElementInterface
  361. */
  362. public function clearLabelOptions()
  363. {
  364. $this->labelOptions = array();
  365. return $this;
  366. }
  367. /**
  368. * Remove many attributes at once
  369. *
  370. * @param array $keys
  371. * @return ElementInterface
  372. */
  373. public function removeLabelOptions(array $keys)
  374. {
  375. foreach ($keys as $key) {
  376. unset($this->labelOptions[$key]);
  377. }
  378. return $this;
  379. }
  380. /**
  381. * Set a single label optionn
  382. *
  383. * @param string $key
  384. * @param mixed $value
  385. * @return Element|ElementInterface
  386. */
  387. public function setLabelOption($key, $value)
  388. {
  389. $this->labelOptions[$key] = $value;
  390. return $this;
  391. }
  392. /**
  393. * Retrieve a single label option
  394. *
  395. * @param $key
  396. * @return mixed|null
  397. */
  398. public function getLabelOption($key)
  399. {
  400. if (!isset($this->labelOptions[$key])) {
  401. return;
  402. }
  403. return $this->labelOptions[$key];
  404. }
  405. /**
  406. * Remove a single label option
  407. *
  408. * @param string $key
  409. * @return ElementInterface
  410. */
  411. public function removeLabelOption($key)
  412. {
  413. unset($this->labelOptions[$key]);
  414. return $this;
  415. }
  416. /**
  417. * Does the element has a specific label option ?
  418. *
  419. * @param string $key
  420. * @return bool
  421. */
  422. public function hasLabelOption($key)
  423. {
  424. return array_key_exists($key, $this->labelOptions);
  425. }
  426. /**
  427. * Set a list of messages to report when validation fails
  428. *
  429. * @param array|Traversable $messages
  430. * @return Element|ElementInterface
  431. * @throws Exception\InvalidArgumentException
  432. */
  433. public function setMessages($messages)
  434. {
  435. if (!is_array($messages) && !$messages instanceof Traversable) {
  436. throw new Exception\InvalidArgumentException(sprintf(
  437. '%s expects an array or Traversable object of validation error messages; received "%s"',
  438. __METHOD__,
  439. (is_object($messages) ? get_class($messages) : gettype($messages))
  440. ));
  441. }
  442. $this->messages = $messages;
  443. return $this;
  444. }
  445. /**
  446. * Get validation error messages, if any.
  447. *
  448. * Returns a list of validation failure messages, if any.
  449. *
  450. * @return array|Traversable
  451. */
  452. public function getMessages()
  453. {
  454. return $this->messages;
  455. }
  456. }