Completed
Push — master ( 6c79fa...0a3c44 )
by Bernhard
21:09 queued 18:24
created

ArrayStore::__construct()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 4
Bugs 1 Features 2
Metric Value
c 4
b 1
f 2
dl 0
loc 24
ccs 11
cts 11
cp 1
rs 8.9713
cc 3
eloc 15
nc 4
nop 2
crap 3
1
<?php
2
3
/*
4
 * This file is part of the webmozart/key-value-store package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webmozart\KeyValueStore;
13
14
use Webmozart\KeyValueStore\Api\CountableStore;
15
use Webmozart\KeyValueStore\Api\NoSuchKeyException;
16
use Webmozart\KeyValueStore\Api\SortableStore;
17
use Webmozart\KeyValueStore\Util\KeyUtil;
18
19
/**
20
 * A key-value store backed by a PHP array.
21
 *
22
 * The contents of the store are lost when the store is released from memory.
23
 *
24
 * @since  1.0
25
 *
26
 * @author Bernhard Schussek <[email protected]>
27
 */
28
class ArrayStore implements SortableStore, CountableStore
29
{
30
    /**
31
     * Flag: Enable serialization.
32
     */
33
    const SERIALIZE = 1;
34
35
    /**
36
     * @var array
37
     */
38
    private $array = array();
39
40
    /**
41
     * @var callable
42
     */
43
    private $serialize;
44
45
    /**
46
     * @var callable
47
     */
48
    private $unserialize;
49
50
    /**
51
     * Creates a new store.
52
     *
53
     * @param array $array The values to set initially in the store.
54
     */
55 192
    public function __construct(array $array = array(), $flags = 0)
56
    {
57 192
        if ($flags & self::SERIALIZE) {
58 93
            $this->serialize = array(
59
                'Webmozart\KeyValueStore\Util\Serializer',
60
                'serialize',
61
            );
62 93
            $this->unserialize = array(
63 93
                'Webmozart\KeyValueStore\Util\Serializer',
64
                'unserialize',
65
            );
66
        } else {
67
            $this->serialize = function ($value) {
68 67
                return $value;
69
            };
70 45
            $this->unserialize = function ($value) {
71 45
                return $value;
72
            };
73
        }
74
75 192
        foreach ($array as $key => $value) {
76 2
            $this->set($key, $value);
77
        }
78 192
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 132
    public function set($key, $value)
84
    {
85 132
        KeyUtil::validate($key);
86
87 128
        $this->array[$key] = call_user_func($this->serialize, $value);
88 128
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 12 View Code Duplication
    public function get($key, $default = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
    {
95 12
        KeyUtil::validate($key);
96
97 8
        if (!array_key_exists($key, $this->array)) {
98 2
            return $default;
99
        }
100
101 6
        return call_user_func($this->unserialize, $this->array[$key]);
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 60 View Code Duplication
    public function getOrFail($key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
    {
109 60
        KeyUtil::validate($key);
110
111 56
        if (!array_key_exists($key, $this->array)) {
112 2
            throw NoSuchKeyException::forKey($key);
113
        }
114
115 54
        return call_user_func($this->unserialize, $this->array[$key]);
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121 80
    public function getMultiple(array $keys, $default = null)
122
    {
123 80
        KeyUtil::validateMultiple($keys);
124
125 76
        $values = array();
126
127 76
        foreach ($keys as $key) {
128 76
            $values[$key] = array_key_exists($key, $this->array)
129 76
                ? call_user_func($this->unserialize, $this->array[$key])
130 76
                : $default;
131
        }
132
133 76
        return $values;
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139 60
    public function getMultipleOrFail(array $keys)
140
    {
141 60
        KeyUtil::validateMultiple($keys);
142
143 56
        $notFoundKeys = array_diff($keys, array_keys($this->array));
144
145 56
        if (count($notFoundKeys) > 0) {
146 2
            throw NoSuchKeyException::forKeys($notFoundKeys);
147
        }
148
149 54
        return $this->getMultiple($keys);
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155 20
    public function remove($key)
156
    {
157 20
        KeyUtil::validate($key);
158
159 16
        $removed = array_key_exists($key, $this->array);
160
161 16
        unset($this->array[$key]);
162
163 16
        return $removed;
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169 60
    public function exists($key)
170
    {
171 60
        KeyUtil::validate($key);
172
173 56
        return array_key_exists($key, $this->array);
174
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179 186
    public function clear()
180
    {
181 186
        $this->array = array();
182 186
    }
183
184
    /**
185
     * {@inheritdoc}
186
     */
187 24
    public function keys()
188
    {
189 24
        return array_keys($this->array);
190
    }
191
192
    /**
193
     * Returns the contents of the store as array.
194
     *
195
     * @return array The keys and values in the store.
196
     */
197
    public function toArray()
198
    {
199
        return array_map($this->unserialize, $this->array);
200
    }
201
202
    /**
203
     * {@inheritdoc}
204
     */
205 12
    public function sort($flags = SORT_REGULAR)
206
    {
207 12
        ksort($this->array, $flags);
208 12
    }
209
210
    /**
211
     * {@inheritdoc}
212
     */
213 2
    public function count()
214
    {
215 2
        return count($this->array);
216
    }
217
}
218