123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php
- /*
- * This file is part of the Sonata Project package.
- *
- * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Sonata\AdminBundle\Admin;
- /**
- * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
- */
- class FieldDescriptionCollection implements \ArrayAccess, \Countable
- {
- /**
- * @var FieldDescriptionInterface[]
- */
- protected $elements = array();
- /**
- * @param FieldDescriptionInterface $fieldDescription
- */
- public function add(FieldDescriptionInterface $fieldDescription)
- {
- $this->elements[$fieldDescription->getName()] = $fieldDescription;
- }
- /**
- * @return array
- */
- public function getElements()
- {
- return $this->elements;
- }
- /**
- * @param string $name
- *
- * @return bool
- */
- public function has($name)
- {
- return array_key_exists($name, $this->elements);
- }
- /**
- * @throws \InvalidArgumentException
- *
- * @param string $name
- *
- * @return FieldDescriptionInterface
- */
- public function get($name)
- {
- if ($this->has($name)) {
- return $this->elements[$name];
- }
- throw new \InvalidArgumentException(sprintf('Element "%s" does not exist.', $name));
- }
- /**
- * @param string $name
- */
- public function remove($name)
- {
- if ($this->has($name)) {
- unset($this->elements[$name]);
- }
- }
- /**
- * {@inheritdoc}
- */
- public function offsetExists($offset)
- {
- return $this->has($offset);
- }
- /**
- * {@inheritdoc}
- */
- public function offsetGet($offset)
- {
- return $this->get($offset);
- }
- /**
- * {@inheritdoc}
- */
- public function offsetSet($offset, $value)
- {
- throw new \RuntimeException('Cannot set value, use add');
- }
- /**
- * {@inheritdoc}
- */
- public function offsetUnset($offset)
- {
- $this->remove($offset);
- }
- /**
- * {@inheritdoc}
- */
- public function count()
- {
- return count($this->elements);
- }
- /**
- * @param array $keys
- */
- public function reorder(array $keys)
- {
- if ($this->has('batch')) {
- array_unshift($keys, 'batch');
- }
- $this->elements = array_merge(array_flip($keys), $this->elements);
- }
- }
|