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)
|
|
|
|
|
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)
|
|
|
|
|
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
|
|
|
|
|
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));
|
|
|
|
|
193
|
|
|
}
|
194
|
|
|
|
195
|
|
|
/**
|
196
|
|
|
* @return mixed
|
197
|
|
|
*/
|
198
|
3 |
|
public function getLastKey()
|
199
|
|
|
{
|
200
|
3 |
|
return end(array_keys($this->value));
|
|
|
|
|
201
|
|
|
}
|
202
|
|
|
|
203
|
|
|
/**
|
204
|
|
|
* @return bool
|
205
|
|
|
*/
|
206
|
2 |
|
public function isLast()
|
207
|
|
|
{
|
208
|
2 |
|
return $this->key() === $this->getLastKey();
|
209
|
|
|
}
|
210
|
|
|
|
211
|
|
|
/**
|
212
|
|
|
* @param ValueObject $object
|
213
|
|
|
* @return bool
|
214
|
|
|
*/
|
215
|
2 |
|
public function equals(ValueObject $object)
|
|
|
|
|
216
|
|
|
{
|
217
|
2 |
|
if (! $object instanceof ArrayVO) {
|
218
|
1 |
|
throw new InvalidTypeException($object, [ ArrayVO::class ]);
|
219
|
|
|
}
|
220
|
|
|
|
221
|
1 |
|
return parent::equals($object);
|
222
|
|
|
}
|
223
|
|
|
|
224
|
|
|
}
|
225
|
|
|
|
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.