summaryrefslogtreecommitdiff
path: root/src/Validator.php
blob: 8d91bb937e99c12c3a3f0b6a3d166be7c3f6b6e3 (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
<?php

declare(strict_types=1);

namespace Askonomm\Validator;

/**
 * The Validator class takes in an array of fields and 
 * an array of validators. Each field must have a key 
 * that matches the validator key so that `Validator` class
 * would know how to connect the two, and the value of the each 
 * validator is a string that consists of rules separated by the 
 * `|` character.
 * 
 * Example use:
 * 
 * ```php
 * $fields = ['email' => 'test@example.com'];
 * $validators = ['email' => 'required|email'];
 * $validator = new Validator($fields, $validators);
 * 
 * if ($validator->fails()) {
 *  return $validator->errors();
 * }
 * ```
 * 
 * @author Asko Nomm <asko@bien.ee>
 */
class Validator
{
    private array $errors = [];

    public function __construct(
        private array $fields,
        private array $validators,
        array $rules = [],
    ) {
        if (empty($rules)) {
            $this->rules = $this->defaultRules();
        }

        $this->validate();
    }

    public function defaultRules(): array
    {
        return [
            'len' => ValidatorRules::len(),
            'email' => ValidatorRules::email(),
            'required' => ValidatorRules::required(),
        ];
    }

    /**
     * Runs `$this->validators` over `$this->fields` to construct 
     * potential errors that will be stored as an array of strings 
     * in `$this->errors`.
     *
     * @return void
     */
    private function validate(): void
    {
        foreach ($this->validators as $field => $validator) {
            $value = $this->fields[$field];

            foreach (explode('|', $validator) as $item) {
                if (str_contains($item, ':')) {
                    [$name, $modifier] = explode(':', $item);

                    if (!$this->rules[$name]['validates']($value, $modifier)) {
                        $this->errors[] = $this->rules[$name]['error']($field, $modifier);
                    }
                } else {

                    if (!$this->rules[$item]['validates']($value)) {
                        $this->errors[] = $this->rules[$item]['error']($field);
                    }
                }
            }
        }
    }

    /**
     * Returns a boolean `true` if there have been any errors.
     * Returns `false` otherwise.
     *
     * @return boolean
     */
    public function fails(): bool
    {
        return count($this->errors) !== 0;
    }

    /**
     * Returns an array of strings where each string 
     * is a single error that happened during validation.
     */
    public function errors(): array
    {
        return $this->errors;
    }

    public function firstError(): string
    {
        if (count($this->errors) > 0) {
            return $this->errors[0];
        }

        return '';
    }
}