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 |
|
$this->expression = preg_replace_callback( |
41
|
2 |
|
'/`{[^}]*}`/', |
42
|
2 |
|
function (array $matches): string { |
43
|
2 |
|
$result = substr($matches[0], 1, strlen($matches[0]) - 2); |
44
|
2 |
|
return sprintf('`(function ($var) %s)($var)`', $result); |
45
|
2 |
|
}, |
46
|
2 |
|
$this->expression |
47
|
|
|
); |
48
|
2 |
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Processes "$." in injected string. |
53
|
|
|
* |
54
|
|
|
* @return InjectedStringSuit object for methods chain |
55
|
|
|
*/ |
56
|
|
|
public function processThis(): self |
57
|
|
|
{ |
58
|
2 |
|
$expression = preg_replace_callback('/"[^"]"|\'[^\']\'/', function (array $matches) { |
59
|
|
|
return str_replace('$.', '\\$.', $matches[0]); |
60
|
2 |
|
}, $this->expression); |
61
|
2 |
|
$expression = preg_replace('/\$\./', '$this->', $expression); |
62
|
2 |
|
$expression = preg_replace_callback('/"[^"]"|\'[^\']\'/', function (array $matches) { |
63
|
|
|
return str_replace('\\$.', '$.', $matches[0]); |
64
|
2 |
|
}, $expression); |
65
|
2 |
|
$this->expression = $expression; |
66
|
2 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Resolves class names in *injected* callbacks and conditions. |
71
|
|
|
* |
72
|
|
|
* @param string $namespace namespace |
73
|
|
|
* |
74
|
|
|
* @return InjectedStringSuit object for methods chain |
75
|
|
|
*/ |
76
|
|
|
public function resolveClassNames(string $namespace): self |
77
|
|
|
{ |
78
|
2 |
|
$expression = preg_replace_callback('/"[^"]"|\'[^\']\'/', function (array $matches) { |
79
|
|
|
return str_replace(':', ':\\', $matches[0]); |
80
|
2 |
|
}, $this->expression); |
81
|
2 |
|
$expression = preg_replace('/(?<!:):(?=([a-zA-Z_][a-zA-Z0-9_]*))/', "$namespace\\", $expression); |
82
|
2 |
|
$expression = preg_replace_callback('/"[^"]"|\'[^\']\'/', function (array $matches) { |
83
|
|
|
return str_replace(':\\', ':', $matches[0]); |
84
|
2 |
|
}, $expression); |
85
|
2 |
|
$this->expression = $expression; |
86
|
2 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Adds slashes to string. |
91
|
|
|
* |
92
|
|
|
* @param string $charlist symbols add slashes to |
93
|
|
|
* |
94
|
|
|
* @return InjectedStringSuit object for methods chain |
95
|
|
|
*/ |
96
|
5 |
|
public function addSlashes(string $charlist): self |
97
|
|
|
{ |
98
|
5 |
|
$this->expression = preg_replace_callback( |
99
|
5 |
|
'/`([^`]|\\\\`)+((?<!\\\\)`)/', |
100
|
5 |
|
function (array $matches) use($charlist): string { |
101
|
2 |
|
return addcslashes($matches[0], $charlist); |
102
|
5 |
|
}, |
103
|
5 |
|
$this->expression |
104
|
|
|
); |
105
|
5 |
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Getter for InjectedStringProcessor::$expression. |
110
|
|
|
* |
111
|
|
|
* @return string processed expression |
112
|
|
|
*/ |
113
|
5 |
|
public function get(): string |
114
|
|
|
{ |
115
|
5 |
|
return $this->expression; |
116
|
|
|
} |
117
|
|
|
} |