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 InjectedStringParser. |
13
|
|
|
* |
14
|
|
|
* Processes *injected* callbacks and conditions. |
15
|
|
|
* |
16
|
|
|
* @package NoOne4rever\Axessors |
17
|
|
|
*/ |
18
|
|
|
class InjectedStringSuit |
19
|
|
|
{ |
20
|
|
|
/** @var string expression */ |
21
|
|
|
private $expression; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* InjectedStringSuit constructor. |
25
|
|
|
* |
26
|
|
|
* @param string $expression expression |
27
|
|
|
*/ |
28
|
5 |
|
public function __construct(string $expression) |
29
|
|
|
{ |
30
|
5 |
|
$this->expression = $expression; |
31
|
5 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Turns code block into a simple closure. |
35
|
|
|
* |
36
|
|
|
* @return $this object for methods chain |
37
|
|
|
*/ |
38
|
2 |
|
public function wrapWithClosure(): self |
39
|
|
|
{ |
40
|
2 |
|
if (preg_match('/^`{[^}]*}`$/', $this->expression)) { |
41
|
2 |
|
$this->expression = substr($this->expression, 1, strlen($this->expression) - 2); |
42
|
2 |
|
$this->expression = "`(function(\$var){$this->expression})(\$var)`"; |
43
|
|
|
} |
44
|
2 |
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Processes "$." in injected string. |
49
|
|
|
* |
50
|
|
|
* @return InjectedStringSuit object for methods chain |
51
|
|
|
*/ |
52
|
|
|
public function processThis(): self |
53
|
|
|
{ |
54
|
2 |
|
$expression = preg_replace_callback('/"[^"]"|\'[^\']\'/', function (array $matches) { |
55
|
|
|
return str_replace('$.', '\\$.', $matches[0]); |
56
|
2 |
|
}, $this->expression); |
57
|
2 |
|
$expression = preg_replace('/\$\./', '$this->', $expression); |
58
|
2 |
|
$expression = preg_replace_callback('/"[^"]"|\'[^\']\'/', function (array $matches) { |
59
|
|
|
return str_replace('\\$.', '$.', $matches[0]); |
60
|
2 |
|
}, $expression); |
61
|
2 |
|
$this->expression = $expression; |
62
|
2 |
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Resolves class names in *injected* callbacks and conditions. |
67
|
|
|
* |
68
|
|
|
* @param string $namespace namespace |
69
|
|
|
* |
70
|
|
|
* @return InjectedStringSuit object for methods chain |
71
|
|
|
*/ |
72
|
|
|
public function resolveClassNames(string $namespace): self |
73
|
|
|
{ |
74
|
2 |
|
$expression = preg_replace_callback('/"[^"]"|\'[^\']\'/', function (array $matches) { |
75
|
|
|
return str_replace(':', ':\\', $matches[0]); |
76
|
2 |
|
}, $this->expression); |
77
|
2 |
|
$expression = preg_replace('/(?<!:):(?=([a-zA-Z_][a-zA-Z0-9_]*))/', "$namespace\\", $expression); |
78
|
2 |
|
$expression = preg_replace_callback('/"[^"]"|\'[^\']\'/', function (array $matches) { |
79
|
|
|
return str_replace(':\\', ':', $matches[0]); |
80
|
2 |
|
}, $expression); |
81
|
2 |
|
$this->expression = $expression; |
82
|
2 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Adds slashes to string. |
87
|
|
|
* |
88
|
|
|
* @param string $charlist symbols add slashes to |
89
|
|
|
* |
90
|
|
|
* @return InjectedStringSuit object for methods chain |
91
|
|
|
*/ |
92
|
5 |
|
public function addSlashes(string $charlist): self |
93
|
|
|
{ |
94
|
5 |
|
$this->expression = preg_replace_callback( |
95
|
5 |
|
'/`([^`]|\\\\`)+((?<!\\\\)`)/', |
96
|
5 |
|
function (array $matches) use($charlist): string { |
97
|
2 |
|
return addcslashes($matches[0], $charlist); |
98
|
5 |
|
}, |
99
|
5 |
|
$this->expression |
100
|
|
|
); |
101
|
5 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Getter for InjectedStringProcessor::$expression. |
106
|
|
|
* |
107
|
|
|
* @return string processed expression |
108
|
|
|
*/ |
109
|
5 |
|
public function get(): string |
110
|
|
|
{ |
111
|
5 |
|
return $this->expression; |
112
|
|
|
} |
113
|
|
|
} |