Completed
Pull Request — master (#23)
by Bernhard
18:34
created

ArrayStore::__construct()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 4
Bugs 1 Features 2
Metric Value
c 4
b 1
f 2
dl 0
loc 24
ccs 8
cts 8
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
                return $value;
69
            };
70
            $this->unserialize = function ($value) {
71 192
                return $value;
72 2
            };
73
        }
74 192
75
        foreach ($array as $key => $value) {
76
            $this->set($key, $value);
77
        }
78
    }
79 132
80
    /**
81 132
     * {@inheritdoc}
82
     */
83 128
    public function set($key, $value)
84 128
    {
85
        KeyUtil::validate($key);
86
87
        $this->array[$key] = call_user_func($this->serialize, $value);
88
    }
89 12
90
    /**
91 12
     * {@inheritdoc}
92
     */
93 8 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 2
    {
95
        KeyUtil::validate($key);
96
97 6
        if (!array_key_exists($key, $this->array)) {
98
            return $default;
99
        }
100
101
        return call_user_func($this->unserialize, $this->array[$key]);
102
    }
103 60
104
    /**
105 60
     * {@inheritdoc}
106
     */
107 56 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 2
    {
109
        KeyUtil::validate($key);
110
111 54
        if (!array_key_exists($key, $this->array)) {
112
            throw NoSuchKeyException::forKey($key);
113
        }
114
115
        return call_user_func($this->unserialize, $this->array[$key]);
116
    }
117 80
118
    /**
119 80
     * {@inheritdoc}
120
     */
121 76
    public function getMultiple(array $keys, $default = null)
122
    {
123 76
        KeyUtil::validateMultiple($keys);
124 76
125 76
        $values = array();
126 76
127
        foreach ($keys as $key) {
128
            $values[$key] = array_key_exists($key, $this->array)
129 76
                ? call_user_func($this->unserialize, $this->array[$key])
130
                : $default;
131
        }
132
133
        return $values;
134
    }
135 60
136
    /**
137 60
     * {@inheritdoc}
138
     */
139 56
    public function getMultipleOrFail(array $keys)
140
    {
141 56
        KeyUtil::validateMultiple($keys);
142 2
143
        $notFoundKeys = array_diff($keys, array_keys($this->array));
144
145 54
        if (count($notFoundKeys) > 0) {
146
            throw NoSuchKeyException::forKeys($notFoundKeys);
147
        }
148
149
        return $this->getMultiple($keys);
150
    }
151 20
152
    /**
153 20
     * {@inheritdoc}
154
     */
155 16
    public function remove($key)
156
    {
157 16
        KeyUtil::validate($key);
158
159 16
        $removed = array_key_exists($key, $this->array);
160
161
        unset($this->array[$key]);
162
163
        return $removed;
164
    }
165 60
166
    /**
167 60
     * {@inheritdoc}
168
     */
169 56
    public function exists($key)
170
    {
171
        KeyUtil::validate($key);
172
173
        return array_key_exists($key, $this->array);
174
    }
175 186
176
    /**
177 186
     * {@inheritdoc}
178 186
     */
179
    public function clear()
180
    {
181
        $this->array = array();
182
    }
183 24
184
    /**
185 24
     * {@inheritdoc}
186
     */
187
    public function keys()
188
    {
189
        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 12
202
    /**
203 12
     * {@inheritdoc}
204 12
     */
205
    public function sort($flags = SORT_REGULAR)
206
    {
207
        ksort($this->array, $flags);
208
    }
209 2
210
    /**
211 2
     * {@inheritdoc}
212
     */
213
    public function count()
214
    {
215
        return count($this->array);
216
    }
217
}
218