|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Smoren\Validator\Factories; |
|
4
|
|
|
|
|
5
|
|
|
use Smoren\Validator\Checks\Check; |
|
6
|
|
|
use Smoren\Validator\Interfaces\CheckInterface; |
|
7
|
|
|
|
|
8
|
|
|
class CheckBuilder |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var string |
|
12
|
|
|
*/ |
|
13
|
|
|
protected string $name; |
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected string $errorName; |
|
18
|
|
|
/** |
|
19
|
|
|
* @var callable |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $predicate; |
|
22
|
|
|
/** |
|
23
|
|
|
* @var array<string, mixed> |
|
24
|
|
|
*/ |
|
25
|
|
|
protected array $params = []; |
|
26
|
|
|
/** |
|
27
|
|
|
* @var array<string, callable> |
|
28
|
|
|
*/ |
|
29
|
|
|
protected array $calculatedParams = []; |
|
30
|
|
|
/** |
|
31
|
|
|
* @var array<CheckInterface> |
|
32
|
|
|
*/ |
|
33
|
|
|
protected array $dependsOnChecks = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param string $name |
|
37
|
|
|
* @param string $errorName |
|
38
|
|
|
* @return self |
|
39
|
|
|
*/ |
|
40
|
39 |
|
public static function create(string $name, string $errorName): self |
|
41
|
|
|
{ |
|
42
|
39 |
|
return new self($name, $errorName); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return CheckInterface |
|
47
|
|
|
*/ |
|
48
|
39 |
|
public function build(): CheckInterface |
|
49
|
|
|
{ |
|
50
|
39 |
|
return new Check( |
|
51
|
39 |
|
$this->name, |
|
52
|
39 |
|
$this->errorName, |
|
53
|
39 |
|
$this->predicate, |
|
54
|
39 |
|
$this->params, |
|
55
|
39 |
|
$this->calculatedParams, |
|
56
|
39 |
|
$this->dependsOnChecks |
|
57
|
39 |
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param callable $predicate |
|
62
|
|
|
* @return $this |
|
63
|
|
|
*/ |
|
64
|
39 |
|
public function withPredicate(callable $predicate): self |
|
65
|
|
|
{ |
|
66
|
39 |
|
$this->predicate = $predicate; |
|
67
|
39 |
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param array<string, mixed> $params |
|
72
|
|
|
* @return $this |
|
73
|
|
|
*/ |
|
74
|
8 |
|
public function withParams(array $params): self |
|
75
|
|
|
{ |
|
76
|
8 |
|
$this->params = $params; |
|
77
|
8 |
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param array<string, callable> $calculatedParams |
|
82
|
|
|
* @return $this |
|
83
|
|
|
*/ |
|
84
|
3 |
|
public function withCalculatedParams(array $calculatedParams): self |
|
85
|
|
|
{ |
|
86
|
3 |
|
$this->calculatedParams = $calculatedParams; |
|
87
|
3 |
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param array<CheckInterface> $dependsOnChecks |
|
92
|
|
|
* @return $this |
|
93
|
|
|
*/ |
|
94
|
18 |
|
public function withDependOnChecks(array $dependsOnChecks): self |
|
95
|
|
|
{ |
|
96
|
18 |
|
$this->dependsOnChecks = $dependsOnChecks; |
|
97
|
18 |
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param string $name |
|
102
|
|
|
* @param string $errorName |
|
103
|
|
|
*/ |
|
104
|
39 |
|
private function __construct(string $name, string $errorName) |
|
105
|
|
|
{ |
|
106
|
39 |
|
$this->name = $name; |
|
107
|
39 |
|
$this->errorName = $errorName; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|