Passed
Push — master ( 6b2979...464731 )
by Alex
04:00 queued 11s
created

ODataPropertyContent::offsetGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace POData\ObjectModel;
6
7
/**
8
 * Class ODataPropertyContent represents properties of a Complex type or entity element instance.
9
 */
10
class ODataPropertyContent implements \ArrayAccess, \Iterator, \Countable
11
{
12
    /**
13
     * The collection of properties.
14
     *
15
     * @var ODataProperty[]
16
     */
17
    private $properties = [];
18
19
    /**
20
     * ODataPropertyContent constructor.
21
     * @param ODataProperty[] $properties
22
     */
23
    public function __construct(array $properties)
24
    {
25
        $this->setPropertys($properties);
26
    }
27
28
    /**
29
     * @return ODataProperty[]
30
     */
31
    public function getPropertys(): array
32
    {
33
        return $this->properties;
34
    }
35
36
    /**
37
     * @param $newProperties ODataProperty[]
38
     * @return ODataPropertyContent
39
     */
40
    public function setPropertys(array $newProperties): self
41
    {
42
        assert(array_reduce($newProperties, function ($carry, $item) {
43
            return  $carry & $item instanceof ODataProperty;
44
        }, true));
45
        $this->properties = $newProperties;
46
        return $this;
47
    }
48
49
    public function offsetExists($offset): bool
50
    {
51
        return array_key_exists($offset, $this->properties);
52
    }
53
54
    public function offsetGet($offset): ODataProperty
55
    {
56
        return $this->properties[$offset];
57
    }
58
59
    public function offsetSet($offset, $value)
60
    {
61
        assert($value instanceof ODataProperty);
62
        null === $offset ? $this->properties[] = $value : $this->properties[$offset] = $value;
63
    }
64
65
    public function offsetUnset($offset)
66
    {
67
        unset($this->properties[$offset]);
68
    }
69
70
    public function current()
71
    {
72
        return current($this->properties);
73
    }
74
75
    public function next()
76
    {
77
        return next($this->properties);
78
    }
79
80
    public function key()
81
    {
82
        return key($this->properties);
83
    }
84
85
    public function valid()
86
    {
87
        return key($this->properties) !== null;
88
    }
89
90
    public function rewind()
91
    {
92
        return reset($this->properties);
93
    }
94
95
    /**
96
     * Count elements of an object.
97
     * @see https://php.net/manual/en/countable.count.php
98
     * @return int The custom count as an integer.
99
     *             </p>
100
     *             <p>
101
     *             The return value is cast to an integer.
102
     * @since 5.1.0
103
     */
104
    public function count()
105
    {
106
        return count($this->properties);
107
    }
108
}
109