FieldDescriptionCollection.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project 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. namespace Sonata\AdminBundle\Admin;
  11. /**
  12. * Class FieldDescriptionCollection.
  13. *
  14. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  15. */
  16. class FieldDescriptionCollection implements \ArrayAccess, \Countable
  17. {
  18. /**
  19. * @var FieldDescriptionInterface[]
  20. */
  21. protected $elements = array();
  22. /**
  23. * @param FieldDescriptionInterface $fieldDescription
  24. */
  25. public function add(FieldDescriptionInterface $fieldDescription)
  26. {
  27. $this->elements[$fieldDescription->getName()] = $fieldDescription;
  28. }
  29. /**
  30. * @return array
  31. */
  32. public function getElements()
  33. {
  34. return $this->elements;
  35. }
  36. /**
  37. * @param string $name
  38. *
  39. * @return bool
  40. */
  41. public function has($name)
  42. {
  43. return array_key_exists($name, $this->elements);
  44. }
  45. /**
  46. * @throws \InvalidArgumentException
  47. *
  48. * @param string $name
  49. *
  50. * @return FieldDescriptionInterface
  51. */
  52. public function get($name)
  53. {
  54. if ($this->has($name)) {
  55. return $this->elements[$name];
  56. }
  57. throw new \InvalidArgumentException(sprintf('Element "%s" does not exist.', $name));
  58. }
  59. /**
  60. * @param string $name
  61. */
  62. public function remove($name)
  63. {
  64. if ($this->has($name)) {
  65. unset($this->elements[$name]);
  66. }
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function offsetExists($offset)
  72. {
  73. return $this->has($offset);
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function offsetGet($offset)
  79. {
  80. return $this->get($offset);
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function offsetSet($offset, $value)
  86. {
  87. throw new \RunTimeException('Cannot set value, use add');
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function offsetUnset($offset)
  93. {
  94. $this->remove($offset);
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function count()
  100. {
  101. return count($this->elements);
  102. }
  103. /**
  104. * @param array $keys
  105. */
  106. public function reorder(array $keys)
  107. {
  108. if ($this->has('batch')) {
  109. array_unshift($keys, 'batch');
  110. }
  111. $this->elements = array_merge(array_flip($keys), $this->elements);
  112. }
  113. }