Passed
Push — master ( a22128...531530 )
by Sebastian
02:45
created

Filter::interpreteRules()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Linna Filter
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2017, Sebastian Rapetti
8
 * @license http://opensource.org/licenses/MIT MIT License
9
 */
10
declare(strict_types = 1);
11
12
namespace Linna\Filter;
13
14
use ReflectionClass;
15
use ReflectionMethod;
16
17
/**
18
 * Filter.
19
 */
20
class Filter
21
{
22
    /**
23
     * @var array User data.
24
     */
25
    private $data = [];
26
27
    /**
28
     * @var array Error messages.
29
     */
30
    private $messages = [];
31
32
    /**
33
     * @var int Occurred errors.
34
     */
35
    private $errors = 0;
36
37
    /**
38
     * Class constructor.
39
     *
40
     * @param array $rules
41
     * @param array $data
42
     */
43 41
    public function __construct(array $rules, array $data)
44
    {
45
        //$this->rules = $rules;
46 41
        $this->data = $data;
47
48 41
        $this->interpreteRules($rules);
49 41
    }
50
51
    /**
52
     * Return occurred error number.
53
     *
54
     * @return int
55
     */
56 41
    public function getErrors(): int
57
    {
58 41
        return $this->errors;
59
    }
60
61
    /**
62
     * Return error messages.
63
     *
64
     * @return array
65
     */
66 1
    public function getMessages(): array
67
    {
68 1
        return $this->messages;
69
    }
70
71
    /**
72
     * Return passed data.
73
     *
74
     * @return array
75
     */
76 14
    public function getData(): array
77
    {
78 14
        return $this->data;
79
    }
80
81
    /**
82
     * Get parsed rules.
83
     */
84 41
    private function interpreteRules(&$rules)
85
    {
86 41
        foreach ($rules as $rule) {
87 41
            $this->ruleToField((new RuleInterpreter($rule))->get());
88
        }
89 41
    }
90
91
    /**
92
     * Apply rules to a field.
93
     *
94
     * @param array $rules
95
     */
96 41
    private function ruleToField(array $rules)
97
    {
98 41
        foreach ($rules as $rule) {
99 41
            $field = $rule[0];
100 41
            $filter = $rule[2][0];
101
102 41
            $class = 'Linna\Filter\Rules\\' . $filter;
103 41
            $refClass = new ReflectionClass($class);
104 41
            $refMethod = new ReflectionMethod($class, 'validate');
105
                    
106 41
            $instance = $refClass->newInstance();
107
108 41
            if (!isset($this->data[$field])) {
109 3
                $this->errors++;
110 3
                $this->messages[$field][$filter] = "{$field} field missing";
111 3
                continue;
112
            }
113
114 39
            if ($refMethod->invokeArgs($instance, $this->getArguments($rule[2][2], $rule[3], $this->data[$field]))) {
115 16
                $this->errors++;
116 16
                $this->messages[$field][$filter] = ['expected' => $rule[3], 'received' => $this->data[$field]];
117 16
                continue;
118
            }
119
120 29
            if ($refClass->hasMethod('sanitize')) {
121 14
                $instance->sanitize($this->data[$field]);
122
            }
123
        }
124 41
    }
125
126
    /**
127
     * Return arguments for validation.
128
     *
129
     * @param int $args
130
     * @param mixed $expected
131
     * @param mixed $received
132
     *
133
     * @return array
134
     */
135 39
    private function getArguments(int $args, $expected, $received): array
136
    {
137 39
        if ($args === 0) {
138 21
            return [$received];
139
        }
140
141 31
        if (is_array($expected)) {
142 5
            array_unshift($expected, $received);
143 5
            return $expected;
144
        }
145
146 26
        return [$received, $expected];
147
    }
148
}
149