1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is a part of "Axessors" library. |
4
|
|
|
* |
5
|
|
|
* @author <[email protected]> |
6
|
|
|
* @license GPL |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace NoOne4rever\Axessors; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class ConditionsProcessor. |
13
|
|
|
* |
14
|
|
|
* Processes Axessors conditions. |
15
|
|
|
* |
16
|
|
|
* @package NoOne4rever\Axessors |
17
|
|
|
*/ |
18
|
|
|
class ConditionsProcessor extends TokenProcessor |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Creates list of conditions for input data. |
22
|
|
|
* |
23
|
|
|
* @return string conditions |
24
|
|
|
*/ |
25
|
2 |
|
public function processInputData(): string |
26
|
|
|
{ |
27
|
2 |
|
return $this->makePhpConditions($this->input); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Creates list of conditions for output data. |
32
|
|
|
* |
33
|
|
|
* @return string conditions |
34
|
|
|
*/ |
35
|
2 |
|
public function processOutputData(): string |
36
|
|
|
{ |
37
|
2 |
|
return $this->makePhpConditions($this->output); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Turns Axessors conditions into PHP code. |
42
|
|
|
* |
43
|
|
|
* @param string $axsConditions Axessors conditional statement |
44
|
|
|
* |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
2 |
|
private function makePhpConditions(string $axsConditions): string |
48
|
|
|
{ |
49
|
2 |
|
if ($axsConditions === '') { |
50
|
2 |
|
return 'true'; |
51
|
|
|
} |
52
|
2 |
|
$injProcessor = new InjectedStringSuit($axsConditions); |
53
|
2 |
|
$axsConditions = $injProcessor->resolveClassNames($this->namespace)->processThis()->wrapWithClosure() |
54
|
2 |
|
->addSlashes('<>!=') |
55
|
2 |
|
->get(); |
56
|
2 |
|
$axsConditions = preg_replace_callback( |
57
|
2 |
|
'/(((>|<)=?)|(=|!)=)\s*\d+/', |
58
|
2 |
|
function (array $matches): string { |
59
|
2 |
|
return sprintf('\%s::count($var) %s', ConditionsRunner::class, $matches[0]); |
60
|
2 |
|
}, |
61
|
2 |
|
$axsConditions |
62
|
|
|
); |
63
|
2 |
|
$axsConditions = preg_replace_callback( |
64
|
2 |
|
'/\d+\.\.\d+/', |
65
|
2 |
|
function (array $matches): string { |
66
|
2 |
|
list($min, $max) = explode('..', $matches[0]); |
67
|
2 |
|
return sprintf('\%s::count($var) >= %d' |
68
|
2 |
|
. ' && \%s::count($var) <= %d', ConditionsRunner::class, $min, ConditionsRunner::class, $max); |
69
|
2 |
|
}, |
70
|
2 |
|
$axsConditions |
71
|
|
|
); |
72
|
2 |
|
$axsConditions = preg_replace_callback( |
73
|
2 |
|
'/\\\\[<>!=]/', |
74
|
2 |
|
function (array $matches): string { |
75
|
2 |
|
return substr($matches[0], 1); |
76
|
2 |
|
}, |
77
|
2 |
|
$axsConditions |
78
|
|
|
); |
79
|
2 |
|
$axsConditions = preg_replace_callback( |
80
|
2 |
|
'/`[^`]+`/', |
81
|
2 |
|
function (array $matches): string { |
82
|
2 |
|
$subject = substr($matches[0], 1, strlen($matches[0]) - 2); |
83
|
2 |
|
return sprintf('(function($var){return %s;})($var)', $subject); |
84
|
2 |
|
}, |
85
|
|
|
$axsConditions |
86
|
|
|
); |
87
|
|
|
return $axsConditions; |
88
|
|
|
} |
89
|
|
|
} |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: