Passed
Push — master ( 21a5dc...1ca6d2 )
by Nate
40s
created

PropertyCollection::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
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 IteratorAggregate;
13
14
/**
15
 * Class PropertyCollection
16
 *
17
 * A collection of [@see Property] objects
18
 *
19
 * @author Nate Brunette <[email protected]>
20
 */
21
final class PropertyCollection implements IteratorAggregate
22
{
23
    /**
24
     * Array of [@see Property] objects
25
     *
26
     * @var Property[]
27
     */
28
    private $elements = [];
29
30
    /**
31
     * @param Property $property
32
     * @return void
33
     */
34 8
    public function add(Property $property): void
35
    {
36 8
        $this->elements[$property->getSerializedName()] = $property;
37 8
    }
38
39
    /**
40
     * Get [@see Property] by property name
41
     *
42
     * @param string $propertyName
43
     * @return Property|null
44
     */
45 2
    public function getByName(string $propertyName): ?Property
46
    {
47 2
        foreach ($this->elements as $property) {
48 1
            if ($property->getName() === $propertyName) {
49 1
                return $property;
50
            }
51
        }
52
53 1
        return null;
54
    }
55
56
    /**
57
     * Get [@see Property] by serialized name
58
     *
59
     * @param string $name
60
     * @return Property|null
61
     */
62 5
    public function getBySerializedName(string $name): ?Property
63
    {
64 5
        if (!isset($this->elements[$name])) {
65 4
            return null;
66
        }
67
68 2
        return $this->elements[$name];
69
    }
70
71
    /**
72
     * Array of Property objects
73
     *
74
     * @return Property[]
75
     */
76 3
    public function toArray(): array
77
    {
78 3
        return \array_values($this->elements);
79
    }
80
81
    /**
82
     * Retrieve an external iterator
83
     *
84
     * @return ArrayIterator
85
     */
86 1
    public function getIterator(): ArrayIterator
87
    {
88 1
        return new ArrayIterator($this->toArray());
89
    }
90
}
91