Passed
Pull Request — main (#1)
by Anatoly
02:42
created

HydrableObjectCollection::add()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 2
rs 10
1
<?php declare(strict_types=1);
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Fenric <[email protected]>
7
 * @copyright Copyright (c) 2021, Anatoly Fenric
8
 * @license https://github.com/sunrise-php/hydrator/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/hydrator
10
 */
11
12
namespace Sunrise\Hydrator;
13
14
/**
15
 * Import classes
16
 */
17
use ArrayIterator;
18
use InvalidArgumentException;
19
use Traversable;
20
21
/**
22
 * Import functions
23
 */
24
use function get_called_class;
25
use function sprintf;
26
27
/**
28
 * HydrableObjectCollection
29
 */
30
class HydrableObjectCollection implements HydrableObjectCollectionInterface
31
{
32
33
    /**
34
     * The collection objects
35
     *
36
     * @var HydrableObjectInterface[]
37
     */
38
    private array $objects = [];
39
40
    /**
41
     * {@inheritDoc}
42
     */
43 3
    final public function add(HydrableObjectInterface $object)
44
    {
45 3
        $type = static::T;
46
47 3
        if (!($object instanceof $type)) {
48 1
            throw new InvalidArgumentException(sprintf(
49 1
                'The <%s> collection must contain only the <%s> objects.',
50 1
                get_called_class(),
51 1
                $type,
52
            ));
53
        }
54
55 2
        $this->objects[] = $object;
56 2
    }
57
58
    /**
59
     * {@inheritDoc}
60
     */
61 2
    final public function getIterator() : Traversable
62
    {
63 2
        return new ArrayIterator($this->objects);
64
    }
65
}
66