Completed
Push — master ( ec8a9b...257aff )
by Maxim
05:03 queued 02:03
created

InjectedStringSuit::__construct()   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 1
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 1
    public function __construct(string $expression)
29
    {
30 1
        $this->expression = $expression;
31 1
    }
32
33
    /**
34
     * Resolves class names in *injected* callbacks and conditions.
35
     *
36
     * @param string $namespace namespace
37
     * @return string expression with resolved class names
38
     */
39
    public function resolveClassNames(string $namespace): string
40
    {
41 1
        $expression = preg_replace_callback('/"[^"]"|\'[^\']\'/', function (array $matches) {
42
            return str_replace(':', ':\\', $matches[0]);
43 1
        }, $this->expression);
44 1
        $expression = preg_replace('/(?<!:):(?=([a-zA-Z_][a-zA-Z0-9_]*))/', "$namespace\\", $expression);
45 1
        $expression = preg_replace_callback('/"[^"]"|\'[^\']\'/', function (array $matches) {
46
            return str_replace(':\\', ':', $matches[0]);
47 1
        }, $expression);
48 1
        return $expression;
49
    }
50
51
    /**
52
     * Adds slashes to string.
53
     * 
54
     * @param string $charlist symbols add slashes to
55
     * @return string string with slashes
56
     */
57 1
    public function addSlashes(string $charlist): string 
58
    {
59 1
        return preg_replace_callback(
60 1
            '/`([^`]|\\\\`)+((?<!\\\\)`)/',
61 1
            function (array $matches) use($charlist): string {
62 1
                return addcslashes($matches[0], $charlist);
63 1
            },
64
            $this->expression
65
        );
66
    }
67
}