1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* For the full copyright and license information, please view |
5
|
|
|
* the LICENSE file that was distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace loophp\fpt; |
11
|
|
|
|
12
|
|
|
use Closure; |
13
|
|
|
use InvalidArgumentException; |
14
|
|
|
|
15
|
|
|
// phpcs:disable Generic.Files.LineLength.TooLong |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @psalm-immutable |
19
|
|
|
*/ |
20
|
|
|
final class Operator |
21
|
|
|
{ |
22
|
|
|
public const OP_AND = 'and'; |
23
|
|
|
|
24
|
|
|
public const OP_BINARY_AND = '&'; |
25
|
|
|
|
26
|
|
|
public const OP_BINARY_OR = '|'; |
27
|
|
|
|
28
|
|
|
public const OP_BINARY_SHIFT_LEFT = '<<'; |
29
|
|
|
|
30
|
|
|
public const OP_BINARY_SHIFT_RIGHT = '>>'; |
31
|
|
|
|
32
|
|
|
public const OP_BINARY_XOR = '^'; |
33
|
|
|
|
34
|
|
|
public const OP_DIV = '/'; |
35
|
|
|
|
36
|
|
|
public const OP_EQUAL = '==='; |
37
|
|
|
|
38
|
|
|
public const OP_GT = '>'; |
39
|
|
|
|
40
|
|
|
public const OP_GTE = '>='; |
41
|
|
|
|
42
|
|
|
public const OP_INSTANCEOF = 'instanceof'; |
43
|
|
|
|
44
|
|
|
public const OP_LIKE = '=='; |
45
|
|
|
|
46
|
|
|
public const OP_LT = '<'; |
47
|
|
|
|
48
|
|
|
public const OP_LTE = '<='; |
49
|
|
|
|
50
|
|
|
public const OP_MINUS = '-'; |
51
|
|
|
|
52
|
|
|
public const OP_MODULO = '%'; |
53
|
|
|
|
54
|
|
|
public const OP_MULT = '*'; |
55
|
|
|
|
56
|
|
|
public const OP_NOT_EQUAL = '!=='; |
57
|
|
|
|
58
|
|
|
public const OP_NOT_LIKE = '!='; |
59
|
|
|
|
60
|
|
|
public const OP_OR = 'or'; |
61
|
|
|
|
62
|
|
|
public const OP_PLUS = '+'; |
63
|
|
|
|
64
|
|
|
public const OP_SPACESHIP = '<=>'; |
65
|
|
|
|
66
|
|
|
public const OP_XOR = 'xor'; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @pure |
70
|
|
|
* |
71
|
|
|
* @return pure-Closure(string): Closure(mixed): Closure(mixed): (bool|int|numeric) |
|
|
|
|
72
|
|
|
*/ |
73
|
25 |
|
public static function of(): Closure |
74
|
|
|
{ |
75
|
|
|
return |
76
|
|
|
/** |
77
|
|
|
* @return pure-Closure(mixed): Closure(mixed): (bool|int|numeric) |
|
|
|
|
78
|
|
|
*/ |
79
|
25 |
|
static fn (string $operator): Closure => |
80
|
|
|
/** |
81
|
|
|
* @param mixed $left |
82
|
|
|
* |
83
|
|
|
* @return pure-Closure(mixed): (bool|int|numeric) |
|
|
|
|
84
|
|
|
*/ |
85
|
24 |
|
static fn (mixed $left): Closure => |
86
|
|
|
/** |
87
|
|
|
* @return bool|int|numeric |
|
|
|
|
88
|
|
|
* |
89
|
|
|
* @psalm-suppress PossiblyInvalidOperand |
90
|
|
|
* @psalm-suppress InvalidOperand |
91
|
|
|
*/ |
92
|
|
|
static function (mixed $right) use ($operator, $left): mixed { |
93
|
|
|
return match ($operator) { |
94
|
|
|
self::OP_AND, '&&' => $left && $right, |
95
|
24 |
|
self::OP_MINUS => $left - $right, |
96
|
|
|
self::OP_PLUS => $left + $right, |
97
|
24 |
|
self::OP_MODULO => $left % $right, |
98
|
24 |
|
self::OP_DIV => $left / $right, |
99
|
1 |
|
self::OP_MULT => $left * $right, |
100
|
|
|
self::OP_OR, '||' => $left || $right, |
101
|
23 |
|
self::OP_XOR => $left xor $right, |
102
|
1 |
|
'<<' => $left << $right, |
103
|
|
|
'>>' => $left >> $right, |
104
|
22 |
|
'&' => $left & $right, |
|
|
|
|
105
|
1 |
|
'|' => $left | $right, |
|
|
|
|
106
|
|
|
'^' => $left ^ $right, |
107
|
21 |
|
'>' => $left > $right, |
108
|
1 |
|
'>=' => $left >= $right, |
109
|
|
|
'<' => $left < $right, |
110
|
20 |
|
'<=' => $left <= $right, |
111
|
1 |
|
'instanceof' => $left instanceof $right, |
112
|
|
|
'!==' => $left !== $right, |
113
|
19 |
|
'!=', '<>' => $left != $right, |
114
|
1 |
|
'===' => $left === $right, |
115
|
|
|
'==' => $left == $right, |
116
|
18 |
|
'<=>' => $left <=> $right, |
117
|
18 |
|
default => throw new InvalidArgumentException('Unable to find the operator.') |
118
|
1 |
|
}; |
119
|
|
|
}; |
120
|
17 |
|
} |
121
|
|
|
} |
122
|
|
|
|