Passed
Push — PSR-11-2 ( dfb1a3...d7a3e8 )
by Nikolaos
04:33
created

ValueObject   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
eloc 20
c 2
b 0
f 0
dl 0
loc 114
ccs 27
cts 27
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getWithDefault() 0 7 2
A merge() 0 8 1
A set() 0 9 2
A has() 0 3 1
A clear() 0 3 1
A remove() 0 3 1
A get() 0 7 2
A count() 0 3 1
1
<?php
2
3
/**
4
 * This file is part of the Phalcon Framework.
5
 *
6
 * (c) Phalcon Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 *
11
 * Implementation of this file has been influenced by AuraPHP
12
 *
13
 * @link    https://github.com/auraphp/Aura.Di
14
 * @license https://github.com/auraphp/Aura.Di/blob/4.x/LICENSE
15
 */
16
17
declare(strict_types=1);
18
19
namespace Phalcon\Container\Resolver;
20
21
use Phalcon\Container\Exception;
22
use Phalcon\Container\Exception\NoSuchProperty;
23
24
use function array_key_exists;
25
26
/**
27
 * Class ArrayObject
28
 *
29
 * @property array $store
30
 */
31
class ValueObject
32
{
33
    /**
34
     * @var array
35
     */
36
    protected $store = [];
37
38
    /**
39
     * Clears the internal collection
40
     */
41 1
    public function clear(): void
42
    {
43 1
        $this->store = [];
44 1
    }
45
46
    /**
47
     * Count elements of an object
48
     *
49
     * @link https://php.net/manual/en/countable.count.php
50
     */
51 8
    public function count(): int
52
    {
53 8
        return count($this->store);
54
    }
55
56
    /**
57
     * Get the element from the collection
58
     *
59
     * @param mixed $element
60
     *
61
     * @return mixed
62
     * @throws NoSuchProperty
63
     */
64 51
    public function get($element)
65
    {
66 51
        if (!$this->has($element)) {
67 1
            Exception::noSuchProperty($element);
68
        }
69
70 50
        return $this->store[$element];
71
    }
72
73
    /**
74
     * Get the element from the collection
75
     *
76
     * @param mixed      $element
77
     * @param mixed|null $defaultValue
78
     *
79
     * @return mixed|null
80
     */
81 1
    public function getWithDefault($element, $defaultValue = null)
82
    {
83 1
        if (!$this->has($element)) {
84 1
            return $defaultValue;
85
        }
86
87 1
        return $this->store[$element];
88
    }
89
90
    /**
91
     * Get the element from the collection
92
     *
93
     * @param mixed $element
94
     *
95
     * @return bool
96
     */
97 53
    public function has($element): bool
98
    {
99 53
        return array_key_exists($element, $this->store);
100
    }
101
102
    /**
103
     * @param mixed $element
104
     * @param array $data
105
     *
106
     * @return array
107
     */
108 27
    public function merge($element, array $data): array
109
    {
110 27
        $this->store[$element] = array_replace(
111 27
            ($this->store[$element] ?? []),
112
            $data
113
        );
114
115 27
        return $this->store[$element];
116
    }
117
118
    /**
119
     * Delete the element from the collection
120
     *
121
     * @param mixed $element
122
     */
123 2
    public function remove($element): void
124
    {
125 2
        unset($this->store[$element]);
126 2
    }
127
128
    /**
129
     * Set an element in the collection
130
     *
131
     * @param mixed $element
132
     * @param mixed $value
133
     *
134
     * @return ValueObject
135
     */
136 59
    public function set($element, $value): ValueObject
137
    {
138 59
        if (is_array($value)) {
139 27
            $this->merge($element, $value);
140
        } else {
141 50
            $this->store[$element] = $value;
142
        }
143
144 59
        return $this;
145
    }
146
}
147