Passed
Push — master ( ce20c6...df670c )
by Maxim
02:56
created

ConditionsProcessor::explodeConditions()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 16
nc 6
nop 1
dl 0
loc 24
ccs 0
cts 17
cp 0
crap 30
rs 8.5125
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 ConditionsProcessor.
13
 * 
14
 * Processes Axessors conditions.
15
 * 
16
 * @package NoOne4rever\Axessors
17
 */
18
class ConditionsProcessor
19
{
20
    private $inputConditions;
21
    private $outputConditions;
22
    private $namespace;
23
    
24
    public function __construct(string $in, string $out, string $namespace)
25
    {
26
        $this->inputConditions = $in;
27
        $this->outputConditions = $out;
28
        $this->namespace = $namespace;
29
    }
30
31
    /**
32
     * Creates list of conditions for input data.
33
     *
34
     * @return string[] conditions
35
     */
36
    public function processInputConditions(): array
37
    {
38
        return $this->inputConditions === '' ? [] : $this->makeConditionsTree($this->inputConditions);
39
    }
40
41
    /**
42
     * Creates list of conditions for output data.
43
     *
44
     * @return string[] conditions
45
     */
46
    public function processOutputConditions(): array
47
    {
48
        return $this->outputConditions === '' ? [] : $this->makeConditionsTree($this->outputConditions);
49
    }
50
51
    /**
52
     * Creates list of conditions from a string of conditions definition.
53
     *
54
     * @param string $conditions conditions
55
     * @return array conditions
56
     */
57
    private function explodeConditions(string $conditions): array
58
    {
59
        $result = [];
60
        $conditions = preg_replace_callback(
61
            '{`([^`]|\\\\`)+((?<!\\\\)`)}',
62
            function (array $matches) {
63
                return addcslashes($matches[0], '&|');
64
            },
65
            $conditions
66
        );
67
        $conditions = preg_split('{\s*\|\|\s*}', $conditions);
68
        foreach ($conditions as $condition) {
69
            $result[] = preg_split('{\s*&&\s*}', $condition);
70
        }
71
        foreach ($result as $number => &$complexCondition) {
72
            if (is_array($complexCondition)) {
73
                foreach ($complexCondition as $num => &$condition) {
74
                    $condition = stripcslashes($condition);
75
                }
76
            } else {
77
                $complexCondition = stripcslashes($complexCondition);
78
            }
79
        }
80
        return $result;
81
    }
82
83
    /**
84
     * Makes tree of conditions.
85
     *
86
     * @param string $conditions string with conditions definition
87
     * @return array tree of conditions
88
     */
89
    private function makeConditionsTree(string $conditions): array
90
    {
91
        $result = [];
92
        $conditions = $this->explodeConditions($conditions);
93
        foreach ($conditions as $number => $condition) {
94
            foreach ($condition as $token) {
95
                if (count($condition) === 1) {
96
                    $injProcessor = new InjectedStringParser($token);
97
                    $result[] = $injProcessor->resolveClassNames($this->namespace);
98
                } else {
99
                    $result[$number][] = $token;
100
                }
101
            }
102
        }
103
        return $result;
104
    }
105
}