Validate   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Test Coverage

Coverage 91.49%

Importance

Changes 0
Metric Value
eloc 48
dl 0
loc 169
ccs 43
cts 47
cp 0.9149
rs 10
c 0
b 0
f 0
wmc 22

10 Methods

Rating   Name   Duplication   Size   Complexity  
A that() 0 3 1
A isUrl() 0 7 2
A now() 0 18 6
A isEmail() 0 7 1
A allInstanceOf() 0 13 3
A is() 0 6 2
A using() 0 7 1
A isStream() 0 7 2
A isFile() 0 7 2
A hasMinLength() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpEmail;
6
7
use Closure;
8
9
/**
10
 * @internal
11
 */
12
class Validate
13
{
14
    /**
15
     * @var array
16
     */
17
    private $validations = [];
18
19
    /**
20
     * @param string  $property   The name of the property the function validates
21
     * @param Closure $validation
22
     * @param string  $message
23
     */
24 27
    public function is($property, Closure $validation, $message)
25
    {
26 27
        if (array_key_exists($property, $this->validations)) {
27 1
            $this->validations[$property][] = [$validation, $message];
28
        } else {
29 27
            $this->validations[$property] = [[$validation, $message]];
30
        }
31
    }
32
33
    /**
34
     * @return Validate
35
     */
36 27
    public static function that(): self
37
    {
38 27
        return new static();
39
    }
40
41
    /**
42
     * A flexible validation supporting a Closure rule.
43
     *
44
     * @param         $property
45
     * @param         $value
46
     * @param Closure $validation
47
     * @param         $message
48
     *
49
     * @return $this
50
     */
51
    public function using($property, $value, Closure $validation, $message)
52
    {
53
        $this->is($property, function () use ($value, $validation) {
54
            return $validation($value);
55
        }, $message);
56
57
        return $this;
58
    }
59
60
    /**
61
     * @param string $property
62
     * @param mixed  $value
63
     *
64
     * @return Validate
65
     */
66
    public function isEmail($property, $value): self
67
    {
68 19
        $this->is($property, function () use ($value) {
69 19
            return filter_var($value, FILTER_VALIDATE_EMAIL);
70 19
        }, sprintf('Value expected to be a valid email address.'));
71
72 19
        return $this;
73
    }
74
75
    /**
76
     * @param string $property
77
     * @param array  $list
78
     * @param string $type
79
     *
80
     * @return Validate
81
     */
82
    public function allInstanceOf($property, array $list, $type): self
83
    {
84 2
        $this->is($property, function () use ($list, $type) {
85 2
            foreach ($list as $element) {
86 2
                if (!$element instanceof $type) {
87 2
                    return false;
88
                }
89
            }
90
91 1
            return true;
92 2
        }, sprintf('All values were expected to be type %s', $type));
93
94 2
        return $this;
95
    }
96
97
    /**
98
     * @param string $property
99
     * @param mixed  $value
100
     * @param int    $length
101
     *
102
     * @return Validate
103
     */
104
    public function hasMinLength($property, $value, $length): self
105
    {
106 3
        $this->is($property, function () use ($value, $length) {
107 3
            return is_string($value) && mb_strlen($value) >= $length;
108 3
        }, sprintf('Value must have a minimum length of %d', $length));
109
110 3
        return $this;
111
    }
112
113
    /**
114
     * @param string $property
115
     * @param mixed  $value
116
     *
117
     * @return Validate
118
     */
119
    public function isFile($property, $value): self
120
    {
121 2
        $this->is($property, function () use ($value) {
122 2
            return is_string($value) && is_file($value);
123 2
        }, sprintf('Value must be a file that exists in the system'));
124
125 2
        return $this;
126
    }
127
128
    /**
129
     * @param string $property
130
     * @param mixed  $value
131
     *
132
     * @return Validate
133
     */
134
    public function isStream($property, $value): self
135
    {
136 2
        $this->is($property, function () use ($value) {
137 2
            return is_resource($value) && get_resource_type($value) === 'stream';
138 2
        }, sprintf('Value must be a stream resource'));
139
140 2
        return $this;
141
    }
142
143
    /**
144
     * @param string $property
145
     * @param mixed  $value
146
     *
147
     * @return Validate
148
     */
149
    public function isUrl($property, $value): self
150
    {
151 2
        $this->is($property, function () use ($value) {
152 2
            return is_string($value) && filter_var($value, FILTER_VALIDATE_URL);
153 2
        }, sprintf('Value must be a valid URL, beginning with a scheme'));
154
155 2
        return $this;
156
    }
157
158
    /**
159
     * @throws ValidationException
160
     *
161
     * @return void
162
     */
163 27
    public function now(): void
164
    {
165 27
        $exceptions = [];
166
167 27
        foreach ($this->validations as $property => $validation) {
168 27
            foreach ($validation as $expectation) {
169 27
                if (!call_user_func($expectation[0])) {
170 13
                    if (array_key_exists($property, $exceptions)) {
171 1
                        $exceptions[$property][] = $expectation[1];
172
                    } else {
173 27
                        $exceptions[$property] = [$expectation[1]];
174
                    }
175
                }
176
            }
177
        }
178
179 27
        if ($exceptions) {
180 13
            throw ValidationException::fromArray($exceptions);
181
        }
182
    }
183
}
184