diff --git a/library/Result.php b/library/Result.php index 5d44392d8..f8f01ec11 100644 --- a/library/Result.php +++ b/library/Result.php @@ -21,72 +21,70 @@ /** @var array */ public array $children; - public Id $id; - /** @param array $parameters */ public function __construct( public bool $hasPassed, public mixed $input, public Rule $rule, + public Id $id, public array $parameters = [], public string $template = Rule::TEMPLATE_STANDARD, public bool $hasInvertedMode = false, public Name|null $name = null, - Id|null $id = null, public Result|null $adjacent = null, public Path|null $path = null, Result ...$children, ) { - $this->id = $id ?? Id::fromRule($rule); $this->children = $children; } /** @param array $parameters */ - public static function failed( + public static function of( + bool $hasPassed, mixed $input, Rule $rule, array $parameters = [], string $template = Rule::TEMPLATE_STANDARD, ): self { - return new self(false, $input, $rule, $parameters, $template); + return new self($hasPassed, $input, $rule, Id::fromRule($rule), $parameters, $template); } /** @param array $parameters */ - public static function passed( + public static function failed( mixed $input, Rule $rule, array $parameters = [], string $template = Rule::TEMPLATE_STANDARD, ): self { - return new self(true, $input, $rule, $parameters, $template); + return self::of(false, $input, $rule, $parameters, $template); } /** @param array $parameters */ - public static function fromAdjacent( + public static function passed( mixed $input, - string $prefix, Rule $rule, - Result $adjacent, array $parameters = [], string $template = Rule::TEMPLATE_STANDARD, - ): Result { - if ($adjacent->allowsAdjacent()) { - return (new Result( - $adjacent->hasPassed, - $input, - $rule, - $parameters, - $template, - id: $adjacent->id->withPrefix($prefix), - ))->withAdjacent($adjacent->withInput($input)); - } + ): self { + return self::of(true, $input, $rule, $parameters, $template); + } - $childrenAsAdjacent = array_map( - static fn(Result $child) => self::fromAdjacent($input, $prefix, $rule, $child, $parameters, $template), - $adjacent->children, - ); + public function asAdjacentOf(Result $result, string $prefix): Result + { + if ($this->allowsAdjacent()) { + return clone ($result, [ + 'id' => $this->id->withPrefix($prefix), + 'adjacent' => $this->withInput($result->input), + ]); + } - return $adjacent->withInput($input)->withChildren(...$childrenAsAdjacent); + return clone ($this, [ + 'input' => $result->input, + 'children' => array_map( + static fn(Result $child) => $child->asAdjacentOf($result, $prefix), + $this->children, + ), + ]); } public function withTemplate(string $template): self diff --git a/library/Rules/AllOf.php b/library/Rules/AllOf.php index ccdefe10a..ea6b8fcc8 100644 --- a/library/Rules/AllOf.php +++ b/library/Rules/AllOf.php @@ -46,6 +46,6 @@ public function evaluate(mixed $input): Result $template = self::TEMPLATE_ALL; } - return (new Result($valid, $input, $this, [], $template))->withChildren(...$children); + return Result::of($valid, $input, $this, [], $template)->withChildren(...$children); } } diff --git a/library/Rules/AnyOf.php b/library/Rules/AnyOf.php index c47826eaf..010e21736 100644 --- a/library/Rules/AnyOf.php +++ b/library/Rules/AnyOf.php @@ -34,6 +34,6 @@ public function evaluate(mixed $input): Result false, ); - return (new Result($valid, $input, $this))->withChildren(...$children); + return Result::of($valid, $input, $this)->withChildren(...$children); } } diff --git a/library/Rules/Base.php b/library/Rules/Base.php index bdc4e6282..204ee0096 100644 --- a/library/Rules/Base.php +++ b/library/Rules/Base.php @@ -38,7 +38,7 @@ public function __construct( public function evaluate(mixed $input): Result { - return new Result( + return Result::of( (bool) preg_match('@^[' . mb_substr($this->chars, 0, $this->base) . ']+$@', (string) $input), $input, $this, diff --git a/library/Rules/Charset.php b/library/Rules/Charset.php index c5ea2d140..67db1b36f 100644 --- a/library/Rules/Charset.php +++ b/library/Rules/Charset.php @@ -47,7 +47,7 @@ public function __construct(string $charset, string ...$charsets) public function evaluate(mixed $input): Result { - return new Result( + return Result::of( in_array(mb_detect_encoding($input, $this->charset, true), $this->charset), $input, $this, diff --git a/library/Rules/Contains.php b/library/Rules/Contains.php index 78c68bf10..cbbf95d1f 100644 --- a/library/Rules/Contains.php +++ b/library/Rules/Contains.php @@ -37,14 +37,14 @@ public function evaluate(mixed $input): Result { $parameters = ['containsValue' => $this->containsValue]; if (is_array($input)) { - return new Result(in_array($this->containsValue, $input, $this->identical), $input, $this, $parameters); + return Result::of(in_array($this->containsValue, $input, $this->identical), $input, $this, $parameters); } if (!is_scalar($input) || !is_scalar($this->containsValue)) { return Result::failed($input, $this, $parameters); } - return new Result( + return Result::of( $this->validateString((string) $input, (string) $this->containsValue), $input, $this, diff --git a/library/Rules/Core/Comparison.php b/library/Rules/Core/Comparison.php index ade8739d9..1fe963f16 100644 --- a/library/Rules/Core/Comparison.php +++ b/library/Rules/Core/Comparison.php @@ -33,7 +33,7 @@ public function evaluate(mixed $input): Result return Result::failed($input, $this, $parameters); } - return new Result($this->compare($left, $right), $input, $this, $parameters); + return Result::of($this->compare($left, $right), $input, $this, $parameters); } abstract protected function compare(mixed $left, mixed $right): bool; diff --git a/library/Rules/Core/Envelope.php b/library/Rules/Core/Envelope.php index 8acd2d340..5b9706d0e 100644 --- a/library/Rules/Core/Envelope.php +++ b/library/Rules/Core/Envelope.php @@ -23,6 +23,6 @@ public function __construct( public function evaluate(mixed $input): Result { - return new Result($this->rule->evaluate($input)->hasPassed, $input, $this, $this->parameters); + return Result::of($this->rule->evaluate($input)->hasPassed, $input, $this, $this->parameters); } } diff --git a/library/Rules/Core/FilteredString.php b/library/Rules/Core/FilteredString.php index 9a62128ad..bf9f2d0cc 100644 --- a/library/Rules/Core/FilteredString.php +++ b/library/Rules/Core/FilteredString.php @@ -44,7 +44,7 @@ public function evaluate(mixed $input): Result $filteredInput = $this->filter($stringInput); $isValid = $filteredInput === '' || $this->isValid($filteredInput); - return new Result($isValid, $input, $this, $parameters, $template); + return Result::of($isValid, $input, $this, $parameters, $template); } abstract protected function isValid(string $input): bool; diff --git a/library/Rules/Core/Simple.php b/library/Rules/Core/Simple.php index 057be969e..69546bb22 100644 --- a/library/Rules/Core/Simple.php +++ b/library/Rules/Core/Simple.php @@ -18,6 +18,6 @@ abstract public function isValid(mixed $input): bool; public function evaluate(mixed $input): Result { - return new Result($this->isValid($input), $input, $this); + return Result::of($this->isValid($input), $input, $this); } } diff --git a/library/Rules/CreditCard.php b/library/Rules/CreditCard.php index 1bdd0e0ff..6bdaff055 100644 --- a/library/Rules/CreditCard.php +++ b/library/Rules/CreditCard.php @@ -79,7 +79,7 @@ public function evaluate(mixed $input): Result return Result::failed($input, $this, $parameters, $template); } - return new Result( + return Result::of( preg_match(self::BRAND_REGEX_LIST[$this->brand], $filteredInput) > 0, $input, $this, diff --git a/library/Rules/CurrencyCode.php b/library/Rules/CurrencyCode.php index 1d9ed1346..5a94ccd9c 100644 --- a/library/Rules/CurrencyCode.php +++ b/library/Rules/CurrencyCode.php @@ -61,6 +61,6 @@ public function evaluate(mixed $input): Result 'numeric' => $this->currencies->getByNumericCode($input), }; - return new Result($currency !== null, $input, $this); + return Result::of($currency !== null, $input, $this); } } diff --git a/library/Rules/Date.php b/library/Rules/Date.php index 586ce7596..43972483e 100644 --- a/library/Rules/Date.php +++ b/library/Rules/Date.php @@ -45,6 +45,6 @@ public function evaluate(mixed $input): Result return Result::failed($input, $this, $parameters); } - return new Result($this->isDateTime($this->format, (string) $input), $input, $this, $parameters); + return Result::of($this->isDateTime($this->format, (string) $input), $input, $this, $parameters); } } diff --git a/library/Rules/DateTime.php b/library/Rules/DateTime.php index 9613726b0..c6beff7f5 100644 --- a/library/Rules/DateTime.php +++ b/library/Rules/DateTime.php @@ -47,7 +47,7 @@ public function evaluate(mixed $input): Result $template = $this->format !== null ? self::TEMPLATE_FORMAT : self::TEMPLATE_STANDARD; $parameters = ['sample' => date($this->format ?: 'c', strtotime('2005-12-30 01:02:03'))]; if ($input instanceof DateTimeInterface) { - return new Result($this->format === null, $input, $this, $parameters, $template); + return Result::of($this->format === null, $input, $this, $parameters, $template); } if (!is_scalar($input)) { @@ -55,9 +55,9 @@ public function evaluate(mixed $input): Result } if ($this->format === null) { - return new Result(strtotime((string) $input) !== false, $input, $this, $parameters, $template); + return Result::of(strtotime((string) $input) !== false, $input, $this, $parameters, $template); } - return new Result($this->isDateTime($this->format, (string) $input), $input, $this, $parameters, $template); + return Result::of($this->isDateTime($this->format, (string) $input), $input, $this, $parameters, $template); } } diff --git a/library/Rules/DateTimeDiff.php b/library/Rules/DateTimeDiff.php index c32499231..b30b9b82b 100644 --- a/library/Rules/DateTimeDiff.php +++ b/library/Rules/DateTimeDiff.php @@ -81,13 +81,17 @@ public function evaluate(mixed $input): Result $nowPlaceholder = $this->nowParameter($now); - return Result::fromAdjacent( - $input, + $result = $this->rule->evaluate($this->comparisonValue($now, $compareTo)); + + return $result->asAdjacentOf( + Result::of( + $result->hasPassed, + $input, + $this, + ['type' => $this->type, 'now' => $nowPlaceholder], + $nowPlaceholder === 'now' ? self::TEMPLATE_STANDARD : self::TEMPLATE_CUSTOMIZED, + ), 'dateTimeDiff', - $this, - $this->rule->evaluate($this->comparisonValue($now, $compareTo)), - ['type' => $this->type, 'now' => $nowPlaceholder], - $nowPlaceholder === 'now' ? self::TEMPLATE_STANDARD : self::TEMPLATE_CUSTOMIZED, ); } diff --git a/library/Rules/Decimal.php b/library/Rules/Decimal.php index 111183b2f..e39e5cc6d 100644 --- a/library/Rules/Decimal.php +++ b/library/Rules/Decimal.php @@ -39,7 +39,7 @@ public function evaluate(mixed $input): Result return Result::failed($input, $this, $parameters); } - return new Result($this->isValidDecimal($input), $input, $this, $parameters); + return Result::of($this->isValidDecimal($input), $input, $this, $parameters); } private function isValidDecimal(mixed $input): bool diff --git a/library/Rules/Domain.php b/library/Rules/Domain.php index 18c3be9cf..6265e1ee2 100644 --- a/library/Rules/Domain.php +++ b/library/Rules/Domain.php @@ -54,7 +54,7 @@ public function evaluate(mixed $input): Result } } - return new Result($this->partsRule->evaluate($parts)->hasPassed, $input, $this); + return Result::of($this->partsRule->evaluate($parts)->hasPassed, $input, $this); } private function createGenericRule(): Circuit diff --git a/library/Rules/Each.php b/library/Rules/Each.php index dd4d9806c..e3aa40335 100644 --- a/library/Rules/Each.php +++ b/library/Rules/Each.php @@ -38,8 +38,6 @@ protected function evaluateNonEmptyArray(array $input): Result true, ); - return (new Result($hasPassed, $input, $this)) - ->withChildren(...$children) - ->withNameFrom($this->rule); + return Result::of($hasPassed, $input, $this)->withChildren(...$children)->withNameFrom($this->rule); } } diff --git a/library/Rules/EndsWith.php b/library/Rules/EndsWith.php index c8464784e..071bee122 100644 --- a/library/Rules/EndsWith.php +++ b/library/Rules/EndsWith.php @@ -37,10 +37,10 @@ public function evaluate(mixed $input): Result { $parameters = ['endValue' => $this->endValue]; if ($this->identical) { - return new Result($this->validateIdentical($input), $input, $this, $parameters); + return Result::of($this->validateIdentical($input), $input, $this, $parameters); } - return new Result($this->validateEquals($input), $input, $this, $parameters); + return Result::of($this->validateEquals($input), $input, $this, $parameters); } private function validateEquals(mixed $input): bool diff --git a/library/Rules/Equals.php b/library/Rules/Equals.php index 22648bff9..5630e3a8e 100644 --- a/library/Rules/Equals.php +++ b/library/Rules/Equals.php @@ -32,7 +32,7 @@ public function evaluate(mixed $input): Result { $parameters = ['compareTo' => $this->compareTo]; if (is_scalar($input) === is_scalar($this->compareTo)) { - return new Result($input == $this->compareTo, $input, $this, $parameters); + return Result::of($input == $this->compareTo, $input, $this, $parameters); } return Result::failed($input, $this, $parameters); diff --git a/library/Rules/Extension.php b/library/Rules/Extension.php index 53f4d1214..bb42be57d 100644 --- a/library/Rules/Extension.php +++ b/library/Rules/Extension.php @@ -36,13 +36,13 @@ public function evaluate(mixed $input): Result { $parameters = ['extension' => $this->extension]; if ($input instanceof SplFileInfo) { - return new Result($this->extension === $input->getExtension(), $input, $this, $parameters); + return Result::of($this->extension === $input->getExtension(), $input, $this, $parameters); } if (!is_string($input)) { return Result::failed($input, $this, $parameters); } - return new Result($this->extension === pathinfo($input, PATHINFO_EXTENSION), $input, $this, $parameters); + return Result::of($this->extension === pathinfo($input, PATHINFO_EXTENSION), $input, $this, $parameters); } } diff --git a/library/Rules/Factor.php b/library/Rules/Factor.php index 5656949e4..10ce504b6 100644 --- a/library/Rules/Factor.php +++ b/library/Rules/Factor.php @@ -50,6 +50,6 @@ public function evaluate(mixed $input): Result // The dividend divided by the input must be an integer if input is a // factor of the dividend. - return new Result(is_int($dividend / $input), $input, $this, $parameters); + return Result::of(is_int($dividend / $input), $input, $this, $parameters); } } diff --git a/library/Rules/Identical.php b/library/Rules/Identical.php index efa4faaa7..589283847 100644 --- a/library/Rules/Identical.php +++ b/library/Rules/Identical.php @@ -28,6 +28,6 @@ public function __construct( public function evaluate(mixed $input): Result { - return new Result($input === $this->compareTo, $input, $this, ['compareTo' => $this->compareTo]); + return Result::of($input === $this->compareTo, $input, $this, ['compareTo' => $this->compareTo]); } } diff --git a/library/Rules/In.php b/library/Rules/In.php index 98acfb12c..8df771594 100644 --- a/library/Rules/In.php +++ b/library/Rules/In.php @@ -36,10 +36,10 @@ public function evaluate(mixed $input): Result { $parameters = ['haystack' => $this->haystack]; if ($this->compareIdentical) { - return new Result($this->validateIdentical($input), $input, $this, $parameters); + return Result::of($this->validateIdentical($input), $input, $this, $parameters); } - return new Result($this->validateEquals($input), $input, $this, $parameters); + return Result::of($this->validateEquals($input), $input, $this, $parameters); } private function validateEquals(mixed $input): bool diff --git a/library/Rules/Instance.php b/library/Rules/Instance.php index f0fee02d9..c1315c05c 100644 --- a/library/Rules/Instance.php +++ b/library/Rules/Instance.php @@ -29,6 +29,6 @@ public function __construct( public function evaluate(mixed $input): Result { - return new Result($input instanceof $this->class, $input, $this, ['class' => $this->class]); + return Result::of($input instanceof $this->class, $input, $this, ['class' => $this->class]); } } diff --git a/library/Rules/Ip.php b/library/Rules/Ip.php index 288da6e15..ac589981b 100644 --- a/library/Rules/Ip.php +++ b/library/Rules/Ip.php @@ -74,11 +74,11 @@ public function evaluate(mixed $input): Result } if ($this->mask) { - return new Result($this->belongsToSubnet($input), $input, $this, $parameters, $template); + return Result::of($this->belongsToSubnet($input), $input, $this, $parameters, $template); } if ($this->startAddress && $this->endAddress) { - return new Result($this->verifyNetwork($input), $input, $this, $parameters, $template); + return Result::of($this->verifyNetwork($input), $input, $this, $parameters, $template); } return Result::passed($input, $this, $parameters, $template); diff --git a/library/Rules/KeyExists.php b/library/Rules/KeyExists.php index c3376aaaf..9325661ce 100644 --- a/library/Rules/KeyExists.php +++ b/library/Rules/KeyExists.php @@ -39,7 +39,7 @@ public function getKey(): int|string public function evaluate(mixed $input): Result { - return new Result($this->hasKey($input), $input, $this, path: new Path($this->key)); + return Result::of($this->hasKey($input), $input, $this)->withPath(new Path($this->key)); } private function hasKey(mixed $input): bool diff --git a/library/Rules/KeySet.php b/library/Rules/KeySet.php index 6e71cc3f2..3847d5588 100644 --- a/library/Rules/KeySet.php +++ b/library/Rules/KeySet.php @@ -85,7 +85,9 @@ public function evaluate(mixed $input): Result ))); $keysResult = $keys->evaluate($input); - return (new Result($keysResult->hasPassed, $input, $this, [], $this->getTemplateFromKeys(array_keys($input)))) + $template = $this->getTemplateFromKeys(array_keys($input)); + + return Result::of($keysResult->hasPassed, $input, $this, [], $template) ->withChildren(...($keysResult->children === [] ? [$keysResult] : $keysResult->children)); } diff --git a/library/Rules/LanguageCode.php b/library/Rules/LanguageCode.php index 90de2502b..800bcfc00 100644 --- a/library/Rules/LanguageCode.php +++ b/library/Rules/LanguageCode.php @@ -67,6 +67,6 @@ public function evaluate(mixed $input): Result 'alpha-3' => $this->languages->getByAlpha3($input), }; - return new Result($currency !== null, $input, $this); + return Result::of($currency !== null, $input, $this); } } diff --git a/library/Rules/Length.php b/library/Rules/Length.php index 5b1b85b17..790edbcb1 100644 --- a/library/Rules/Length.php +++ b/library/Rules/Length.php @@ -43,7 +43,12 @@ public function evaluate(mixed $input): Result ->withId($this->rule->evaluate($input)->id->withPrefix('length')); } - return Result::fromAdjacent($input, 'length', $this, $this->rule->evaluate($length)); + $result = $this->rule->evaluate($length); + + return $result->asAdjacentOf( + Result::of($result->hasPassed, $input, $this), + 'length', + ); } private function extractLength(mixed $input): int|null diff --git a/library/Rules/Max.php b/library/Rules/Max.php index cc09829c6..a8003438b 100644 --- a/library/Rules/Max.php +++ b/library/Rules/Max.php @@ -23,6 +23,11 @@ final class Max extends FilteredNonEmptyArray /** @param non-empty-array $input */ protected function evaluateNonEmptyArray(array $input): Result { - return Result::fromAdjacent($input, 'max', $this, $this->rule->evaluate(max($input))); + $result = $this->rule->evaluate(max($input)); + + return $result->asAdjacentOf( + Result::of($result->hasPassed, $input, $this), + 'max', + ); } } diff --git a/library/Rules/Mimetype.php b/library/Rules/Mimetype.php index b7855db8f..3cd9f7e8d 100644 --- a/library/Rules/Mimetype.php +++ b/library/Rules/Mimetype.php @@ -49,7 +49,7 @@ public function evaluate(mixed $input): Result return Result::failed($input, $this, $parameters); } - return new Result( + return Result::of( $this->mimetype === $this->fileInfo->file($input, FILEINFO_MIME_TYPE), $input, $this, diff --git a/library/Rules/Min.php b/library/Rules/Min.php index 7f992d4a3..545d9c704 100644 --- a/library/Rules/Min.php +++ b/library/Rules/Min.php @@ -23,6 +23,11 @@ final class Min extends FilteredNonEmptyArray /** @param non-empty-array $input */ protected function evaluateNonEmptyArray(array $input): Result { - return Result::fromAdjacent($input, 'min', $this, $this->rule->evaluate(min($input))); + $result = $this->rule->evaluate(min($input)); + + return $result->asAdjacentOf( + Result::of($result->hasPassed, $input, $this), + 'min', + ); } } diff --git a/library/Rules/Multiple.php b/library/Rules/Multiple.php index e8285cbef..7a2a8ea2e 100644 --- a/library/Rules/Multiple.php +++ b/library/Rules/Multiple.php @@ -30,9 +30,9 @@ public function evaluate(mixed $input): Result { $parameters = ['multipleOf' => $this->multipleOf]; if ($this->multipleOf == 0) { - return new Result($input == 0, $input, $this, $parameters); + return Result::of($input == 0, $input, $this, $parameters); } - return new Result($input % $this->multipleOf == 0, $input, $this, $parameters); + return Result::of($input % $this->multipleOf == 0, $input, $this, $parameters); } } diff --git a/library/Rules/NoneOf.php b/library/Rules/NoneOf.php index 80954da25..352f0d307 100644 --- a/library/Rules/NoneOf.php +++ b/library/Rules/NoneOf.php @@ -49,6 +49,6 @@ public function evaluate(mixed $input): Result $template = self::TEMPLATE_ALL; } - return (new Result($valid, $input, $this, [], $template))->withChildren(...$children); + return Result::of($valid, $input, $this, [], $template)->withChildren(...$children); } } diff --git a/library/Rules/NotBlank.php b/library/Rules/NotBlank.php index 0821300e2..f251dd3c3 100644 --- a/library/Rules/NotBlank.php +++ b/library/Rules/NotBlank.php @@ -30,7 +30,7 @@ final class NotBlank implements Rule { public function evaluate(mixed $input): Result { - return new Result($this->isBlank($input), $input, $this); + return Result::of($this->isBlank($input), $input, $this); } private function isBlank(mixed $input): bool diff --git a/library/Rules/NotEmpty.php b/library/Rules/NotEmpty.php index 150ca9927..c6a582cf9 100644 --- a/library/Rules/NotEmpty.php +++ b/library/Rules/NotEmpty.php @@ -30,6 +30,6 @@ public function evaluate(mixed $input): Result $input = trim($input); } - return new Result(!empty($input), $input, $this); + return Result::of(!empty($input), $input, $this); } } diff --git a/library/Rules/NotUndef.php b/library/Rules/NotUndef.php index 8d02e23ac..0cee0e1c7 100644 --- a/library/Rules/NotUndef.php +++ b/library/Rules/NotUndef.php @@ -26,6 +26,6 @@ final class NotUndef implements Rule public function evaluate(mixed $input): Result { - return new Result($this->isUndefined($input) === false, $input, $this); + return Result::of($this->isUndefined($input) === false, $input, $this); } } diff --git a/library/Rules/NullOr.php b/library/Rules/NullOr.php index 36c2d9983..2975e300a 100644 --- a/library/Rules/NullOr.php +++ b/library/Rules/NullOr.php @@ -42,7 +42,7 @@ private function enrichResult(Result $result): Result if ($result->allowsAdjacent()) { return $result ->withId($result->id->withPrefix('nullOr')) - ->withAdjacent(new Result($result->hasPassed, $result->input, $this)); + ->withAdjacent(Result::of($result->hasPassed, $result->input, $this)); } return $result->withChildren(...array_map(fn(Result $child) => $this->enrichResult($child), $result->children)); diff --git a/library/Rules/OneOf.php b/library/Rules/OneOf.php index d8cb4978b..c7eab4a79 100644 --- a/library/Rules/OneOf.php +++ b/library/Rules/OneOf.php @@ -58,6 +58,6 @@ public function evaluate(mixed $input): Result ); } - return (new Result($valid, $input, $this, [], $template))->withChildren(...$children); + return Result::of($valid, $input, $this, [], $template)->withChildren(...$children); } } diff --git a/library/Rules/Phone.php b/library/Rules/Phone.php index 58df1c3d6..3a1bb99ce 100644 --- a/library/Rules/Phone.php +++ b/library/Rules/Phone.php @@ -78,7 +78,7 @@ public function evaluate(mixed $input): Result return Result::failed($input, $this, $parameters, $template); } - return new Result($this->isValidPhone((string) $input), $input, $this, $parameters, $template); + return Result::of($this->isValidPhone((string) $input), $input, $this, $parameters, $template); } private function isValidPhone(string $input): bool diff --git a/library/Rules/PropertyExists.php b/library/Rules/PropertyExists.php index c4fe3e57c..6666a61f9 100644 --- a/library/Rules/PropertyExists.php +++ b/library/Rules/PropertyExists.php @@ -33,12 +33,8 @@ public function __construct( public function evaluate(mixed $input): Result { - return new Result( - is_object($input) && $this->hasProperty($input), - $input, - $this, - path: new Path($this->propertyName), - ); + return Result::of(is_object($input) && $this->hasProperty($input), $input, $this) + ->withPath(new Path($this->propertyName)); } private function hasProperty(object $object): bool diff --git a/library/Rules/Regex.php b/library/Rules/Regex.php index 55ffe5ce3..15999ad18 100644 --- a/library/Rules/Regex.php +++ b/library/Rules/Regex.php @@ -36,6 +36,6 @@ public function evaluate(mixed $input): Result return Result::failed($input, $this, $parameters); } - return new Result(preg_match($this->regex, (string) $input) === 1, $input, $this, $parameters); + return Result::of(preg_match($this->regex, (string) $input) === 1, $input, $this, $parameters); } } diff --git a/library/Rules/Size.php b/library/Rules/Size.php index f674286e2..f0b67d31f 100644 --- a/library/Rules/Size.php +++ b/library/Rules/Size.php @@ -72,7 +72,10 @@ public function evaluate(mixed $input): Result $result = $this->rule->evaluate($this->getSize($input) / self::DATA_STORAGE_UNITS[$this->unit]['bytes']); $parameters = ['unit' => self::DATA_STORAGE_UNITS[$this->unit]['name']]; - return Result::fromAdjacent($input, 'size', $this, $result, $parameters); + return $result->asAdjacentOf( + Result::of($result->hasPassed, $input, $this, $parameters), + 'size', + ); } private function getSize(mixed $input): int|null diff --git a/library/Rules/StartsWith.php b/library/Rules/StartsWith.php index e97618280..9d7c0e4c9 100644 --- a/library/Rules/StartsWith.php +++ b/library/Rules/StartsWith.php @@ -37,10 +37,10 @@ public function evaluate(mixed $input): Result { $parameters = ['startValue' => $this->startValue]; if ($this->identical) { - return new Result($this->validateIdentical($input), $input, $this, $parameters); + return Result::of($this->validateIdentical($input), $input, $this, $parameters); } - return new Result($this->validateEquals($input), $input, $this, $parameters); + return Result::of($this->validateEquals($input), $input, $this, $parameters); } protected function validateEquals(mixed $input): bool diff --git a/library/Rules/SubdivisionCode.php b/library/Rules/SubdivisionCode.php index 2d2ac4a62..c4813a3e1 100644 --- a/library/Rules/SubdivisionCode.php +++ b/library/Rules/SubdivisionCode.php @@ -65,6 +65,6 @@ public function evaluate(mixed $input): Result return Result::passed($input, $this, $parameters); } - return new Result($subdivision !== null, $input, $this, $parameters); + return Result::of($subdivision !== null, $input, $this, $parameters); } } diff --git a/library/Rules/Subset.php b/library/Rules/Subset.php index f311e3cd2..eecd9ea98 100644 --- a/library/Rules/Subset.php +++ b/library/Rules/Subset.php @@ -37,6 +37,6 @@ public function evaluate(mixed $input): Result return Result::failed($input, $this, $parameters); } - return new Result(array_diff($input, $this->superset) === [], $input, $this, $parameters); + return Result::of(array_diff($input, $this->superset) === [], $input, $this, $parameters); } } diff --git a/library/Rules/Time.php b/library/Rules/Time.php index 580afb6d1..be1ec02ed 100644 --- a/library/Rules/Time.php +++ b/library/Rules/Time.php @@ -45,6 +45,6 @@ public function evaluate(mixed $input): Result return Result::failed($input, $this, $parameters); } - return new Result($this->isDateTime($this->format, (string) $input), $input, $this, $parameters); + return Result::of($this->isDateTime($this->format, (string) $input), $input, $this, $parameters); } } diff --git a/library/Rules/UndefOr.php b/library/Rules/UndefOr.php index 225474925..2a0901b51 100644 --- a/library/Rules/UndefOr.php +++ b/library/Rules/UndefOr.php @@ -45,7 +45,7 @@ private function enrichResult(Result $result): Result if ($result->allowsAdjacent()) { return $result ->withId($result->id->withPrefix('undefOr')) - ->withAdjacent(new Result($result->hasPassed, $result->input, $this)); + ->withAdjacent(Result::of($result->hasPassed, $result->input, $this)); } return $result->withChildren(...array_map(fn(Result $child) => $this->enrichResult($child), $result->children)); diff --git a/library/Rules/Uuid.php b/library/Rules/Uuid.php index 180299ad1..0b60f23b4 100644 --- a/library/Rules/Uuid.php +++ b/library/Rules/Uuid.php @@ -73,7 +73,7 @@ public function evaluate(mixed $input): Result $hasPassed = $this->version ? $uuidVersion === $this->version : $uuidVersion !== null; - return new Result($hasPassed, $input, $this, $parameters, $template); + return Result::of($hasPassed, $input, $this, $parameters, $template); } private function isSupportedVersion(int $version): bool diff --git a/library/Rules/VideoUrl.php b/library/Rules/VideoUrl.php index 149af97dd..7041ac880 100644 --- a/library/Rules/VideoUrl.php +++ b/library/Rules/VideoUrl.php @@ -60,7 +60,7 @@ public function evaluate(mixed $input): Result } if ($this->service !== null) { - return new Result($this->isValid($this->service, $input), $input, $this, $parameters, $template); + return Result::of($this->isValid($this->service, $input), $input, $this, $parameters, $template); } foreach (array_keys(self::SERVICES) as $service) { diff --git a/tests/library/Builders/ResultBuilder.php b/tests/library/Builders/ResultBuilder.php index 2dcd0dd9e..6d22f0e08 100644 --- a/tests/library/Builders/ResultBuilder.php +++ b/tests/library/Builders/ResultBuilder.php @@ -31,7 +31,7 @@ final class ResultBuilder private Name|null $name = null; - private Id|null $id = null; + private Id $id; private Rule $rule; @@ -45,6 +45,7 @@ final class ResultBuilder public function __construct() { $this->rule = Stub::daze(); + $this->id = Id::fromRule($this->rule); } public function build(): Result @@ -53,11 +54,11 @@ public function build(): Result $this->hasPassed, $this->input, $this->rule, + $this->id, $this->parameters, $this->template, $this->hasInvertedMode, $this->name, - $this->id, $this->adjacent, $this->path, ...$this->children,