summaryrefslogtreecommitdiff
path: root/src/Http/Validation/Validator.php
blob: 976f56824429f8d82e13375bc1f44ce1f55b177a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php

namespace Engelsystem\Http\Validation;

use Illuminate\Support\Str;
use InvalidArgumentException;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Validator as RespectValidator;

class Validator
{
    /** @var string[] */
    protected $errors = [];

    /** @var array */
    protected $data = [];

    /** @var array */
    protected $mapping = [
        'accepted' => 'TrueVal',
        'int'      => 'IntVal',
        'required' => 'NotEmpty',
    ];

    /** @var array */
    protected $nestedRules = ['optional', 'not'];

    /**
     * @param array $data
     * @param array $rules
     * @return bool
     */
    public function validate($data, $rules)
    {
        $this->errors = [];
        $this->data = [];

        foreach ($rules as $key => $values) {
            $v = new RespectValidator();
            $v->with('\\Engelsystem\\Http\\Validation\\Rules', true);

            $value = isset($data[$key]) ? $data[$key] : null;
            $values = explode('|', $values);

            $packing = [];
            foreach ($this->nestedRules as $rule) {
                if (in_array($rule, $values)) {
                    $packing[] = $rule;
                }
            }

            $values = array_diff($values, $this->nestedRules);
            foreach ($values as $parameters) {
                $parameters = explode(':', $parameters);
                $rule = array_shift($parameters);
                $rule = Str::camel($rule);
                $rule = $this->map($rule);

                // To allow rules nesting
                $w = $v;
                try {
                    foreach (array_reverse(array_merge($packing, [$rule])) as $rule) {
                        if (!in_array($rule, $this->nestedRules)) {
                            call_user_func_array([$w, $rule], $parameters);
                            continue;
                        }

                        $w = call_user_func_array([new RespectValidator(), $rule], [$w]);
                    }
                } catch (ComponentException $e) {
                    throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
                }

                if ($w->validate($value)) {
                    $this->data[$key] = $value;
                } else {
                    $this->errors[$key][] = implode('.', ['validation', $key, $this->mapBack($rule)]);
                }

                $v->removeRules();
            }
        }

        return empty($this->errors);
    }

    /**
     * @param string $rule
     * @return string
     */
    protected function map($rule)
    {
        return $this->mapping[$rule] ?? $rule;
    }

    /**
     * @param string $rule
     * @return string
     */
    protected function mapBack($rule)
    {
        $mapping = array_flip($this->mapping);

        return $mapping[$rule] ?? $rule;
    }

    /**
     * @return array
     */
    public function getData(): array
    {
        return $this->data;
    }

    /**
     * @return string[]
     */
    public function getErrors(): array
    {
        return $this->errors;
    }
}