Skip to content

Commit

Permalink
v6
Browse files Browse the repository at this point in the history
  • Loading branch information
henriquemoody committed Dec 26, 2024
1 parent e55ba85 commit 4db95a1
Show file tree
Hide file tree
Showing 19 changed files with 70 additions and 149 deletions.
23 changes: 0 additions & 23 deletions library/Helpers/CanExtractPropertyValue.php

This file was deleted.

32 changes: 4 additions & 28 deletions library/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

use Respect\Validation\Rules\Core\Nameable;
use Respect\Validation\Rules\Core\PreNamed;
use Respect\Validation\Rules\Named;

use function array_filter;
use function array_map;
use function count;
Expand Down Expand Up @@ -138,36 +138,12 @@ public function withChildren(Result ...$children): self
}

public function withName(string $name): self
{
return $this->clone(
name: $name,
adjacent: $this->adjacent?->withName($name),
children: array_map(
static fn (Result $child) => $child->withName($name),
$this->children
),
);
}

public function withNameIfMissing(string $name): self
{
return $this->clone(
name: $this->name ?? $name,
adjacent: $this->adjacent?->withNameIfMissing($name),
children: array_map(
static fn (Result $child) => $child->withNameIfMissing($name),
$this->children
),
);
}

public function withFuckingName(string $name): self
{
return $this->clone(
name: $this->rule instanceof PreNamed ? $name : ($this->name ?? $name),
adjacent: $this->adjacent?->withFuckingName($name),
adjacent: $this->adjacent?->withName($name),
children: array_map(
static fn (Result $child) => $child->withFuckingName($child->name ?? $name),
static fn (Result $child) => $child->withName($child->name ?? $name),
$this->children
),
);
Expand All @@ -176,7 +152,7 @@ public function withFuckingName(string $name): self
public function withNameFrom(Rule $rule): self
{
if ($rule instanceof Nameable && $rule->getName() !== null) {
return $this->withFuckingName($rule->getName());
return $this->withName($rule->getName());
}

return $this;
Expand Down
5 changes: 2 additions & 3 deletions library/Rules/Attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use ReflectionObject;
use Respect\Validation\Result;
use Respect\Validation\Rule;
use Respect\Validation\Rules\Core\Binder;
use Respect\Validation\Rules\Core\Reducer;
use Respect\Validation\Rules\Core\Standard;

Expand All @@ -23,7 +22,7 @@ final class Attributes extends Standard
{
public function evaluate(mixed $input): Result
{
$objectType = (new Binder($this, new ObjectType()))->evaluate($input);
$objectType = (new ObjectType())->evaluate($input);
if (!$objectType->isValid) {
return $objectType->withId('attributes');
}
Expand All @@ -49,6 +48,6 @@ public function evaluate(mixed $input): Result
return (new AlwaysValid())->evaluate($input)->withId('attributes');
}

return (new Binder($this, new Reducer(...$rules)))->evaluate($input)->withId('attributes');
return (new Reducer(...$rules))->evaluate($input)->withId('attributes');
}
}
3 changes: 1 addition & 2 deletions library/Rules/Circuit.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use Attribute;
use Respect\Validation\Result;
use Respect\Validation\Rules\Core\Binder;
use Respect\Validation\Rules\Core\Composite;

#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
Expand All @@ -20,7 +19,7 @@ final class Circuit extends Composite
public function evaluate(mixed $input): Result
{
foreach ($this->rules as $rule) {
$result = (new Binder($this, $rule))->evaluate($input);
$result = $rule->evaluate($input);
if (!$result->isValid) {
return $result;
}
Expand Down
27 changes: 0 additions & 27 deletions library/Rules/Core/Binder.php

This file was deleted.

1 change: 0 additions & 1 deletion library/Rules/Core/PreNamed.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@

interface PreNamed
{

}
1 change: 0 additions & 1 deletion library/Rules/Each.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Respect\Validation\Result;
use Respect\Validation\Rules\Core\FilteredNonEmptyArray;

use Respect\Validation\Rules\Core\Nameable;
use function array_reduce;

#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
Expand Down
19 changes: 11 additions & 8 deletions library/Rules/Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Attribute;
use Respect\Validation\Result;
use Respect\Validation\Rule;
use Respect\Validation\Rules\Core\Binder;
use Respect\Validation\Rules\Core\KeyRelated;
use Respect\Validation\Rules\Core\Nameable;
use Respect\Validation\Rules\Core\Wrapper;
Expand All @@ -34,19 +33,23 @@ public function getKey(): int|string

public function evaluate(mixed $input): Result
{
$name = (string) $this->key;
if ($this->rule instanceof Nameable) {
$name = $this->rule->getName() ?? $name;
}

$keyExistsResult = (new KeyExists($this->key))->evaluate($input);
if (!$keyExistsResult->isValid) {
return $keyExistsResult->withFuckingName($name);
return $keyExistsResult->withNameFrom($this->rule);
}

return $this->rule
->evaluate($input[$this->key])
->withFuckingName($name)
->withName($this->getName())
->withUnchangeableId((string) $this->key);
}

private function getName(): string
{
if ($this->rule instanceof Nameable) {
return $this->rule->getName() ?? ((string) $this->key);
}

return (string) $this->key;
}
}
9 changes: 4 additions & 5 deletions library/Rules/KeyOptional.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Attribute;
use Respect\Validation\Result;
use Respect\Validation\Rule;
use Respect\Validation\Rules\Core\Binder;
use Respect\Validation\Rules\Core\KeyRelated;
use Respect\Validation\Rules\Core\Wrapper;

Expand All @@ -23,7 +22,7 @@ public function __construct(
private readonly int|string $key,
Rule $rule,
) {
parent::__construct(new Named($rule, (string) $this->key));
parent::__construct($rule);
}

public function getKey(): int|string
Expand All @@ -33,11 +32,11 @@ public function getKey(): int|string

public function evaluate(mixed $input): Result
{
$keyExistsResult = (new Binder($this, new KeyExists($this->key)))->evaluate($input);
$keyExistsResult = (new KeyExists($this->key))->evaluate($input);
if (!$keyExistsResult->isValid) {
return $keyExistsResult->withInvertedMode();
return $keyExistsResult->withNameFrom($this->rule)->withInvertedMode();
}

return (new Binder($this, $this->rule))->evaluate($input[$this->key])->withUnchangeableId((string) $this->key);
return (new Key($this->key, $this->rule))->evaluate($input);
}
}
3 changes: 1 addition & 2 deletions library/Rules/KeySet.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Respect\Validation\Message\Template;
use Respect\Validation\Result;
use Respect\Validation\Rule;
use Respect\Validation\Rules\Core\Binder;
use Respect\Validation\Rules\Core\KeyRelated;
use Respect\Validation\Rules\Core\Reducer;
use Respect\Validation\Rules\Core\Standard;
Expand Down Expand Up @@ -76,7 +75,7 @@ public function __construct(Rule $rule, Rule ...$rules)

public function evaluate(mixed $input): Result
{
$arrayResult = (new Binder($this, new ArrayType()))->evaluate($input);
$arrayResult = (new ArrayType())->evaluate($input);
if (!$arrayResult->isValid) {
return $arrayResult;
}
Expand Down
3 changes: 1 addition & 2 deletions library/Rules/Lazy.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Result;
use Respect\Validation\Rule;
use Respect\Validation\Rules\Core\Binder;
use Respect\Validation\Rules\Core\Standard;

use function call_user_func;
Expand All @@ -37,6 +36,6 @@ public function evaluate(mixed $input): Result
throw new ComponentException('Lazy failed because it could not create the rule');
}

return (new Binder($this, $rule))->evaluate($input);
return $rule->evaluate($input);
}
}
12 changes: 1 addition & 11 deletions library/Rules/Named.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@
use Attribute;
use Respect\Validation\Result;
use Respect\Validation\Rule;
use Respect\Validation\Rules\Core\Binder;
use Respect\Validation\Rules\Core\Nameable;
use Respect\Validation\Rules\Core\Wrapper;

