Passed
Branch 2.0 (d24509)
by Donald
02:04
created

Store::keyValueContains()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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