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

ConditionsProcessor   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 96.77%

Importance

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