Completed
Pull Request — master (#5)
by Maxim
02:34
created

InjectedStringSuit::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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
     * Processes "$." in injected string.
35
     * 
36
     * @return InjectedStringSuit object for methods chain
37
     */
38
    public function processThis(): self
39
    {
40 2
        $expression = preg_replace_callback('/"[^"]"|\'[^\']\'/', function (array $matches) {
41
            return str_replace('$.', '\\$.', $matches[0]);
42 2
        }, $this->expression);
43 2
        $expression = preg_replace('/\$\./', '$this->', $expression);
44 2
        $expression = preg_replace_callback('/"[^"]"|\'[^\']\'/', function (array $matches) {
45
            return str_replace('\\$.', '$.', $matches[0]);
46 2
        }, $expression);
47 2
        $this->expression = $expression;
48 2
        return $this;
49
    }
50
51
    /**
52
     * Resolves class names in *injected* callbacks and conditions.
53
     *
54
     * @param string $namespace namespace
55
     * 
56
     * @return InjectedStringSuit object for methods chain
57
     */
58
    public function resolveClassNames(string $namespace): self
59
    {
60 2
        $expression = preg_replace_callback('/"[^"]"|\'[^\']\'/', function (array $matches) {
61
            return str_replace(':', ':\\', $matches[0]);
62 2
        }, $this->expression);
63 2
        $expression = preg_replace('/(?<!:):(?=([a-zA-Z_][a-zA-Z0-9_]*))/', "$namespace\\", $expression);
64 2
        $expression = preg_replace_callback('/"[^"]"|\'[^\']\'/', function (array $matches) {
65
            return str_replace(':\\', ':', $matches[0]);
66 2
        }, $expression);
67 2
        $this->expression = $expression;
68 2
        return $this;
69
    }
70
71
    /**
72
     * Adds slashes to string.
73
     *
74
     * @param string $charlist symbols add slashes to
75
     * 
76
     * @return InjectedStringSuit object for methods chain 
77
     */
78 5
    public function addSlashes(string $charlist): self
79
    {
80 5
        $this->expression = preg_replace_callback(
81 5
            '/`([^`]|\\\\`)+((?<!\\\\)`)/',
82 5
            function (array $matches) use($charlist): string {
83 2
                return addcslashes($matches[0], $charlist);
84 5
            },
85 5
            $this->expression
86
        );
87 5
        return $this;
88
    }
89
    
90
    /**
91
     * Getter for InjectedStringProcessor::$expression.
92
     * 
93
     * @return string processed expression
94
     */
95 5
    public function get(): string 
96
    {
97 5
        return $this->expression;
98
    }
99
}