Passed
Pull Request — master (#269)
by Christopher
03:33
created

ODataPropertyContent::key()   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 0
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) { return  $carry & $item instanceof ODataProperty; }, true));
43
        $this->properties = $newProperties;
44
        return $this;
45
    }
46
47
    public function offsetExists($offset): bool
48
    {
49
        return array_key_exists($offset, $this->properties);
50
    }
51
52
    public function offsetGet($offset): ODataProperty
53
    {
54
        return $this->properties[$offset];
55
    }
56
57
    public function offsetSet($offset, $value)
58
    {
59
        assert($value instanceof ODataProperty);
60
        null === $offset ? $this->properties[] = $value : $this->properties[$offset] = $value;
61
    }
62
63
    public function offsetUnset($offset)
64
    {
65
        unset($this->properties[$offset]);
66
    }
67
68
    public function current()
69
    {
70
        return current($this->properties);
71
    }
72
73
    public function next()
74
    {
75
        return next($this->properties);
76
    }
77
78
    public function key()
79
    {
80
        return key($this->properties);
81
    }
82
83
    public function valid()
84
    {
85
        return key($this->properties) !== null;
86
    }
87
88
    public function rewind()
89
    {
90
        return reset($this->properties);
91
    }
92
93
    /**
94
     * Count elements of an object.
95
     * @see https://php.net/manual/en/countable.count.php
96
     * @return int The custom count as an integer.
97
     *             </p>
98
     *             <p>
99
     *             The return value is cast to an integer.
100
     * @since 5.1.0
101
     */
102
    public function count()
103
    {
104
        return count($this->properties);
105
    }
106
}
107