Passed
Pull Request — master (#12)
by Donald
12:16
created

Store::parseKey()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

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