Validator::validate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 3
Bugs 2 Features 0
Metric Value
eloc 15
c 3
b 2
f 0
dl 0
loc 29
ccs 0
cts 19
cp 0
rs 9.7666
cc 3
nc 3
nop 2
crap 12
1
<?php
2
3
namespace Mediadevs\Validator;
4
5
use Mediadevs\Validator\Helpers\Arguments;
6
use Mediadevs\Validator\Factories\FilterFactory;
7
8
class Validator
9
{
10
    /**
11
     * The validation results will be stored in here.
12
     *
13
     * @var array
14
     */
15
    private $results = array();
16
17
    /**
18
     * Validating all the values and applying all the assigned filters.
19
     *
20
     * @param array $request
21
     * @param array $configuration
22
     *
23
     * @return array
24
     */
25
    public function validate(array $request, array $configuration = array()): array
26
    {
27
        $factory = new FilterFactory();
28
        $arguments = new Arguments();
29
30
        // Parsing through all the fields
31
        foreach ($arguments->getArguments($configuration) as $config) {
32
            // Assigning the variables for the factory
33
            $filter = $config['filter'];
34
            $thresholds = $config['thresholds'];
35
            $field = $config['field'];
36
37
            // Making sure $values is an array
38
            $values = is_array($request[$field]) ? $request[$field] : array($request[$field]);
39
40
            // Getting the configuration for the filter
41
            $results = $factory->build($filter, $values, $thresholds);
42
43
            // Creating a validation config for the current operation
44
            $this->results[] = [
45
                'valid'         => (bool) $results,
46
                'field'         => (string) $field,
47
                'filter'        => (string) $filter,
48
                'values'        => (array) $values,
49
                'thresholds'    => (array) $thresholds,
50
            ];
51
        }
52
53
        return $this->results;
54
    }
55
}
56