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