|
1
|
|
|
<?php namespace Chekote\NounStore; |
|
2
|
|
|
|
|
3
|
|
|
use InvalidArgumentException; |
|
4
|
|
|
use OutOfBoundsException; |
|
5
|
|
|
|
|
6
|
|
|
class Store |
|
7
|
|
|
{ |
|
8
|
|
|
/** @var array */ |
|
9
|
|
|
protected $nouns; |
|
10
|
|
|
|
|
11
|
|
|
/** @var Key */ |
|
12
|
|
|
protected $keyService; |
|
13
|
|
|
|
|
14
|
27 |
|
public function __construct(Key $keyService = null) |
|
15
|
|
|
{ |
|
16
|
27 |
|
$this->keyService = $keyService ?: Key::getInstance(); |
|
17
|
27 |
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Removes all entries from the store. |
|
21
|
|
|
* |
|
22
|
|
|
* @return void |
|
23
|
|
|
*/ |
|
24
|
1 |
|
public function reset() |
|
25
|
|
|
{ |
|
26
|
1 |
|
$this->nouns = []; |
|
27
|
1 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Retrieves a value for the specified key. |
|
31
|
|
|
* |
|
32
|
|
|
* Each key is actually a collection. If you do not specify which item in the collection you want, |
|
33
|
|
|
* the method will return the most recent entry. You can specify the entry you want by either |
|
34
|
|
|
* using the plain english 1st, 2nd, 3rd etc in the $key param, or by specifying 0, 1, 2 etc in |
|
35
|
|
|
* the $index param. For example: |
|
36
|
|
|
* |
|
37
|
|
|
* Retrieve the most recent entry "Thing" collection: |
|
38
|
|
|
* retrieve("Thing") |
|
39
|
|
|
* |
|
40
|
|
|
* Retrieve the 1st entry in the "Thing" collection: |
|
41
|
|
|
* retrieve("1st Thing") |
|
42
|
|
|
* retrieve("Thing", 0) |
|
43
|
|
|
* |
|
44
|
|
|
* Retrieve the 3rd entry in the "Thing" collection: |
|
45
|
|
|
* retrieve("3rd Thing") |
|
46
|
|
|
* retrieve("Thing", 2) |
|
47
|
|
|
* |
|
48
|
|
|
* Please note: The nth value in the string key is indexed from 1. In that "1st" is the first item stored. |
|
49
|
|
|
* The index parameter is indexed from 0. In that 0 is the first item stored. |
|
50
|
|
|
* |
|
51
|
|
|
* Please Note: If you specify both an $index param and an nth in the $key, they must both reference the same index. |
|
52
|
|
|
* If they do not, the method will throw an InvalidArgumentException. |
|
53
|
|
|
* |
|
54
|
|
|
* retrieve("1st Thing", 1); |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $key The key to retrieve the value for. Can be prefixed with an nth descriptor. |
|
57
|
|
|
* @param int $index [optional] The index of the key entry to retrieve. If not specified, the |
|
58
|
|
|
* method will return the most recent value stored under the key. |
|
59
|
|
|
* @throws InvalidArgumentException if both an $index and $key are provided, but the $key contains an nth value |
|
60
|
|
|
* that does not match the index. |
|
61
|
|
|
* @return mixed The value, or null if no value exists for the specified key/index combination. |
|
62
|
|
|
*/ |
|
63
|
16 |
|
public function get($key, $index = null) |
|
64
|
|
|
{ |
|
65
|
16 |
|
list($key, $index) = $this->keyService->parse($key, $index); |
|
66
|
|
|
|
|
67
|
15 |
|
if (!$this->keyExists($key, $index)) { |
|
68
|
5 |
|
return; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
10 |
|
return $index !== null ? $this->nouns[$key][$index] : end($this->nouns[$key]); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Retrieves all values for the specified key. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $key The key to retrieve the values for. |
|
78
|
|
|
* @throws OutOfBoundsException if the specified $key does not exist in the store. |
|
79
|
|
|
* @return array The values. |
|
80
|
|
|
*/ |
|
81
|
2 |
|
public function getAll($key) |
|
82
|
|
|
{ |
|
83
|
2 |
|
if (!isset($this->nouns[$key])) { |
|
84
|
1 |
|
throw new OutOfBoundsException("'$key' does not exist in the store"); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
1 |
|
return $this->nouns[$key]; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Determines if a value has been stored for the specified key. |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $key The key to check. |
|
94
|
|
|
* @param int $index [optional] The index of the key entry to check. If not specified, the |
|
95
|
|
|
* method will ensure that at least one item is stored for the key. |
|
96
|
|
|
* @throws InvalidArgumentException if both an $index and $key are provided, but the $key contains an nth value |
|
97
|
|
|
* that does not match the index. |
|
98
|
|
|
* @return bool True if the a value has been stored, false if not. |
|
99
|
|
|
*/ |
|
100
|
20 |
|
public function keyExists($key, $index = null) |
|
101
|
|
|
{ |
|
102
|
20 |
|
list($key, $index) = $this->keyService->parse($key, $index); |
|
103
|
|
|
|
|
104
|
19 |
|
return $index !== null ? isset($this->nouns[$key][$index]) : isset($this->nouns[$key]); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Asserts that the key's value contains the specified string. |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $key The key to check. @see self::get() for formatting options. |
|
111
|
|
|
* @param string $value The value expected to be contained within the key's value. |
|
112
|
|
|
* @param int $index [optional] The index of the key entry to retrieve. If not specified, the |
|
113
|
|
|
* method will check the most recent value stored under the key. |
|
114
|
|
|
* @throws InvalidArgumentException if both an $index and $key are provided, but the $key contains an nth value |
|
115
|
|
|
* that does not match the index. |
|
116
|
|
|
* @return bool True if the key's value contains the specified string, false if not. |
|
117
|
|
|
*/ |
|
118
|
10 |
|
public function keyValueContains($key, $value, $index = null) |
|
119
|
|
|
{ |
|
120
|
10 |
|
list($key, $index) = $this->keyService->parse($key, $index); |
|
121
|
|
|
|
|
122
|
9 |
|
$actual = $this->get($key, $index); |
|
123
|
|
|
|
|
124
|
9 |
|
return is_string($actual) && strpos($actual, $value) !== false; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Stores a value for the specified key. |
|
129
|
|
|
* |
|
130
|
|
|
* @param string $key The key to store the value under. |
|
131
|
|
|
* @param mixed $value The value to store. |
|
132
|
|
|
*/ |
|
133
|
27 |
|
public function set($key, $value) |
|
134
|
|
|
{ |
|
135
|
27 |
|
$this->nouns[$key][] = $value; |
|
136
|
27 |
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|