use function array_map;

#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
final class Named extends Wrapper implements Nameable
{
Expand All @@ -35,13 +32,6 @@ public function getName(): string

public function evaluate(mixed $input): Result
{
return $this->enrichResult((new Binder($this, $this->rule))->evaluate($input));
}

private function enrichResult(Result $result): Result
{
return $result
->withName($result->name ?? $this->name)
->withChildren(...array_map(fn (Result $child) => $this->enrichResult($child), $result->children));
return $this->rule->evaluate($input)->withName($this->name);
}
}
30 changes: 23 additions & 7 deletions library/Rules/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@
namespace Respect\Validation\Rules;

use Attribute;
use Respect\Validation\Helpers\CanExtractPropertyValue;
use ReflectionObject;
use Respect\Validation\Result;
use Respect\Validation\Rule;
use Respect\Validation\Rules\Core\Binder;
use Respect\Validation\Rules\Core\Nameable;
use Respect\Validation\Rules\Core\Wrapper;

#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
final class Property extends Wrapper
{
use CanExtractPropertyValue;

public function __construct(
private readonly string $propertyName,
Rule $rule,
Expand All @@ -30,13 +28,31 @@ public function __construct(

public function evaluate(mixed $input): Result
{
$propertyExistsResult = (new Binder($this, new PropertyExists($this->propertyName)))->evaluate($input);
$propertyExistsResult = (new PropertyExists($this->propertyName))->evaluate($input);
if (!$propertyExistsResult->isValid) {
return $propertyExistsResult;
return $propertyExistsResult->withNameFrom($this->rule);
}

return (new Binder($this, $this->rule))
return $this->rule
->evaluate($this->extractPropertyValue($input, $this->propertyName))
->withName($this->getName())
->withUnchangeableId($this->propertyName);
}

private function extractPropertyValue(object $input, string $property): mixed
{
$reflectionObject = new ReflectionObject($input);
$reflectionProperty = $reflectionObject->getProperty($property);

return $reflectionProperty->isInitialized($input) ? $reflectionProperty->getValue($input) : null;
}

private function getName(): string
{
if ($this->rule instanceof Nameable) {
return $this->rule->getName() ?? $this->propertyName;
}

return $this->propertyName;

Check warning on line 56 in library/Rules/Property.php

View check run for this annotation

Codecov / codecov/patch

library/Rules/Property.php#L56

Added line #L56 was not covered by tests
}
}
3 changes: 2 additions & 1 deletion library/Rules/PropertyExists.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use ReflectionObject;
use Respect\Validation\Message\Template;
use Respect\Validation\Result;
use Respect\Validation\Rules\Core\PreNamed;
use Respect\Validation\Rules\Core\Standard;

use function is_object;
Expand All @@ -22,7 +23,7 @@
'{{name}} must be present',
'{{name}} must not be present',
)]
final class PropertyExists extends Standard
final class PropertyExists extends Standard implements PreNamed
{
public function __construct(
public readonly string $propertyName
Expand Down
Loading

0 comments on commit 4db95a1

Please sign in to comment.