|
21 | 21 | /** @var array<Result> */ |
22 | 22 | public array $children; |
23 | 23 |
|
24 | | - public Id $id; |
25 | | - |
26 | 24 | /** @param array<string, mixed> $parameters */ |
27 | 25 | public function __construct( |
28 | 26 | public bool $hasPassed, |
29 | 27 | public mixed $input, |
30 | 28 | public Rule $rule, |
| 29 | + public Id $id, |
31 | 30 | public array $parameters = [], |
32 | 31 | public string $template = Rule::TEMPLATE_STANDARD, |
33 | 32 | public bool $hasInvertedMode = false, |
34 | 33 | public Name|null $name = null, |
35 | | - Id|null $id = null, |
36 | 34 | public Result|null $adjacent = null, |
37 | 35 | public Path|null $path = null, |
38 | 36 | Result ...$children, |
39 | 37 | ) { |
40 | | - $this->id = $id ?? Id::fromRule($rule); |
41 | 38 | $this->children = $children; |
42 | 39 | } |
43 | 40 |
|
44 | 41 | /** @param array<string, mixed> $parameters */ |
45 | | - public static function failed( |
| 42 | + public static function of( |
| 43 | + bool $hasPassed, |
46 | 44 | mixed $input, |
47 | 45 | Rule $rule, |
48 | 46 | array $parameters = [], |
49 | 47 | string $template = Rule::TEMPLATE_STANDARD, |
50 | 48 | ): self { |
51 | | - return new self(false, $input, $rule, $parameters, $template); |
| 49 | + return new self($hasPassed, $input, $rule, Id::fromRule($rule), $parameters, $template); |
52 | 50 | } |
53 | 51 |
|
54 | 52 | /** @param array<string, mixed> $parameters */ |
55 | | - public static function passed( |
| 53 | + public static function failed( |
56 | 54 | mixed $input, |
57 | 55 | Rule $rule, |
58 | 56 | array $parameters = [], |
59 | 57 | string $template = Rule::TEMPLATE_STANDARD, |
60 | 58 | ): self { |
61 | | - return new self(true, $input, $rule, $parameters, $template); |
| 59 | + return self::of(false, $input, $rule, $parameters, $template); |
62 | 60 | } |
63 | 61 |
|
64 | 62 | /** @param array<string, mixed> $parameters */ |
65 | | - public static function fromAdjacent( |
| 63 | + public static function passed( |
66 | 64 | mixed $input, |
67 | | - string $prefix, |
68 | 65 | Rule $rule, |
69 | | - Result $adjacent, |
70 | 66 | array $parameters = [], |
71 | 67 | string $template = Rule::TEMPLATE_STANDARD, |
72 | | - ): Result { |
73 | | - if ($adjacent->allowsAdjacent()) { |
74 | | - return (new Result( |
75 | | - $adjacent->hasPassed, |
76 | | - $input, |
77 | | - $rule, |
78 | | - $parameters, |
79 | | - $template, |
80 | | - id: $adjacent->id->withPrefix($prefix), |
81 | | - ))->withAdjacent($adjacent->withInput($input)); |
82 | | - } |
| 68 | + ): self { |
| 69 | + return self::of(true, $input, $rule, $parameters, $template); |
| 70 | + } |
83 | 71 |
|
84 | | - $childrenAsAdjacent = array_map( |
85 | | - static fn(Result $child) => self::fromAdjacent($input, $prefix, $rule, $child, $parameters, $template), |
86 | | - $adjacent->children, |
87 | | - ); |
| 72 | + public function asAdjacentOf(Result $result, string $prefix): Result |
| 73 | + { |
| 74 | + if ($this->allowsAdjacent()) { |
| 75 | + return clone ($result, [ |
| 76 | + 'id' => $this->id->withPrefix($prefix), |
| 77 | + 'adjacent' => $this->withInput($result->input), |
| 78 | + ]); |
| 79 | + } |
88 | 80 |
|
89 | | - return $adjacent->withInput($input)->withChildren(...$childrenAsAdjacent); |
| 81 | + return clone ($this, [ |
| 82 | + 'input' => $result->input, |
| 83 | + 'children' => array_map( |
| 84 | + static fn(Result $child) => $child->asAdjacentOf($result, $prefix), |
| 85 | + $this->children, |
| 86 | + ), |
| 87 | + ]); |
90 | 88 | } |
91 | 89 |
|
92 | 90 | public function withTemplate(string $template): self |
|
0 commit comments