Properties::offsetSet()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 8.8571
c 1
b 0
f 0
cc 6
eloc 9
nc 6
nop 2
crap 6
1
<?php
2
3
/**
4
 * This file is part of the Mediapart Selligent Client API
5
 *
6
 * CC BY-NC-SA <https://github.com/mediapart/selligent>
7
 *
8
 * For the full license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Mediapart\Selligent;
13
14
use Mediapart\Selligent\Property;
15
16
/**
17
 *
18
 */
19
class Properties implements \ArrayAccess, \IteratorAggregate
20
{
21
    /**
22
     * @var Array[Property]
23
     */
24
    public $Property = [];
25
26
    /**
27
     * @return boolean 
28
     */
29 6
    public function offsetExists($offset)
30
    {
31 6
        foreach ($this->Property as $property) {
32 6
            if ($offset === $property->getKey()) {
33 6
                return true;
34
            }
35
        }
36 2
        return false;
37
    }
38
39
    /**
40
     * @return mixed 
41
     */
42 6
    public function offsetGet($offset)
43
    {
44 6
        foreach ($this->Property as $property) {
45 6
            if ($offset === $property->getKey()) {
46 6
                return $property->getValue();
47
            }
48
        }
49 1
        return null;
50
    }
51
52
    /**
53
     * @return void 
54
     */
55 7
    public function offsetSet($offset, $value)
56
    {
57 7
        if (is_null($offset) && $value instanceof Property) {
58 1
            $offset = $value->getKey();
59
        }
60
61 7
        foreach ($this->Property as $i => $property) {
62 7
            if ($offset === $property->getKey()) {
63 7
                return $this->Property[$i]->setValue($value);
64
            }
65
        }
66
67 7
        return $this->Property[] = new Property(
68 7
            $offset,
69 7
            $value instanceof Property ? $value->getValue() : $value
70
        );
71
    }
72
73
    /**
74
     * @return void 
75
     */
76 1
    public function offsetUnset($offset)
77
    {
78 1
        foreach ($this->Property as $i => $property) {
79 1
            if ($offset === $property->getKey()) {
80 1
                unset($this->Property[$i]);
81
            }
82
        }
83 1
    }
84
85
    /**
86
     * @return \ArrayIterator
87
     */
88 3
    public function getIterator()
89
    {
90 3
        return new \ArrayIterator($this->Property);
91
    }
92
}
93