TraversableArrayObject.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  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 Symfony\Component\PropertyAccess\Tests\Fixtures;
  11. /**
  12. * This class is a hand written simplified version of PHP native `ArrayObject`
  13. * class, to show that it behaves differently than the PHP native implementation.
  14. */
  15. class TraversableArrayObject implements \ArrayAccess, \IteratorAggregate, \Countable, \Serializable
  16. {
  17. private $array;
  18. public function __construct(array $array = null)
  19. {
  20. $this->array = $array ?: array();
  21. }
  22. public function offsetExists($offset)
  23. {
  24. return array_key_exists($offset, $this->array);
  25. }
  26. public function offsetGet($offset)
  27. {
  28. return $this->array[$offset];
  29. }
  30. public function offsetSet($offset, $value)
  31. {
  32. if (null === $offset) {
  33. $this->array[] = $value;
  34. } else {
  35. $this->array[$offset] = $value;
  36. }
  37. }
  38. public function offsetUnset($offset)
  39. {
  40. unset($this->array[$offset]);
  41. }
  42. public function getIterator()
  43. {
  44. return new \ArrayIterator($this->array);
  45. }
  46. public function count()
  47. {
  48. return count($this->array);
  49. }
  50. public function serialize()
  51. {
  52. return serialize($this->array);
  53. }
  54. public function unserialize($serialized)
  55. {
  56. $this->array = (array) unserialize((string) $serialized);
  57. }
  58. }