|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Marcosh\PhpValidationDSL\Result; |
|
6
|
|
|
|
|
7
|
|
|
use Closure; |
|
8
|
|
|
use function Marcosh\PhpValidationDSL\curry; |
|
9
|
|
|
use ReflectionFunction; |
|
10
|
|
|
use Webmozart\Assert\Assert; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @param callable $f with signature (T1 $a1, T2 $a2, ...) -> T |
|
14
|
|
|
* @return Closure with signature (ValidationResult T1, ValidationResult T2, ...) -> ValidationResult T |
|
15
|
|
|
* |
|
16
|
|
|
* @psalm-return Closure(): ValidationResult |
|
17
|
|
|
*/ |
|
18
|
|
|
function lift(callable $f): Closure |
|
19
|
|
|
{ |
|
20
|
|
|
$innerLift = |
|
21
|
|
|
/** |
|
22
|
|
|
* @return Closure |
|
23
|
|
|
* |
|
24
|
|
|
* @psalm-return Closure(): ValidationResult |
|
25
|
|
|
*/ |
|
26
|
|
|
static function ( |
|
27
|
|
|
callable $f, |
|
28
|
|
|
?int $numberOfParameters = null, |
|
29
|
|
|
array $parameters = [] |
|
30
|
|
|
) use (&$innerLift): Closure { |
|
31
|
|
|
if (null === $numberOfParameters) { |
|
32
|
|
|
// retrieve number of parameters from reflection |
|
33
|
|
|
$fClosure = Closure::fromCallable($f); |
|
34
|
|
|
$fRef = new ReflectionFunction($fClosure); |
|
35
|
|
|
$numberOfParameters = $fRef->getNumberOfParameters(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** @return ValidationResult|Closure */ |
|
39
|
|
|
/** @psalm-suppress MissingClosureReturnType */ |
|
40
|
|
|
return static function () use ($f, $numberOfParameters, $parameters, $innerLift) { |
|
41
|
|
|
// collect all necessary parameters |
|
42
|
|
|
|
|
43
|
|
|
/** @var array<int, mixed> $newParameters */ |
|
44
|
|
|
$newParameters = array_merge($parameters, func_get_args()); |
|
45
|
|
|
|
|
46
|
|
|
if (count($newParameters) >= $numberOfParameters) { |
|
47
|
|
|
if ([] === $newParameters) { |
|
48
|
|
|
return ValidationResult::valid($f()); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$firstParameter = $newParameters[0]; |
|
52
|
|
|
|
|
53
|
|
|
$result = $firstParameter->map(curry($f)); |
|
54
|
|
|
|
|
55
|
|
|
foreach (array_slice($newParameters, 1) as $validatedParameter) { |
|
56
|
|
|
$result = $validatedParameter->apply($result); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $result; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $innerLift($f, $numberOfParameters, $newParameters); |
|
63
|
|
|
}; |
|
64
|
|
|
}; |
|
65
|
|
|
|
|
66
|
|
|
return $innerLift($f); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param callable[] $fs every function takes as arguments the unwrapped results of the previous one and returns a |
|
71
|
|
|
* ValidationResult |
|
72
|
|
|
* @return ValidationResult |
|
73
|
|
|
*/ |
|
74
|
|
|
function sdo(callable ...$fs): ValidationResult |
|
75
|
|
|
{ |
|
76
|
|
|
Assert::notEmpty($fs, 'do_ must receive at least one callable'); |
|
77
|
|
|
|
|
78
|
|
|
$result = ValidationResult::valid(null); |
|
79
|
|
|
|
|
80
|
|
|
foreach ($fs as $f) { |
|
81
|
|
|
$result = $result->bind($f); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $result; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
final class DoPartialTempResult |
|
88
|
|
|
{ |
|
89
|
|
|
/** |
|
90
|
|
|
* @var callable |
|
91
|
|
|
*/ |
|
92
|
|
|
private $f; |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @var array |
|
96
|
|
|
*/ |
|
97
|
|
|
private $arguments; |
|
98
|
|
|
|
|
99
|
|
|
private function __construct(callable $f, array $arguments) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->f = $f; |
|
102
|
|
|
$this->arguments = $arguments; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public static function fromCallableAndArguments(callable $f, array $arguments): ValidationResult |
|
106
|
|
|
{ |
|
107
|
|
|
return ValidationResult::valid(new self($f, $arguments)); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public static function fromPreviousAndCallable(ValidationResult $previous, callable $f): ValidationResult |
|
111
|
|
|
{ |
|
112
|
|
|
return $previous->bind(function (DoPartialTempResult $doPartialTempResult) use ($f): ValidationResult { |
|
113
|
|
|
$lastArgumentResult = $doPartialTempResult(); |
|
114
|
|
|
|
|
115
|
|
|
/** @psalm-suppress MissingClosureParamType */ |
|
116
|
|
|
return $lastArgumentResult->bind(function ($lastArgument) use ($doPartialTempResult, $f): ValidationResult { |
|
117
|
|
|
$fArguments = array_merge($doPartialTempResult->arguments, [$lastArgument]); |
|
118
|
|
|
|
|
119
|
|
|
return self::fromCallableAndArguments($f, $fArguments); |
|
120
|
|
|
}); |
|
121
|
|
|
}); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function __invoke(): ValidationResult |
|
125
|
|
|
{ |
|
126
|
|
|
return call_user_func_array($this->f, $this->arguments); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param callable[] $fs every function takes as arguments the unwrapped results of the previous one and returns a |
|
132
|
|
|
* ValidationResult |
|
133
|
|
|
* @return ValidationResult |
|
134
|
|
|
*/ |
|
135
|
|
|
function mdo(callable ...$fs): ValidationResult |
|
136
|
|
|
{ |
|
137
|
|
|
Assert::notEmpty($fs, 'do__ must receive at least one callable'); |
|
138
|
|
|
|
|
139
|
|
|
$doPartialTempResult = DoPartialTempResult::fromCallableAndArguments($fs[0], []); |
|
140
|
|
|
|
|
141
|
|
|
foreach (array_slice($fs, 1) as $f) { |
|
142
|
|
|
$doPartialTempResult = DoPartialTempResult::fromPreviousAndCallable($doPartialTempResult, $f); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return $doPartialTempResult->bind(function (DoPartialTempResult $doPartialTempResult): ValidationResult { |
|
146
|
|
|
return $doPartialTempResult(); |
|
147
|
|
|
}); |
|
148
|
|
|
} |
|
149
|
|
|
|