Completed
Pull Request — master (#9)
by Maxim
02:05
created

InjectedStringSuit::processThis()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1.0028

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 11
ccs 6
cts 7
cp 0.8571
crap 1.0028
rs 9.4285
c 0
b 0
f 0
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
                $result = substr($matches[0], 1, strlen($matches[0]) - 2);
44 2
                return sprintf('`(function ($var) %s)($var)`', $result);
45
            },
46
            $this->expression
47
        );
48
        return $this;
49
    }
50
51
    /**
52
     * Processes "$." in injected string.
53
     * 
54 2
     * @return InjectedStringSuit object for methods chain
55
     */
56 2
    public function processThis(): self
57 2
    {
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
        }, $expression);
65
        $this->expression = $expression;
66
        return $this;
67
    }
68
69
    /**
70
     * Resolves class names in *injected* callbacks and conditions.
71
     *
72
     * @param string $namespace namespace
73
     * 
74 2
     * @return InjectedStringSuit object for methods chain
75
     */
76 2
    public function resolveClassNames(string $namespace): self
77 2
    {
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
        }, $expression);
85
        $this->expression = $expression;
86
        return $this;
87
    }
88
89
    /**
90
     * Adds slashes to string.
91
     *
92 5
     * @param string $charlist symbols add slashes to
93
     * 
94 5
     * @return InjectedStringSuit object for methods chain 
95 5
     */
96 5
    public function addSlashes(string $charlist): self
97 2
    {
98 5
        $this->expression = preg_replace_callback(
99 5
            '/`([^`]|\\\\`)+((?<!\\\\)`)/',
100
            function (array $matches) use($charlist): string {
101 5
                return addcslashes($matches[0], $charlist);
102
            },
103
            $this->expression
104
        );
105
        return $this;
106
    }
107
    
108
    /**
109 5
     * Getter for InjectedStringProcessor::$expression.
110
     * 
111 5
     * @return string processed expression
112
     */
113
    public function get(): string 
114
    {
115
        return $this->expression;
116
    }
117
}