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