AuthorList.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace JMS\SerializerBundle\Tests\Fixtures;
  3. /**
  4. * An array-acting object that holds many author instances.
  5. */
  6. class AuthorList implements \IteratorAggregate, \Countable, \ArrayAccess
  7. {
  8. protected $authors = array();
  9. /**
  10. * @param Author $author
  11. */
  12. public function add(Author $author)
  13. {
  14. $this->authors[] = $author;
  15. }
  16. /**
  17. * @see IteratorAggregate
  18. */
  19. public function getIterator()
  20. {
  21. return new \ArrayIterator($this->authors);
  22. }
  23. /**
  24. * @see Countable
  25. */
  26. public function count()
  27. {
  28. return count($this->authors);
  29. }
  30. /**
  31. * @see ArrayAccess
  32. */
  33. public function offsetExists($offset)
  34. {
  35. return isset($this->authors[$offset]);
  36. }
  37. /**
  38. * @see ArrayAccess
  39. */
  40. public function offsetGet($offset)
  41. {
  42. return isset($this->authors[$offset]) ? $this->authors[$offset] : null;
  43. }
  44. /**
  45. * @see ArrayAccess
  46. */
  47. public function offsetSet($offset, $value)
  48. {
  49. if (null === $offset) {
  50. $this->authors[] = $value;
  51. } else {
  52. $this->authors[$offset] = $value;
  53. }
  54. }
  55. /**
  56. * @see ArrayAccess
  57. */
  58. public function offsetUnset($offset)
  59. {
  60. unset($this->authors[$offset]);
  61. }
  62. }