1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Platine Expression |
5
|
|
|
* |
6
|
|
|
* Platine Expression is an expression parser, evaluator with support of custom |
7
|
|
|
* operators and functions |
8
|
|
|
* |
9
|
|
|
* This content is released under the MIT License (MIT) |
10
|
|
|
* |
11
|
|
|
* Copyright (c) 2020 Platine Expression |
12
|
|
|
* Copyright (c) Alexander Kiryukhin |
13
|
|
|
* |
14
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
15
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
16
|
|
|
* in the Software without restriction, including without limitation the rights |
17
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
18
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
19
|
|
|
* furnished to do so, subject to the following conditions: |
20
|
|
|
* |
21
|
|
|
* The above copyright notice and this permission notice shall be included in all |
22
|
|
|
* copies or substantial portions of the Software. |
23
|
|
|
* |
24
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
25
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
26
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
27
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
28
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
29
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
30
|
|
|
* SOFTWARE. |
31
|
|
|
*/ |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @file CustomFunction.php |
35
|
|
|
* |
36
|
|
|
* The custom function class |
37
|
|
|
* |
38
|
|
|
* @package Platine\Expression |
39
|
|
|
* @author Platine Developers Team |
40
|
|
|
* @copyright Copyright (c) 2020 |
41
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
42
|
|
|
* @link https://www.platine-php.com |
43
|
|
|
* @version 1.0.0 |
44
|
|
|
* @filesource |
45
|
|
|
*/ |
46
|
|
|
declare(strict_types=1); |
47
|
|
|
|
48
|
|
|
namespace Platine\Expression; |
49
|
|
|
|
50
|
|
|
use Closure; |
51
|
|
|
use Platine\Expression\Exception\IncorrectNumberOfFunctionParametersException; |
52
|
|
|
use ReflectionFunction; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @class CustomFunction |
56
|
|
|
* @package Platine\Expression |
57
|
|
|
*/ |
58
|
|
|
class CustomFunction |
59
|
|
|
{ |
60
|
|
|
/** |
61
|
|
|
* Number of function argument required |
62
|
|
|
* @var int |
63
|
|
|
*/ |
64
|
|
|
protected int $requiredParamCount = 0; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Create new instance |
68
|
|
|
* @param string $name The function name |
69
|
|
|
* @param callable $function The function to be called |
70
|
|
|
*/ |
71
|
|
|
public function __construct(protected string $name, protected $function) |
72
|
|
|
{ |
73
|
|
|
$this->name = $name; |
74
|
|
|
$this->function = $function; |
75
|
|
|
|
76
|
|
|
$reflection = new ReflectionFunction(Closure::fromCallable($function)); |
77
|
|
|
$this->requiredParamCount = $reflection->getNumberOfRequiredParameters(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Execute the function |
82
|
|
|
* @param Token[] $stack |
83
|
|
|
* @param int $stackParamCount |
84
|
|
|
* @return Token |
85
|
|
|
*/ |
86
|
|
|
public function execute(array &$stack, int $stackParamCount): Token |
87
|
|
|
{ |
88
|
|
|
if ($stackParamCount < $this->requiredParamCount) { |
89
|
|
|
throw new IncorrectNumberOfFunctionParametersException(sprintf( |
90
|
|
|
'Incorrect number of function parameters, [%d] needed, [%d] passed', |
91
|
|
|
$this->requiredParamCount, |
92
|
|
|
$stackParamCount |
93
|
|
|
)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$args = []; |
97
|
|
|
if ($stackParamCount > 0) { |
98
|
|
|
for ($i = 0; $i < $stackParamCount; $i++) { |
99
|
|
|
$token = array_pop($stack); |
100
|
|
|
if ($token !== null) { |
101
|
|
|
array_unshift($args, $token->getValue()); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$result = call_user_func_array($this->function, $args); |
107
|
|
|
|
108
|
|
|
return new Token(Token::LITERAL, $result); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Return the name |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
public function getName(): string |
116
|
|
|
{ |
117
|
|
|
return $this->name; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Return the function |
122
|
|
|
* @return callable |
123
|
|
|
*/ |
124
|
|
|
public function getFunction(): callable |
125
|
|
|
{ |
126
|
|
|
return $this->function; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Return the number of required parameters |
131
|
|
|
* @return int |
132
|
|
|
*/ |
133
|
|
|
public function getRequiredParamCount(): int |
134
|
|
|
{ |
135
|
|
|
return $this->requiredParamCount; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|