1
|
|
|
<?php namespace Chekote\NounStore; |
2
|
|
|
|
3
|
|
|
use Illuminate\Support\Arr; |
4
|
|
|
use InvalidArgumentException; |
5
|
|
|
|
6
|
|
|
class Store |
7
|
|
|
{ |
8
|
|
|
/** @var Key */ |
9
|
|
|
protected $keyService; |
10
|
|
|
|
11
|
|
|
/** @var array */ |
12
|
|
|
protected $nouns; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @param Key $keyService the key service to use for parsing and building keys |
16
|
|
|
* @codeCoverageIgnore |
17
|
|
|
*/ |
18
|
|
|
public function __construct(Key $keyService = null) |
19
|
|
|
{ |
20
|
|
|
$this->keyService = $keyService ?: Key::getInstance(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Removes all entries from the store. |
25
|
|
|
* |
26
|
|
|
* @return void |
27
|
|
|
*/ |
28
|
1 |
|
public function reset() |
29
|
|
|
{ |
30
|
1 |
|
$this->nouns = []; |
31
|
1 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Retrieves a value for the specified key. |
35
|
|
|
* |
36
|
|
|
* Each key is actually a collection. If you do not specify which item in the collection you want, |
37
|
|
|
* the method will return the most recent entry. You can optionally specify the entry you want by |
38
|
|
|
* using the plain english 1st, 2nd, 3rd etc in the $key param. For example: |
39
|
|
|
* |
40
|
|
|
* Retrieve the most recent entry "Thing" collection: |
41
|
|
|
* retrieve("Thing") |
42
|
|
|
* |
43
|
|
|
* Retrieve the 1st entry in the "Thing" collection: |
44
|
|
|
* retrieve("1st Thing") |
45
|
|
|
* |
46
|
|
|
* Retrieve the 3rd entry in the "Thing" collection: |
47
|
|
|
* retrieve("3rd Thing") |
48
|
|
|
* |
49
|
|
|
* @see Key::build() |
50
|
|
|
* @see Key::parseNoun() |
51
|
|
|
* @param string $key The key to retrieve the value for. Supports nth notation. |
52
|
|
|
* @throws InvalidArgumentException if the key syntax is invalid. |
53
|
|
|
* @return mixed The value, or null if no value exists for the specified key/index combination. |
54
|
|
|
*/ |
55
|
|
|
public function get($key) |
56
|
|
|
{ |
57
|
|
|
$i = 0; |
58
|
|
|
|
59
|
|
|
return array_reduce( |
60
|
|
|
$this->keyService->parse($key), |
61
|
|
|
static function ($carry, $item) use (&$i) { |
62
|
|
|
list($noun, $index) = $item; |
63
|
|
|
|
64
|
|
|
$carry = data_get($carry, $noun); |
65
|
|
|
|
66
|
|
|
try { |
67
|
5 |
|
// Was a specific index requested? |
68
|
|
|
if ($index !== null) { |
69
|
5 |
|
// Yes, fetch the specific index |
70
|
|
|
return data_get($carry, $index); |
71
|
4 |
|
} else { |
72
|
1 |
|
// No, return the noun itself, or the latest noun if this is the |
73
|
|
|
// first component of the key, and the noun is a collection |
74
|
|
|
return $i === 0 && Arr::accessible($carry) ? end($carry) : $carry; |
|
|
|
|
75
|
3 |
|
} |
76
|
|
|
} finally { |
77
|
|
|
++$i; |
78
|
|
|
} |
79
|
|
|
}, |
80
|
|
|
$this->nouns |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
2 |
|
* Retrieves all values for the specified key. |
86
|
|
|
* |
87
|
2 |
|
* @param string $key The key to retrieve the values for. Does not support nth notation. |
88
|
1 |
|
* @return array The values, or an empty array if no value exists for the specified key. |
89
|
|
|
*/ |
90
|
|
|
public function getAll($key) |
91
|
1 |
|
{ |
92
|
|
|
return isset($this->nouns[$key]) ? $this->nouns[$key] : []; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Determines if a value has been stored for the specified key. |
97
|
|
|
* |
98
|
|
|
* @see Key::build() |
99
|
|
|
* @see Key::parseNoun() |
100
|
|
|
* @param string $key The key to check. Supports nth notation. |
101
|
|
|
* @throws InvalidArgumentException if the key syntax is invalid. |
102
|
|
|
* @return bool True if the a value has been stored, false if not. |
103
|
|
|
*/ |
104
|
5 |
|
public function keyExists($key) |
105
|
|
|
{ |
106
|
5 |
|
return $this->get($key) !== null; |
107
|
|
|
} |
108
|
4 |
|
|
109
|
|
|
/** |
110
|
|
|
* Asserts that the key's value contains the specified string. |
111
|
|
|
* |
112
|
|
|
* @see Key::build() |
113
|
|
|
* @see Key::parseNoun() |
114
|
|
|
* @param string $key The key to check. |
115
|
|
|
* @param string $value The value expected to be contained within the key's value. |
116
|
|
|
* @throws InvalidArgumentException if the key syntax is invalid. |
117
|
|
|
* @return bool True if the key's value contains the specified string, false if not. |
118
|
|
|
*/ |
119
|
|
|
public function keyValueContains($key, $value) |
120
|
|
|
{ |
121
|
|
|
$actual = $this->get($key); |
122
|
4 |
|
|
123
|
|
|
return is_string($actual) && strpos($actual, $value) !== false; |
124
|
4 |
|
} |
125
|
|
|
|
126
|
3 |
|
/** |
127
|
|
|
* Stores a value for the specified key. |
128
|
3 |
|
* |
129
|
|
|
* The specified value is added to the top of the "stack" for the specified key. |
130
|
|
|
* |
131
|
|
|
* @param string $key The key to store the value under. Does not support nth notation. |
132
|
|
|
* @param mixed $value The value to store. |
133
|
|
|
*/ |
134
|
|
|
public function set($key, $value) |
135
|
|
|
{ |
136
|
|
|
$this->nouns[$key][] = $value; |
137
|
2 |
|
} |
138
|
|
|
|
139
|
2 |
|
/** |
140
|
2 |
|
* Asserts that the key's value contains the specified class instance. |
141
|
|
|
* |
142
|
|
|
* @see Key::build() |
143
|
|
|
* @see Key::parseNoun() |
144
|
|
|
* @param string $key The key to check. |
145
|
|
|
* @param string $class The class instance expected to be contained within the key's value. |
146
|
|
|
* @return bool True if the key's value contains the specified class instance, false if not. |
147
|
|
|
* @throws InvalidArgumentException if the key syntax is invalid. |
148
|
|
|
*/ |
149
|
|
|
public function keyIsClass($key, $class) |
150
|
|
|
{ |
151
|
|
|
$actual = $this->get($key); |
152
|
|
|
|
153
|
|
|
return $actual instanceof $class; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|