AuthorList.php 1.4 KB

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