Passed
Push — main ( 973bee...d57b17 )
by Anatoly
01:56 queued 12s
created

ObjectCollection::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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 InvalidArgumentException;
18
use JsonSerializable;
19
use RuntimeException;
20
21
/**
22
 * Import functions
23
 */
24
use function sprintf;
25
26
/**
27
 * ObjectCollection
28
 */
29
abstract class ObjectCollection implements ObjectCollectionInterface, JsonSerializable
30
{
31
32
    /**
33
     * The type of objects in the collection
34
     *
35
     * @var class-string
36
     */
37
    public const T = null;
38
39
    /**
40
     * The collection objects
41
     *
42
     * @var array<array-key, object>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, object> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, object>.
Loading history...
43
     */
44
    private $objects = [];
45
46
    /**
47
     * {@inheritdoc}
48
     *
49
     * @throws RuntimeException
50
     *         If the called class doesn't contain the T constant.
51
     */
52 4
    final public function getItemClassName() : string
53
    {
54 4
        if (null === static::T) {
0 ignored issues
show
introduced by
The condition null === static::T is always true.
Loading history...
55 1
            throw new RuntimeException(sprintf(
56
                'The %s collection must contain the T constant.',
57
                static::class
58
            ));
59
        }
60
61 3
        return static::T;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 4
    final public function add($key, object $object) : void
68
    {
69 4
        $type = $this->getItemClassName();
70
71 3
        if (!($object instanceof $type)) {
72 1
            throw new InvalidArgumentException(sprintf(
73
                'The %s collection can contain the %s objects only.',
74
                static::class,
75
                $type
76
            ));
77
        }
78
79 2
        $this->objects[$key] = $object;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 1
    final public function get($key) : ?object
86
    {
87 1
        return $this->objects[$key] ?? null;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 2
    final public function all() : array
94
    {
95 2
        return $this->objects;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 1
    final public function isEmpty() : bool
102
    {
103 1
        return [] === $this->objects;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     *
109
     * @since 2.3.0
110
     */
111 1
    public function jsonSerialize() : array
112
    {
113 1
        return $this->objects;
114
    }
115
}
116