Test Failed
Pull Request — main (#19)
by Anatoly
04:48 queued 02:08
created

ObjectCollection::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
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
 * @template T
30
 */
31
abstract class ObjectCollection implements ObjectCollectionInterface, JsonSerializable
32
{
33
34
    /**
35
     * The collection type
36
     *
37
     * @var class-string<T>
38
     */
39
    public const T = null;
40
41
    /**
42
     * The collection objects
43
     *
44
     * @var array<array-key, T>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, T> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, T>.
Loading history...
45
     */
46
    private $objects = [];
47
48
    /**
49
     * Gets the collection type
50
     *
51
     * @return class-string<T>
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<T> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<T>.
Loading history...
52 4
     *
53
     * @throws RuntimeException
54 4
     *         If the called class doesn't contain the T constant.
55 1
     */
56
    final public function getItemClassName() : string
57
    {
58
        if (null === static::T) {
0 ignored issues
show
introduced by
The condition null === static::T is always true.
Loading history...
59
            throw new RuntimeException(sprintf(
60
                'The %s collection must contain the T constant.',
61 3
                static::class
62
            ));
63
        }
64
65
        return static::T;
66
    }
67 4
68
    /**
69 4
     * Checks by the given key if an object exists in the collection
70
     *
71 3
     * @param array-key $key
0 ignored issues
show
Documentation Bug introduced by
The doc comment array-key at position 0 could not be parsed: Unknown type name 'array-key' at position 0 in array-key.
Loading history...
72 1
     *
73
     * @return bool
74
     */
75
    public function has($key) : bool
76
    {
77
        return isset($this->objects[$key]);
78
    }
79 2
80
    /**
81
     * Adds the given object to the collection with the given key
82
     *
83
     * @param array-key $key
0 ignored issues
show
Documentation Bug introduced by
The doc comment array-key at position 0 could not be parsed: Unknown type name 'array-key' at position 0 in array-key.
Loading history...
84
     * @param T $object
85 1
     *
86
     * @return void
87 1
     *
88
     * @throws InvalidArgumentException
89
     *         If the object cannot be added to the collection.
90
     */
91
    final public function add($key, object $object) : void
92
    {
93 2
        $type = $this->getItemClassName();
94
95 2
        if (!($object instanceof $type)) {
96
            throw new InvalidArgumentException(sprintf(
97
                'The %s collection can contain the %s objects only.',
98
                static::class,
99
                $type
100
            ));
101 1
        }
102
103 1
        $this->objects[$key] = $object;
104
    }
105
106
    /**
107
     * Gets an object from the collection by the given key
108
     *
109
     * @param array-key $key
0 ignored issues
show
Documentation Bug introduced by
The doc comment array-key at position 0 could not be parsed: Unknown type name 'array-key' at position 0 in array-key.
Loading history...
110
     *
111 1
     * @return T|null
112
     */
113 1
    final public function get($key) : ?object
114
    {
115
        return $this->objects[$key] ?? null;
116
    }
117
118
    /**
119
     * Gets all objects from the collection
120
     *
121
     * @return array<array-key, T>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, T> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, T>.
Loading history...
122
     */
123
    final public function all() : array
124
    {
125
        return $this->objects;
126
    }
127
128
    /**
129
     * Checks if the collection is empty
130
     *
131
     * @return bool
132
     */
133
    final public function isEmpty() : bool
134
    {
135
        return [] === $this->objects;
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     *
141
     * @since 2.3.0
142
     */
143
    public function jsonSerialize() : array
144
    {
145
        return $this->objects;
146
    }
147
}
148