|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace RedisProxy; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @method int llen(string $key) Get the length of a list |
|
7
|
|
|
* @method array lrange(string $key, int $start, int $stop) Get a range of elements from a list |
|
8
|
|
|
*/ |
|
9
|
|
|
trait ListBehavior |
|
10
|
|
|
{ |
|
11
|
|
|
private $driver; |
|
12
|
|
|
|
|
13
|
|
|
abstract protected function init(); |
|
14
|
|
|
|
|
15
|
|
|
abstract public function actualDriver(); |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Prepend one or multiple values to a list |
|
19
|
|
|
* @param string $key |
|
20
|
|
|
* @param array $elements |
|
21
|
|
|
* @return int the length of the list after the push operations |
|
22
|
|
|
*/ |
|
23
|
|
|
public function lpush($key, ...$elements) |
|
24
|
|
|
{ |
|
25
|
|
|
$elements = $this->prepareArguments('lpush', ...$elements); |
|
|
|
|
|
|
26
|
|
|
$this->init(); |
|
27
|
|
|
return $this->driver->lpush($key, ...$elements); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Append one or multiple values to a list |
|
32
|
|
|
* @param string $key |
|
33
|
|
|
* @param array $elements |
|
34
|
|
|
* @return int the length of the list after the push operations |
|
35
|
|
|
*/ |
|
36
|
|
|
public function rpush($key, ...$elements) |
|
37
|
|
|
{ |
|
38
|
|
|
$elements = $this->prepareArguments('rpush', ...$elements); |
|
|
|
|
|
|
39
|
|
|
$this->init(); |
|
40
|
|
|
return $this->driver->rpush($key, ...$elements); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Remove and get the first element in a list |
|
45
|
|
|
* @param string $key |
|
46
|
|
|
* @return string|null |
|
47
|
|
|
*/ |
|
48
|
|
|
public function lpop($key) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->init(); |
|
51
|
|
|
$result = $this->driver->lpop($key); |
|
52
|
|
|
return $this->convertFalseToNull($result); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Remove and get the last element in a list |
|
57
|
|
|
* @param string $key |
|
58
|
|
|
* @return string|null |
|
59
|
|
|
*/ |
|
60
|
|
|
public function rpop($key) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->init(); |
|
63
|
|
|
$result = $this->driver->rpop($key); |
|
64
|
|
|
return $this->convertFalseToNull($result); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get an element from a list by its index |
|
69
|
|
|
* @param string $key |
|
70
|
|
|
* @param int $index zero-based, so 0 means the first element, 1 the second element and so on. -1 means the last element, -2 means the penultimate and so forth |
|
71
|
|
|
* @return string|null |
|
72
|
|
|
*/ |
|
73
|
|
|
public function lindex($key, $index) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->init(); |
|
76
|
|
|
$result = $this->driver->lindex($key, $index); |
|
77
|
|
|
return $this->convertFalseToNull($result); |
|
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.