FieldDescriptionCollection.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /*
  3. * This file is part of the Sonata package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. *
  10. */
  11. namespace Sonata\AdminBundle\Admin;
  12. use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
  13. class FieldDescriptionCollection implements \ArrayAccess, \Countable
  14. {
  15. protected $elements = array();
  16. /**
  17. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  18. * @return void
  19. */
  20. public function add(FieldDescriptionInterface $fieldDescription)
  21. {
  22. $this->elements[$fieldDescription->getName()] = $fieldDescription;
  23. }
  24. /**
  25. * @return array
  26. */
  27. public function getElements()
  28. {
  29. return $this->elements;
  30. }
  31. /**
  32. * @param string $name
  33. * @return bool
  34. */
  35. public function has($name)
  36. {
  37. return array_key_exists($name, $this->elements);
  38. }
  39. /**
  40. * @throws \InvalidArgumentException
  41. * @param string $name
  42. * @return array
  43. */
  44. public function get($name)
  45. {
  46. if ($this->has($name)) {
  47. return $this->elements[$name];
  48. }
  49. throw new \InvalidArgumentException(sprintf('Element "%s" does not exist.', $name));
  50. }
  51. /**
  52. * @param string $name
  53. * @return void
  54. */
  55. public function remove($name)
  56. {
  57. if ($this->has($name)) {
  58. unset($this->elements[$name]);
  59. }
  60. }
  61. public function offsetExists($offset)
  62. {
  63. return $this->has($offset);
  64. }
  65. public function offsetGet($offset)
  66. {
  67. return $this->get($offset);
  68. }
  69. public function offsetSet($offset, $value)
  70. {
  71. throw new \RunTimeException('Cannot set value, use add');
  72. }
  73. public function offsetUnset($offset)
  74. {
  75. $this->remove($offset);
  76. }
  77. public function count()
  78. {
  79. return count($this->elements);
  80. }
  81. }