Passed
Push — master ( f4bcab...7d4b66 )
by Maxim
07:25 queued 20s
created

ConditionsProcessor::processInputData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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