/var/www/html_us/wp-content/plugins/wp-smtp/vendor/stellarwp/validation/src/Validator.php


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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php

declare(strict_types=1);

namespace 
StellarWP\Validation;

use 
StellarWP\Validation\Commands\ExcludeValue;
use 
StellarWP\Validation\Commands\SkipValidationRules;
use 
StellarWP\Validation\Contracts\Sanitizer;

/**
 * A tool for taking in a set of values and corresponding validation rules, and then validating the values.
 *
 * @since 1.0.0
 */
class Validator
{
    
/**
     * @var array<string, ValidationRuleSet>
     */
    
private $ruleSets;

    
/**
     * @var array<string, mixed>
     */
    
private $values;

    
/**
     * @var array<string, string>
     */
    
private $labels;

    
/**
     * @var array<string, string>
     */
    
private $errors = [];

    
/**
     * @var array<string, mixed>
     */
    
private $validatedValues = [];

    
/**
     * @var bool
     */
    
private $ranValidationRules false;

    
/**
     * @since 1.0.0
     *
     * @param array<string, ValidationRuleSet|array> $ruleSets
     * @param array<string, mixed> $values
     */
    
public function __construct(array $ruleSets, array $values, array $labels = [])
    {
        
$validatedRules = [];
        foreach (
$ruleSets as $key => $rule) {
            if (
is_array($rule)) {
                
$validatedRules[$key] = Config::getServiceContainer()->get(ValidationRuleSet::class)->rules(...$rule);
            } elseif (
$rule instanceof ValidationRuleSet) {
                
$validatedRules[$key] = $rule;
            } else {
                
Config::throwInvalidArgumentException(
                    
'Validation rules must be an instance of ValidationRuleSet or a compatible array'
                
);
            }
        }

        
$this->ruleSets $validatedRules;
        
$this->values $values;
        
$this->labels $labels;
    }

    
/**
     * Returns whether the values failed validation or not.
     *
     * @since 1.0.0
     */
    
public function fails(): bool
    
{
        return !
$this->passes();
    }

    
/**
     * Returns whether the values passed validation or not.
     *
     * @since 1.0.0
     */
    
public function passes(): bool
    
{
        
$this->runValidationRules();

        return empty(
$this->errors);
    }

    
/**
     * Runs the validation rules on the values, and stores any resulting errors.
     * Will run only once, and then store the results for subsequent calls.
     *
     * @since 1.0.0
     *
     * @return void
     */
    
private function runValidationRules()
    {
        if (
$this->ranValidationRules) {
            return;
        }

        foreach (
$this->ruleSets as $key => $ruleSet) {
            
$label $this->labels[$key] ?? $key;
            
$value $this->values[$key] ?? null;

            
$fail = function (string $message) use ($key$label) {
                
$this->errors[$key] = str_ireplace('{field}'$label$message);
            };

            foreach (
$ruleSet as $rule) {
                
$command $rule($value$fail$key$this->values);

                if (
$command instanceof SkipValidationRules) {
                    break;
                }

                if (
$command instanceof ExcludeValue) {
                    
// Skip the rest of the rule and do not store the value
                    
continue 2;
                }

                if (
$rule instanceof Sanitizer) {
                    
$value $rule->sanitize($value);
                }
            }

            
$this->validatedValues[$key] = $value;
        }

        
$this->ranValidationRules true;
    }

    
/**
     * Returns the errors that were found during validation.
     *
     * @since 1.0.0
     *
     * @return array<string, string>
     */
    
public function errors(): array
    {
        
$this->runValidationRules();

        return 
$this->errors;
    }

    
/**
     * Returns the validated values, with any sanitization rules applied.
     *
     * @since 1.0.0
     */
    
public function validated(): array
    {
        
$this->runValidationRules();

        return 
$this->validatedValues;
    }
}