Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 25 additions & 27 deletions library/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,72 +21,70 @@
/** @var array<Result> */
public array $children;

public Id $id;

/** @param array<string, mixed> $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<string, mixed> $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<string, mixed> $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<string, mixed> $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
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/AllOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion library/Rules/AnyOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion library/Rules/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Charset.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions library/Rules/Contains.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Core/Comparison.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Core/Envelope.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion library/Rules/Core/FilteredString.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Core/Simple.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion library/Rules/CreditCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/CurrencyCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion library/Rules/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
6 changes: 3 additions & 3 deletions library/Rules/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@ 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)) {
return Result::failed($input, $this, $parameters, $template);
}

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);
}
}
16 changes: 10 additions & 6 deletions library/Rules/DateTimeDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}

Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Decimal.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions library/Rules/Each.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
4 changes: 2 additions & 2 deletions library/Rules/EndsWith.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Equals.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions library/Rules/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion library/Rules/Factor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion library/Rules/Identical.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
4 changes: 2 additions & 2 deletions library/Rules/In.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
4 changes: 2 additions & 2 deletions library/Rules/Ip.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/KeyExists.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion library/Rules/KeySet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
2 changes: 1 addition & 1 deletion library/Rules/LanguageCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Loading