ListCollection.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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\Datagrid;
  12. use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
  13. class ListCollection
  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. }