1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace hanneskod\clean; |
6
|
|
|
|
7
|
|
|
final class Rule implements ValidatorInterface |
8
|
|
|
{ |
9
|
|
|
private ?string $default; |
|
|
|
|
10
|
|
|
private ?string $errorMsg; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var array<callable> Pre match filters |
14
|
|
|
*/ |
15
|
|
|
private array $pre; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array<callable> Post match filters |
19
|
|
|
*/ |
20
|
|
|
private array $post; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array<callable> |
24
|
|
|
*/ |
25
|
|
|
private array $matchers; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param array<callable> $pre |
29
|
|
|
* @param array<callable> $matchers |
30
|
|
|
* @param array<callable> $post |
31
|
|
|
*/ |
32
|
|
|
public function __construct( |
33
|
|
|
array $pre = [], |
34
|
|
|
array $matchers = [], |
35
|
|
|
array $post = [], |
36
|
|
|
?string $default = null, |
37
|
|
|
?string $errorMsg = null |
38
|
|
|
) { |
39
|
|
|
$this->pre = $pre; |
40
|
|
|
$this->post = $post; |
41
|
|
|
$this->matchers = $matchers; |
42
|
|
|
$this->default = $default; |
43
|
|
|
$this->errorMsg = $errorMsg; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Create a new Rule with one or more pre match filters |
48
|
|
|
* |
49
|
|
|
* A filter should take a raw value and return the filtered value. |
50
|
|
|
*/ |
51
|
|
|
public function pre(callable ...$pre): Rule |
52
|
|
|
{ |
53
|
|
|
return new static([...$this->pre, ...$pre], $this->matchers, $this->post, $this->default, $this->errorMsg); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Create a new Rule with one or more matchers |
58
|
|
|
* |
59
|
|
|
* A matcher should take a raw value and return true if value is a match |
60
|
|
|
* and false if it is not. |
61
|
|
|
*/ |
62
|
|
|
public function match(callable ...$match): Rule |
63
|
|
|
{ |
64
|
|
|
return new static($this->pre, [...$this->matchers, ...$match], $this->post, $this->default, $this->errorMsg); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Create a new Rule with a regular expression matcher |
69
|
|
|
* |
70
|
|
|
* $regexp should be a regular expression consumable by preg_match(). |
71
|
|
|
*/ |
72
|
|
|
public function regexp(string $regexp): Rule |
73
|
|
|
{ |
74
|
|
|
return $this->match('is_string', function (string $value) use ($regexp): bool { |
75
|
|
|
return (bool)preg_match($regexp, $value); |
76
|
|
|
}); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Create a new Rule with one or more post match filters |
81
|
|
|
* |
82
|
|
|
* A filter should take a raw value and return the filtered value. |
83
|
|
|
*/ |
84
|
|
|
public function post(callable ...$post): Rule |
85
|
|
|
{ |
86
|
|
|
return new static($this->pre, $this->matchers, [...$this->post, ...$post], $this->default, $this->errorMsg); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Create a new Rule with default value |
91
|
|
|
*/ |
92
|
|
|
public function def(string $default): Rule |
93
|
|
|
{ |
94
|
|
|
return new static($this->pre, $this->matchers, $this->post, $default, $this->errorMsg); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Create a new Rule with exception message |
99
|
|
|
* |
100
|
|
|
* %s is replaced with parent exception message |
101
|
|
|
*/ |
102
|
|
|
public function msg(string $errorMsg): Rule |
103
|
|
|
{ |
104
|
|
|
return new static($this->pre, $this->matchers, $this->post, $this->default, $errorMsg); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function applyTo($data): ResultInterface |
108
|
|
|
{ |
109
|
|
|
try { |
110
|
|
|
if (is_null($data)) { |
111
|
|
|
if (!isset($this->default)) { |
112
|
|
|
throw new Exception('value missing'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$data = $this->default; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
foreach ($this->pre as $filter) { |
119
|
|
|
$data = $filter($data); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
foreach ($this->matchers as $matcherId => $matcher) { |
123
|
|
|
if (!$matcher($data)) { |
124
|
|
|
throw new Exception("matcher #$matcherId failed"); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
foreach ($this->post as $filter) { |
129
|
|
|
$data = $filter($data); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return new Valid($data); |
133
|
|
|
} catch (\Throwable $e) { |
134
|
|
|
if (isset($this->errorMsg)) { |
135
|
|
|
$e = new Exception(sprintf($this->errorMsg, $e->getMessage()), 0, $e); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return new Invalid($e); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function validate($data) |
143
|
|
|
{ |
144
|
|
|
return $this->applyTo($data)->getValidData(); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|