Completed
Push — master ( 92e121...67bb00 )
by Roberto
14s queued 10s
created

Strings.php ➔ is_associative()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Webdcg\Redis\Traits;
4
5
use Webdcg\Redis\Exceptions\NotAssociativeArrayException;
6
7
function is_associative(array $array)
8
{
9
    return array_keys($array) !== range(0, count($array) - 1);
10
}
11
12
trait Strings
13
{
14
    /**
15
     * Append specified string to the string stored in specified key.
16
     * See: https://redis.io/commands/append.
17
     *
18
     * @param  string $key
19
     * @param  string $value
20
     *
21
     * @return int              Size of the value after the append.
22
     */
23
    public function append(string $key, string $value): int
24
    {
25
        return $this->redis->append($key, $value);
0 ignored issues
show
Bug introduced by
The property redis does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
26
    }
27
28
    /**
29
     * Decrement the number stored at key by one.
30
     * See: https://redis.io/commands/decr.
31
     *
32
     * @param  string $key
33
     *
34
     * @return int          the new value
35
     */
36
    public function decr(string $key): int
37
    {
38
        return $this->redis->decr($key);
39
    }
40
41
    /**
42
     * Decrement the number stored at key by the given decrement.
43
     * See: https://redis.io/commands/decrby.
44
     *
45
     * @param  string $key       [description]
46
     * @param  int    $decrement [description]
47
     *
48
     * @return int              The new value
49
     */
50
    public function decrBy(string $key, int $decrement): int
51
    {
52
        return $this->redis->decrBy($key, $decrement);
53
    }
54
55
    public function get(string $key)
56
    {
57
        return $this->redis->get($key);
58
    }
59
60
    /**
61
     * Return a substring of a larger string.
62
     * See: https://redis.io/commands/getrange.
63
     *
64
     * @param  string $key   [description]
65
     * @param  int    $start [description]
66
     * @param  int    $end   [description]
67
     *
68
     * @return string       the substring
69
     */
70
    public function getRange(string $key, int $start, int $end): string
71
    {
72
        return $this->redis->getRange($key, $start, $end);
73
    }
74
75
    /**
76
     * Atomically sets key to value and returns the old value stored at key.
77
     * Returns an error when key exists but does not hold a string value.
78
     * See: https://redis.io/commands/getset.
79
     *
80
     * @param  string $key   [description]
81
     * @param  string $value [description]
82
     *
83
     * @return string       Bulk string reply: the old value stored at key,
84
     *                      or nil when key did not exist.
85
     */
86
    public function getSet(string $key, string $value): string
87
    {
88
        return $this->redis->getSet($key, $value);
89
    }
90
91
    /**
92
     * Increment the number stored at key by one.
93
     * See: https://redis.io/commands/incr.
94
     *
95
     * @param  string $key [description]
96
     *
97
     * @return int         The new value.
98
     */
99
    public function incr(string $key): int
100
    {
101
        return $this->redis->incr($key);
102
    }
103
104
    /**
105
     * Increment the number stored at key by the given increment.
106
     * See: https://redis.io/commands/incrby.
107
     *
108
     * @param  string $key       [description]
109
     * @param  int    $increment [description]
110
     *
111
     * @return int                  The new value
112
     */
113
    public function incrBy(string $key, int $increment): int
114
    {
115
        return $this->redis->incrBy($key, $increment);
116
    }
117
118
    /**
119
     * Increment the number stored at key by the given increment.
120
     * See: https://redis.io/commands/incrbyfloat.
121
     *
122
     * @param  string $key       [description]
123
     * @param  float  $increment [description]
124
     *
125
     * @return float                The new value.
126
     */
127
    public function incrByFloat(string $key, float $increment): float
128
    {
129
        return $this->redis->incrByFloat($key, $increment);
130
    }
131
132
    /**
133
     * Get the values of all the specified keys. If one or more keys don't
134
     * exist, the array will contain FALSE at the position of the key.
135
     * See: https://redis.io/commands/mget.
136
     *
137
     * @param  array  $keys [description]
138
     *
139
     * @return array        Array reply: list of values at the specified keys.
140
     */
141
    public function mGet(array $keys): array
142
    {
143
        return $this->redis->mGet($keys);
144
    }
145
146
    /**
147
     * Get the values of all the specified keys. If one or more keys don't
148
     * exist, the array will contain FALSE at the position of the key.
149
     * See: https://redis.io/commands/mget.
150
     *
151
     * @param  array  $keys [description]
152
     *
153
     * @return array        Array reply: list of values at the specified keys.
154
     */
155
    public function getMultiple(array $keys): array
156
    {
157
        return $this->redis->mGet($keys);
158
    }
159
160
    /**
161
     * Sets multiple key-value pairs in one atomic command.
162
     * See: https://redis.io/commands/mset.
163
     *
164
     * @param  array  $pairs [description]
165
     *
166
     * @return bool         TRUE in case of success, FALSE in case of failure.
167
     */
168
    public function mSet(array $pairs): bool
169
    {
170
        if (! is_associative($pairs)) {
171
            throw new NotAssociativeArrayException('The array provided is not associative.', 1);
172
        }
173
174
        return $this->redis->mSet($pairs);
175
    }
176
177
    public function mSetNX(): bool
178
    {
179
        return false;
180
    }
181
182
    /**
183
     * Set the string value in argument as value of the key. If you're using Redis >= 2.6.12, you can pass extended
184
     * options as explained below.
185
     *
186
     * @param string $key
187
     * @param mixed $value
188
     * @param mixed $args Timeout or Options Array (optional). If you pass an
189
     *                    integer, phpredis will redirect to SETEX, and will
190
     *                    try to use Redis >= 2.6.12 extended options if you
191
     *                    pass an array with valid values
192
     *
193
     * @return bool TRUE if the command is successful.
194
     */
195
    public function set(string $key, $value, ...$args): bool
196
    {
197
        if (empty($args)) {
198
            return $this->redis->set($key, $value);
199
        }
200
201
        return $this->redis->set($key, $value, $args[0]);
202
    }
203
204
    public function setEx(string $key, int $timeout, $value): bool
205
    {
206
        return $this->redis->setEx($key, $timeout, $value);
207
    }
208
209
    public function pSetEx(): bool
210
    {
211
        return false;
212
    }
213
214
    public function setNx(): bool
215
    {
216
        return false;
217
    }
218
219
    /**
220
     * Changes a substring of a larger string.
221
     * See: https://redis.io/commands/setrange.
222
     *
223
     * @param string $key    [description]
224
     * @param int    $offset [description]
225
     * @param string $value  [description]
226
     *
227
     * @return int          the length of the string after it was modified.
228
     */
229
    public function setRange(string $key, int $offset, string $value): int
230
    {
231
        return $this->redis->setRange($key, $offset, $value);
232
    }
233
234
    /**
235
     * Returns the length of the string value stored at key. An error is
236
     * returned when key holds a non-string value.
237
     * See: https://redis.io/commands/strlen.
238
     *
239
     * @param  string $key [description]
240
     *
241
     * @return int          The length of the string at key, or 0 when key does
242
     *                      not exist.
243
     */
244
    public function strLen(string $key): int
245
    {
246
        return $this->redis->strLen($key);
247
    }
248
}
249