FieldDescriptionCollection.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. *
  19. * @return void
  20. */
  21. public function add(FieldDescriptionInterface $fieldDescription)
  22. {
  23. $this->elements[$fieldDescription->getName()] = $fieldDescription;
  24. }
  25. /**
  26. * @return array
  27. */
  28. public function getElements()
  29. {
  30. return $this->elements;
  31. }
  32. /**
  33. * @param string $name
  34. *
  35. * @return bool
  36. */
  37. public function has($name)
  38. {
  39. return array_key_exists($name, $this->elements);
  40. }
  41. /**
  42. * @throws \InvalidArgumentException
  43. *
  44. * @param string $name
  45. *
  46. * @return FieldDescriptionInterface
  47. */
  48. public function get($name)
  49. {
  50. if ($this->has($name)) {
  51. return $this->elements[$name];
  52. }
  53. throw new \InvalidArgumentException(sprintf('Element "%s" does not exist.', $name));
  54. }
  55. /**
  56. * @param string $name
  57. *
  58. * @return void
  59. */
  60. public function remove($name)
  61. {
  62. if ($this->has($name)) {
  63. unset($this->elements[$name]);
  64. }
  65. }
  66. /**
  67. * {@inheritDoc}
  68. */
  69. public function offsetExists($offset)
  70. {
  71. return $this->has($offset);
  72. }
  73. /**
  74. * {@inheritDoc}
  75. */
  76. public function offsetGet($offset)
  77. {
  78. return $this->get($offset);
  79. }
  80. /**
  81. * {@inheritDoc}
  82. */
  83. public function offsetSet($offset, $value)
  84. {
  85. throw new \RunTimeException('Cannot set value, use add');
  86. }
  87. /**
  88. * {@inheritDoc}
  89. */
  90. public function offsetUnset($offset)
  91. {
  92. $this->remove($offset);
  93. }
  94. /**
  95. * {@inheritDoc}
  96. */
  97. public function count()
  98. {
  99. return count($this->elements);
  100. }
  101. /**
  102. * @param array $keys
  103. */
  104. public function reorder(array $keys)
  105. {
  106. array_unshift($keys, 'batch');
  107. $this->elements = array_merge(array_flip($keys), $this->elements);
  108. }
  109. }