Completed
Push — master ( 8f2777...1851a9 )
by Adam
03:05
created

ArrayVO   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 211
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 91.07%

Importance

Changes 0
Metric Value
wmc 28
lcom 1
cbo 3
dl 0
loc 211
ccs 51
cts 56
cp 0.9107
rs 10
c 0
b 0
f 0

23 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __toString() 0 4 1
A toArray() 0 4 1
A fromArray() 0 4 1
A keyExists() 0 4 1
A dropFirst() 0 4 1
A dropLast() 0 4 1
A findJsonNotation() 0 4 1
A findArray() 0 4 1
A findArrayVO() 0 10 3
A getKey() 0 4 2
A current() 0 4 1
A key() 0 4 1
A valid() 0 4 2
A next() 0 5 1
A rewind() 0 5 1
A count() 0 4 1
A isEmpty() 0 4 1
A getFirstKey() 0 4 1
A getKeys() 0 4 1
A getLastKey() 0 4 1
A isLast() 0 4 1
A equals() 0 8 2
1
<?php
2
3
namespace BestServedCold\PhalueObjects;
4
5
use BestServedCold\PhalueObjects\Contract\Arrayable;
6
use BestServedCold\PhalueObjects\Contract\Countable;
7
use BestServedCold\PhalueObjects\Exception\InvalidTypeException;
8
use BestServedCold\PhalueObjects\Format\Json\Notation;
9
use Iterator;
10
11
/**
12
 * Class ArrayVO
13
 *
14
 * @package   BestServedCold\PhalueObjects\ValueObject
15
 * @author    Adam Lewis <[email protected]>
16
 * @copyright Copyright (c) 2015 Best Served Cold Media Limited
17
 * @license   http://http://opensource.org/licenses/GPL-3.0 GPL License
18
 * @link      http://bestservedcold.com
19
 * @since     0.0.1-alpha
20
 * @version   0.0.2-alpha
21
 */
22
class ArrayVO extends ValueObject implements Iterator, Arrayable, Countable
23
{
24
    /**
25
     * ArrayValueObject constructor.
26
     *
27
     * @param $value
28
     */
29 48
    public function __construct(array $value)
30 1
    {
31 48
        parent::__construct($value);
32 48
    }
33
34
    /**
35
     * @return string
36
     */
37 1
    public function __toString()
38
    {
39 1
        return (string) serialize($this->getValue());
40
    }
41
42
    /**
43
     * @return mixed
44
     */
45
    public function toArray()
46
    {
47
        return $this->getValue();
48
    }
49
50
    /**
51
     * @param array $array
52
     * @return static
53
     */
54 5
    public static function fromArray(array $array)
55
    {
56 5
        return new static($array);
57
    }
58
59
    /**
60
     * @param  $key
61
     * @return bool
62
     */
63 2
    public function keyExists($key)
0 ignored issues
show
Coding Style introduced by
function keyExists() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
64
    {
65 2
        return array_key_exists($key, $this->getValue());
66
    }
67
68
    /**
69
     * @return static
70
     */
71 1
    public function dropFirst()
72
    {
73 1
        return new static(array_slice($this->getValue(), 1, $this->count()));
74
    }
75
76
    /**
77
     * @return static
78
     */
79 1
    public function dropLast()
80
    {
81 1
        return new static(array_slice($this->getValue(), 0, -1));
82
    }
83
84
    /**
85
     * @param  Notation $notation
86
     * @return bool
87
     */
88 1
    public function findJsonNotation(Notation $notation)
0 ignored issues
show
Coding Style introduced by
function findJsonNotation() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
89
    {
90 1
        return $this->findArrayVO($notation->toArrayVO());
91
    }
92
93
    /**
94
     * @param  array $array
95
     * @return bool|mixed
96
     */
97 1
    public function findArray(array $array)
98
    {
99 1
        return $this->findArrayVO(ArrayVO::fromArray($array));
100
    }
101
102
    /**
103
     * Iterates through an array of values and finds a key.  If it makes it to the final value,
104
     * it returns what ever value matches that key.
105
     *
106
     * @param  ArrayValueObject $arrayValueObject
0 ignored issues
show
Documentation introduced by
Should the type for parameter $arrayValueObject not be ArrayVO?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
107
     * @return bool|mixed
108
     */
109 2
    public function findArrayVO(ArrayVO $arrayValueObject)
110
    {
111 2
        if ($key = $this->getKey($arrayValueObject->current())) {
112 2
            return $arrayValueObject->isLast() ? $key
113
                : ArrayVO::fromArray($key)
114
                    ->findArrayVO($arrayValueObject->dropFirst());
115
        }
116
117
        return false;
118
    }
119
120
    /**
121
     * @param  string|int $key
122
     * @return bool|mixed
123
     */
124 2
    public function getKey($key)
125
    {
126 2
        return $this->keyExists($key) ? $this->value[$key] : false;
127
    }
128
129
    /**
130
     * @return mixed
131
     */
132 3
    public function current()
133
    {
134 3
        return current($this->value);
135
    }
136
137
    /**
138
     * @return mixed
139
     */
140 2
    public function key()
141
    {
142 2
        return key($this->value);
143
    }
144
145
    /**
146
     * @return bool
147
     */
148 1
    public function valid()
149
    {
150 1
        return key($this->value) !== null && key($this->value) !== false;
151
    }
152
153
    /**
154
     * @return $this
155
     */
156 2
    public function next()
157
    {
158 2
        next($this->value);
159 2
        return $this;
160
    }
161
162
    /**
163
     * @return $this
164
     */
165 1
    public function rewind()
166
    {
167 1
        reset($this->value);
168 1
        return $this;
169
    }
170
171
    /**
172
     * @return int
173
     */
174 1
    public function count()
175
    {
176 1
        return count($this->getValue());
177
    }
178
179
    /**
180
     * @return bool
181
     */
182 1
    public function isEmpty()
183
    {
184 1
        return empty($this->value);
185
    }
186
187
    /**
188
     * @return mixed
189
     */
190 1
    public function getFirstKey()
191
    {
192 1
        return array_shift(array_keys($this->value));
0 ignored issues
show
Bug introduced by
array_keys($this->value) cannot be passed to array_shift() as the parameter $array expects a reference.
Loading history...
193
    }
194
195
    /**
196
     * @return array
197
     */
198 3
    public function getKeys()
199
    {
200 3
        return array_keys($this->value);
201
    }
202
203
    /**
204
     * @return mixed
205
     */
206 3
    public function getLastKey()
207
    {
208 3
        return end($this->getKeys());
0 ignored issues
show
Bug introduced by
$this->getKeys() cannot be passed to end() as the parameter $array expects a reference.
Loading history...
209
    }
210
211
    /**
212
     * @return bool
213
     */
214 2
    public function isLast()
215
    {
216 2
        return $this->key() === $this->getLastKey();
217
    }
218
219
    /**
220
     * @param  ValueObject $object
221
     * @return bool
222
     */
223 2
    public function equals(ValueObject $object)
0 ignored issues
show
Coding Style introduced by
function equals() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
224
    {
225 2
        if (! $object instanceof ArrayVO) {
226 1
            throw new InvalidTypeException($object, [ ArrayVO::class ]);
227
        }
228
229 1
        return parent::equals($object);
230
    }
231
232
}
233