Completed
Push — master ( 4dcf1f...9ebefd )
by Maxim
02:52
created

ConditionsProcessor   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 84
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A processInputData() 0 3 1
A explodeConditions() 0 13 3
A processOutputData() 0 3 1
A makeConditionsTree() 0 15 4
A rmSlashes() 0 12 4
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 2
    public function processInputData(): array
26
    {
27 2
        return $this->makeConditionsTree($this->input);
28
    }
29
30
    /**
31
     * Creates list of conditions for output data.
32
     *
33
     * @return string[] conditions
34
     */
35 2
    public function processOutputData(): array
36
    {
37 2
        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 2
    private function explodeConditions(string $conditions): array
47
    {
48 2
        if ($conditions === '') {
49 2
            return [];
50
        }
51 2
        $result = [];
52 2
        $injProcessor = new InjectedStringSuit($conditions);
53 2
        $conditions = $injProcessor->addSlashes('|&');
54 2
        $conditions = preg_split('{\s*\|\|\s*}', $conditions);
55 2
        foreach ($conditions as $condition) {
56 2
            $result[] = preg_split('{\s*&&\s*}', $condition);
57
        }
58 2
        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 2
    private function rmSlashes(array $conditions): array 
68
    {
69 2
        foreach ($conditions as $number => &$complexCondition) {
70 2
            if (is_array($complexCondition)) {
71 2
                foreach ($complexCondition as $num => &$condition) {
72 2
                    $condition = stripcslashes($condition);
73
                }
74
            } else {
75 2
                $complexCondition = stripcslashes($complexCondition);
76
            }
77
        }
78 2
        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 2
    private function makeConditionsTree(string $conditions): array
88
    {
89 2
        $result = [];
90 2
        $conditions = $this->explodeConditions($conditions);
91 2
        foreach ($conditions as $number => $condition) {
92 2
            foreach ($condition as $token) {
93 2
                $injProcessor = new InjectedStringSuit($token);
94 2
                if (count($condition) === 1) {
95 2
                    $result[] = $injProcessor->resolveClassNames($this->namespace);
96
                } else {
97 2
                    $result[$number][] = $token;
98
                }
99
            }
100
        }
101 2
        return $result;
102
    }
103
}