|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Webdcg\Redis\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Webdcg\Redis\Exceptions\BitwiseOperationException; |
|
6
|
|
|
|
|
7
|
|
|
trait Bits |
|
8
|
|
|
{ |
|
9
|
|
|
/* |
|
10
|
|
|
* Available Bit Operations |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $BIT_OPERATIONS = ['AND', 'OR', 'XOR', 'NOT']; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Count set bits in a string. |
|
16
|
|
|
* |
|
17
|
|
|
* @param string $key |
|
18
|
|
|
* |
|
19
|
|
|
* @return int |
|
20
|
|
|
*/ |
|
21
|
|
|
public function bitCount(string $key): int |
|
22
|
|
|
{ |
|
23
|
|
|
return $this->redis->bitCount($key); |
|
|
|
|
|
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Perform bitwise operations between strings. |
|
28
|
|
|
* |
|
29
|
|
|
* @param string $operation NOT, AND, OR, XOR |
|
30
|
|
|
* @param string $returnKey Return Key |
|
31
|
|
|
* @param splat $keys List of keys for input |
|
32
|
|
|
* |
|
33
|
|
|
* @return int The size of the string stored in the destination key. |
|
34
|
|
|
*/ |
|
35
|
|
|
public function bitOp(string $operation, string $returnKey, ...$keys): int |
|
36
|
|
|
{ |
|
37
|
|
|
$operation = strtoupper($operation); |
|
38
|
|
|
|
|
39
|
|
|
if (!in_array($operation, $this->BIT_OPERATIONS)) { |
|
40
|
|
|
throw new BitwiseOperationException('Operation not supported', 1); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
if ('NOT' == $operation) { |
|
44
|
|
|
return $this->redis->bitOp($operation, $returnKey, $keys[0]); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return $this->redis->bitOp($operation, $returnKey, ...$keys); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Sets or clears the bit at offset in the string value stored at key. |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $key |
|
54
|
|
|
* @param int $offset |
|
55
|
|
|
* @param int $value |
|
56
|
|
|
* |
|
57
|
|
|
* @return int 0 or 1, the value of the bit before it was set. |
|
58
|
|
|
*/ |
|
59
|
|
|
public function setBit(string $key, int $offset, int $value): int |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->redis->setBit($key, $offset, $value); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Returns the bit value at offset in the string value stored at key. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $key |
|
68
|
|
|
* @param int $offset |
|
69
|
|
|
* |
|
70
|
|
|
* @return int |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getBit(string $key, int $offset): int |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->redis->getBit($key, $offset); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function bitField(): bool |
|
78
|
|
|
{ |
|
79
|
|
|
return false; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function bitPos(): bool |
|
83
|
|
|
{ |
|
84
|
|
|
return false; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: