1
|
|
|
<?php namespace Chekote\NounStore; |
2
|
|
|
|
3
|
|
|
use InvalidArgumentException; |
4
|
|
|
use OutOfBoundsException; |
5
|
|
|
use RuntimeException; |
6
|
|
|
|
7
|
|
|
class Store |
8
|
|
|
{ |
9
|
|
|
/** @var array */ |
10
|
|
|
protected $nouns; |
11
|
|
|
|
12
|
|
|
const FIRST_ORDINAL = 'st'; |
13
|
|
|
const SECOND_ORDINAL = 'nd'; |
14
|
|
|
const THIRD_ORDINAL = 'rd'; |
15
|
|
|
const FOURTH_THROUGH_NINTH_ORDINAL = 'th'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Asserts that a value has been stored for the specified key. |
19
|
|
|
* |
20
|
|
|
* @param string $key The key to check. @see self::get() for formatting options. |
21
|
|
|
* @param int $index [optional] The index of the key entry to check. If not specified, the |
22
|
|
|
* method will ensure that at least one item is stored for the key. |
23
|
|
|
* @throws OutOfBoundsException if a value has not been stored for the specified key. |
24
|
|
|
* @throws InvalidArgumentException if both an $index and $key are provided, but the $key contains an nth value |
25
|
|
|
* that does not match the index. |
26
|
|
|
* @return mixed The value. |
27
|
|
|
*/ |
28
|
|
|
public function assertKeyExists($key, $index = null) |
29
|
|
|
{ |
30
|
|
|
list($key, $index) = $this->parseKey($key, $index); |
31
|
|
|
|
32
|
|
|
if (!$this->keyExists($key, $index)) { |
33
|
|
|
throw new OutOfBoundsException("Entry '{$this->buildKey($key, $index)}' was not found in the store."); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return $this->get($key, $index); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Asserts that the key's value matches the specified value. |
41
|
|
|
* |
42
|
|
|
* @param string $key The key to check. @see self::get() for formatting options. |
43
|
|
|
* @param mixed $value The expected value. |
44
|
|
|
* @param int $index [optional] The index of the key entry to retrieve. If not specified, the |
45
|
|
|
* method will check the most recent value stored under the key. |
46
|
|
|
* @throws OutOfBoundsException If a value has not been stored for the specified key. |
47
|
|
|
* @throws InvalidArgumentException if both an $index and $key are provided, but the $key contains an nth value |
48
|
|
|
* that does not match the index. |
49
|
|
|
*/ |
50
|
|
|
public function assertKeyValueIs($key, $value, $index = null) |
51
|
|
|
{ |
52
|
|
|
list($key, $index) = $this->parseKey($key, $index); |
53
|
|
|
|
54
|
|
|
$this->assertKeyExists($key, $index); |
55
|
|
|
|
56
|
|
|
if ($this->get($key, $index) != $value) { |
57
|
|
|
throw new RuntimeException( |
58
|
|
|
"Entry '{$this->buildKey($key, $index)}' does not match '" . print_r($value, true) . "'" |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Asserts that the key's value contains the specified string. |
65
|
|
|
* |
66
|
|
|
* @param string $key The key to check. @see self::get() for formatting options. |
67
|
|
|
* @param string $value The value expected to be contained within the key's value. |
68
|
|
|
* @param int $index [optional] The index of the key entry to retrieve. If not specified, the |
69
|
|
|
* method will check the most recent value stored under the key. |
70
|
|
|
* @throws OutOfBoundsException If a value has not been stored for the specified key. |
71
|
|
|
* @throws InvalidArgumentException if both an $index and $key are provided, but the $key contains an nth value |
72
|
|
|
* that does not match the index. |
73
|
|
|
*/ |
74
|
|
|
public function assertKeyValueContains($key, $value, $index = null) |
75
|
|
|
{ |
76
|
|
|
list($key, $index) = $this->parseKey($key, $index); |
77
|
|
|
|
78
|
|
|
$this->assertKeyExists($key, $index); |
79
|
|
|
|
80
|
|
|
if (!$this->keyValueContains($key, $value, $index)) { |
81
|
|
|
throw new RuntimeException( |
82
|
|
|
"Entry '{$this->buildKey($key, $index)}' does not contain '$value'" |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Removes all entries from the store. |
89
|
|
|
* |
90
|
|
|
* @return void |
91
|
|
|
*/ |
92
|
|
|
public function reset() |
93
|
|
|
{ |
94
|
|
|
$this->nouns = []; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Retrieves a value for the specified key. |
99
|
|
|
* |
100
|
|
|
* Each key is actually a collection. If you do not specify which item in the collection you want, |
101
|
|
|
* the method will return the most recent entry. You can specify the entry you want by either |
102
|
|
|
* using the plain english 1st, 2nd, 3rd etc in the $key param, or by specifying 0, 1, 2 etc in |
103
|
|
|
* the $index param. For example: |
104
|
|
|
* |
105
|
|
|
* Retrieve the most recent entry "Thing" collection: |
106
|
|
|
* retrieve("Thing") |
107
|
|
|
* |
108
|
|
|
* Retrieve the 1st entry in the "Thing" collection: |
109
|
|
|
* retrieve("1st Thing") |
110
|
|
|
* retrieve("Thing", 0) |
111
|
|
|
* |
112
|
|
|
* Retrieve the 3rd entry in the "Thing" collection: |
113
|
|
|
* retrieve("3rd Thing") |
114
|
|
|
* retrieve("Thing", 2) |
115
|
|
|
* |
116
|
|
|
* Please note: The nth value in the string key is indexed from 1. In that "1st" is the first item stored. |
117
|
|
|
* The index parameter is indexed from 0. In that 0 is the first item stored. |
118
|
|
|
* |
119
|
|
|
* Please Note: If you specify both an $index param and an nth in the $key, they must both reference the same index. |
120
|
|
|
* If they do not, the method will throw an InvalidArgumentException. |
121
|
|
|
* |
122
|
|
|
* retrieve("1st Thing", 1); |
123
|
|
|
* |
124
|
|
|
* @param string $key The key to retrieve the value for. Can be prefixed with an nth descriptor. |
125
|
|
|
* @param int $index [optional] The index of the key entry to retrieve. If not specified, the |
126
|
|
|
* method will return the most recent value stored under the key. |
127
|
|
|
* @throws InvalidArgumentException if both an $index and $key are provided, but the $key contains an nth value |
128
|
|
|
* that does not match the index. |
129
|
|
|
* @return mixed The value, or null if no value exists for the specified key/index combination. |
130
|
|
|
*/ |
131
|
|
|
public function get($key, $index = null) |
132
|
|
|
{ |
133
|
|
|
list($key, $index) = $this->parseKey($key, $index); |
134
|
|
|
|
135
|
|
|
if (!$this->keyExists($key, $index)) { |
136
|
|
|
return null; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $index !== null ? $this->nouns[$key][$index] : end($this->nouns[$key]); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Retrieves all values for the specified key. |
144
|
|
|
* |
145
|
|
|
* @param string $key The key to retrieve the values for. |
146
|
|
|
* @throws OutOfBoundsException if the specified $key does not exist in the store. |
147
|
|
|
* @return array The values. |
148
|
|
|
*/ |
149
|
|
|
public function getAll($key) |
150
|
|
|
{ |
151
|
|
|
if (!isset($this->nouns[$key])) { |
152
|
|
|
throw new OutOfBoundsException("'$key' does not exist in the store"); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $this->nouns[$key]; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Determines if a value has been stored for the specified key. |
160
|
|
|
* |
161
|
|
|
* @param string $key The key to check. |
162
|
|
|
* @param int $index [optional] The index of the key entry to check. If not specified, the |
163
|
|
|
* method will ensure that at least one item is stored for the key. |
164
|
|
|
* @throws InvalidArgumentException if both an $index and $key are provided, but the $key contains an nth value |
165
|
|
|
* that does not match the index. |
166
|
|
|
* @return bool True if the a value has been stored, false if not. |
167
|
|
|
*/ |
168
|
|
|
public function keyExists($key, $index = null) |
169
|
|
|
{ |
170
|
|
|
list($key, $index) = $this->parseKey($key, $index); |
171
|
|
|
|
172
|
|
|
return $index !== null ? isset($this->nouns[$key][$index]) : isset($this->nouns[$key]); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Asserts that the key's value contains the specified string. |
177
|
|
|
* |
178
|
|
|
* @param string $key The key to check. @see self::get() for formatting options. |
179
|
|
|
* @param string $value The value expected to be contained within the key's value. |
180
|
|
|
* @param int $index [optional] The index of the key entry to retrieve. If not specified, the |
181
|
|
|
* method will check the most recent value stored under the key. |
182
|
|
|
* @throws InvalidArgumentException if both an $index and $key are provided, but the $key contains an nth value |
183
|
|
|
* that does not match the index. |
184
|
|
|
* @return bool True if the key's value contains the specified string, false if not. |
185
|
|
|
*/ |
186
|
|
|
public function keyValueContains($key, $value, $index = null) { |
187
|
|
|
list($key, $index) = $this->parseKey($key, $index); |
188
|
|
|
|
189
|
|
|
$actual = $this->get($key, $index); |
190
|
|
|
|
191
|
|
|
return is_string($actual) && strpos($actual, $value) !== false; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Stores a value for the specified key. |
196
|
|
|
* |
197
|
|
|
* @param string $key The key to store the value under. |
198
|
|
|
* @param mixed $value The value to store. |
199
|
|
|
*/ |
200
|
|
|
public function set($key, $value) |
201
|
|
|
{ |
202
|
|
|
$this->nouns[$key][] = $value; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Parses a key into the separate key and index value. |
207
|
|
|
* |
208
|
|
|
* @example parseKey("Item"): ["Item", null] |
209
|
|
|
* @example parseKey("Item", 1): ["Item", 1] |
210
|
|
|
* @example parseKey("1st Item"): ["Item", 0] |
211
|
|
|
* @example parseKey("2nd Item"): ["Item", 1] |
212
|
|
|
* @example parseKey("3rd Item"): ["Item", 2] |
213
|
|
|
* |
214
|
|
|
* @param string $key the key to parse. |
215
|
|
|
* @param int $index [optional] the index to return if the key does not contain one. |
216
|
|
|
* @throws InvalidArgumentException if both an $index and $key are provided, but the $key contains an nth value |
217
|
|
|
* that does not match the index. |
218
|
|
|
* @return array a tuple, the 1st being the key with the nth removed, and the 2nd being the |
219
|
|
|
* index. |
220
|
|
|
*/ |
221
|
|
|
protected function parseKey($key, $index = null) |
222
|
|
|
{ |
223
|
|
|
if (preg_match('/^([1-9][0-9]*)(?:st|nd|rd|th) (.+)$/', $key, $matches)) { |
224
|
|
|
if ($index !== null && $index != $matches[1] - 1) { |
225
|
|
|
throw new InvalidArgumentException( |
226
|
|
|
"$index was provided for index param when key '$key' contains an nth value, but they do not match" |
227
|
|
|
); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
$index = $matches[1] - 1; |
231
|
|
|
$key = $matches[2]; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
return [$key, $index]; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Builds a key from it's separate key and index values. |
239
|
|
|
* |
240
|
|
|
* @example buildKey("Item", null): "Item" |
241
|
|
|
* @example buildKey("Item", 0): "1st Item" |
242
|
|
|
* @example buildKey("Item", 1): "2nd Item" |
243
|
|
|
* @example buildKey("Item", 2): "3rd Item" |
244
|
|
|
* |
245
|
|
|
* @param string $key The key to check. |
246
|
|
|
* @param int $index The index (zero indexed) value for the key. If not specified, the method |
247
|
|
|
* will not add an index notation to the key. |
248
|
|
|
* @throws InvalidArgumentException if $key is not a string. |
249
|
|
|
* @throws InvalidArgumentException if $index is not an int. |
250
|
|
|
* @return string the key with the index, or just the key if index is null. |
251
|
|
|
*/ |
252
|
|
|
protected function buildKey($key, $index) |
253
|
|
|
{ |
254
|
|
|
if ($index === null) { |
255
|
|
|
return $key; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
$nth = $index + 1; |
259
|
|
|
|
260
|
|
|
return $nth . $this->getOrdinal($nth) . ' ' . $key; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Provides the ordinal notation for the specified nth number. |
265
|
|
|
* |
266
|
|
|
* @param int $nth the number to determine the ordinal for |
267
|
|
|
* @return string the ordinal |
268
|
|
|
*/ |
269
|
|
|
protected function getOrdinal($nth) |
270
|
|
|
{ |
271
|
|
|
switch (substr($nth, -1)) { |
272
|
|
|
case 1: |
273
|
|
|
$ordinal = self::FIRST_ORDINAL; |
274
|
|
|
break; |
275
|
|
|
case 2: |
276
|
|
|
$ordinal = self::SECOND_ORDINAL; |
277
|
|
|
break; |
278
|
|
|
case 3: |
279
|
|
|
$ordinal = self::THIRD_ORDINAL; |
280
|
|
|
break; |
281
|
|
|
default: |
282
|
|
|
$ordinal = self::FOURTH_THROUGH_NINTH_ORDINAL; |
283
|
|
|
break; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
return $ordinal; |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
|