Passed
Push — master ( 37395a...ba940f )
by Nate
01:20 queued 11s
created

PropertyCollection::removeByName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * Copyright (c) Nate Brunette.
4
 * Distributed under the MIT License (http://opensource.org/licenses/MIT)
5
 */
6
7
declare(strict_types=1);
8
9
namespace Tebru\Gson\Internal\Data;
10
11
use ArrayIterator;
12
use Tebru\Gson\PropertyMetadata;
13
use Tebru\Gson\PropertyMetadataCollection;
14
15
/**
16
 * Class PropertyCollection
17
 *
18
 * A collection of [@see PropertyMetadata] objects
19
 *
20
 * @author Nate Brunette <[email protected]>
21
 */
22
final class PropertyCollection implements PropertyMetadataCollection
23
{
24
    /**
25
     * Array of [@see PropertyMetadata] objects
26
     *
27
     * @var PropertyMetadata[]
28
     */
29
    private $elements;
30
31
    /**
32
     * Constructor
33
     *
34
     * @param PropertyMetadata[] $elements
35
     */
36 13
    public function __construct(array $elements = [])
37
    {
38 13
        $this->elements = $elements;
39 13
    }
40
41
    /**
42
     * @param PropertyMetadata $property
43
     * @return void
44
     */
45 12
    public function add(PropertyMetadata $property): void
46
    {
47 12
        $this->elements[$property->getSerializedName()] = $property;
48 12
    }
49
50
    /**
51
     * Add a property to the collection
52
     *
53
     * @param PropertyMetadata $property
54
     * @return void
55
     */
56 1
    public function remove(PropertyMetadata $property): void
57
    {
58 1
        $this->removeBySerializedName($property->getSerializedName());
59 1
    }
60
61
    /**
62
     * Clear out collection
63
     */
64 1
    public function clear(): void
65
    {
66 1
        $this->elements = [];
67 1
    }
68
69
    /**
70
     * Get [@see PropertyMetadata] by property name
71
     *
72
     * @param string $propertyName
73
     * @return PropertyMetadata|null
74
     */
75 2
    public function getByName(string $propertyName): ?PropertyMetadata
76
    {
77 2
        foreach ($this->elements as $property) {
78 2
            if ($property->getName() === $propertyName) {
79 2
                return $property;
80
            }
81
        }
82
83 1
        return null;
84
    }
85
86
    /**
87
     * Remove property by name
88
     *
89
     * @param string $propertyName
90
     * @return void
91
     */
92 2
    public function removeByName(string $propertyName): void
93
    {
94 2
        $property = $this->getByName($propertyName);
95 2
        if ($property === null) {
96 1
            return;
97
        }
98
99 1
        $this->removeBySerializedName($property->getSerializedName());
100 1
    }
101
102
    /**
103
     * Get [@see Property] by serialized name
104
     *
105
     * @param string $name
106
     * @return PropertyMetadata|null
107
     */
108 5
    public function getBySerializedName(string $name): ?PropertyMetadata
109
    {
110 5
        if (!isset($this->elements[$name])) {
111 4
            return null;
112
        }
113
114 2
        return $this->elements[$name];
115
    }
116
117
    /**
118
     * Remove property by serialized name
119
     *
120
     * @param string $name
121
     * @return void
122
     */
123 4
    public function removeBySerializedName(string $name): void
124
    {
125 4
        if (!isset($this->elements[$name])) {
126 1
            return;
127
        }
128
129 3
        unset($this->elements[$name]);
130 3
    }
131
132
    /**
133
     * Array of Property objects
134
     *
135
     * @return PropertyMetadata[]
136
     */
137 8
    public function toArray(): array
138
    {
139 8
        return \array_values($this->elements);
140
    }
141
142
    /**
143
     * Retrieve an external iterator
144
     *
145
     * @return ArrayIterator|PropertyMetadata[]
146
     */
147 7
    public function getIterator(): ArrayIterator
148
    {
149 7
        return new ArrayIterator($this->toArray());
150
    }
151
}
152