Skip to content

Commit 15b70de

Browse files
committed
v6
1 parent e55ba85 commit 15b70de

File tree

8 files changed

+38
-32
lines changed

8 files changed

+38
-32
lines changed

library/Rules/Key.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@ public function getKey(): int|string
3434

3535
public function evaluate(mixed $input): Result
3636
{
37+
$keyExistsResult = (new KeyExists($this->key))->evaluate($input);
38+
if (!$keyExistsResult->isValid) {
39+
return $keyExistsResult->withNameFrom($this->rule);
40+
}
41+
3742
$name = (string) $this->key;
3843
if ($this->rule instanceof Nameable) {
3944
$name = $this->rule->getName() ?? $name;
4045
}
4146

42-
$keyExistsResult = (new KeyExists($this->key))->evaluate($input);
43-
if (!$keyExistsResult->isValid) {
44-
return $keyExistsResult->withFuckingName($name);
45-
}
46-
4747
return $this->rule
4848
->evaluate($input[$this->key])
4949
->withFuckingName($name)

library/Rules/KeyOptional.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Respect\Validation\Rule;
1515
use Respect\Validation\Rules\Core\Binder;
1616
use Respect\Validation\Rules\Core\KeyRelated;
17+
use Respect\Validation\Rules\Core\Nameable;
1718
use Respect\Validation\Rules\Core\Wrapper;
1819

1920
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
@@ -23,7 +24,7 @@ public function __construct(
2324
private readonly int|string $key,
2425
Rule $rule,
2526
) {
26-
parent::__construct(new Named($rule, (string) $this->key));
27+
parent::__construct($rule);
2728
}
2829

2930
public function getKey(): int|string
@@ -33,11 +34,11 @@ public function getKey(): int|string
3334

3435
public function evaluate(mixed $input): Result
3536
{
36-
$keyExistsResult = (new Binder($this, new KeyExists($this->key)))->evaluate($input);
37+
$keyExistsResult = (new KeyExists($this->key))->evaluate($input);
3738
if (!$keyExistsResult->isValid) {
38-
return $keyExistsResult->withInvertedMode();
39+
return $keyExistsResult->withNameFrom($this->rule)->withInvertedMode();
3940
}
4041

41-
return (new Binder($this, $this->rule))->evaluate($input[$this->key])->withUnchangeableId((string) $this->key);
42+
return (new Key($this->key, $this->rule))->evaluate($input);
4243
}
4344
}

library/Rules/Named.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public function evaluate(mixed $input): Result
4141
private function enrichResult(Result $result): Result
4242
{
4343
return $result
44-
->withName($result->name ?? $this->name)
45-
->withChildren(...array_map(fn (Result $child) => $this->enrichResult($child), $result->children));
44+
->withFuckingName($this->name);
4645
}
4746
}

library/Rules/Property.php

+10-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Respect\Validation\Result;
1515
use Respect\Validation\Rule;
1616
use Respect\Validation\Rules\Core\Binder;
17+
use Respect\Validation\Rules\Core\Nameable;
1718
use Respect\Validation\Rules\Core\Wrapper;
1819

1920
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
@@ -30,13 +31,19 @@ public function __construct(
3031

3132
public function evaluate(mixed $input): Result
3233
{
33-
$propertyExistsResult = (new Binder($this, new PropertyExists($this->propertyName)))->evaluate($input);
34+
$propertyExistsResult = (new PropertyExists($this->propertyName))->evaluate($input);
3435
if (!$propertyExistsResult->isValid) {
35-
return $propertyExistsResult;
36+
return $propertyExistsResult->withNameFrom($this->rule);
3637
}
3738

38-
return (new Binder($this, $this->rule))
39+
$name = $this->propertyName;
40+
if ($this->rule instanceof Nameable) {
41+
$name = $this->rule->getName() ?? $name;
42+
}
43+
44+
return $this->rule
3945
->evaluate($this->extractPropertyValue($input, $this->propertyName))
46+
->withFuckingName($name)
4047
->withUnchangeableId($this->propertyName);
4148
}
4249
}

library/Rules/PropertyExists.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use ReflectionObject;
1414
use Respect\Validation\Message\Template;
1515
use Respect\Validation\Result;
16+
use Respect\Validation\Rules\Core\PreNamed;
1617
use Respect\Validation\Rules\Core\Standard;
1718

1819
use function is_object;
@@ -22,7 +23,7 @@
2223
'{{name}} must be present',
2324
'{{name}} must not be present',
2425
)]
25-
final class PropertyExists extends Standard
26+
final class PropertyExists extends Standard implements PreNamed
2627
{
2728
public function __construct(
2829
public readonly string $propertyName

library/Rules/PropertyOptional.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,9 @@ public function evaluate(mixed $input): Result
3232
{
3333
$propertyExistsResult = (new Binder($this, new PropertyExists($this->propertyName)))->evaluate($input);
3434
if (!$propertyExistsResult->isValid) {
35-
return $propertyExistsResult->withInvertedMode();
35+
return $propertyExistsResult->withNameFrom($this->rule)->withInvertedMode();
3636
}
3737

38-
return (new Binder($this, $this->rule))
39-
->evaluate($this->extractPropertyValue($input, $this->propertyName))
40-
->withUnchangeableId($this->propertyName);
38+
return (new Property($this->propertyName, $this->rule))->evaluate($input);
4139
}
4240
}

tests/feature/Rules/KeyTest.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,21 @@
3636
));
3737

3838
test('With wrapped name, missing key', expectAll(
39-
fn() => v::key('foo', v::intType()->setName('Wrapped'))->setName('Wrapper')->assert([]),
39+
fn() => v::key('foo', v::intType()->setName('Wrapped'))->assert([]),
4040
'Wrapped must be present',
4141
'- Wrapped must be present',
4242
['foo' => 'Wrapped must be present'],
4343
));
4444

4545
test('With wrapped name, default', expectAll(
46-
fn() => v::key('foo', v::intType()->setName('Wrapped'))->setName('Wrapper')->assert(['foo' => 'string']),
46+
fn() => v::key('foo', v::intType()->setName('Wrapped'))->assert(['foo' => 'string']),
4747
'Wrapped must be an integer',
4848
'- Wrapped must be an integer',
4949
['foo' => 'Wrapped must be an integer'],
5050
));
5151

5252
test('With wrapped name, inverted', expectAll(
53-
fn() => v::not(v::key('foo', v::intType()->setName('Wrapped'))->setName('Wrapper'))->setName('Not')->assert(['foo' => 12]),
53+
fn() => v::not(v::key('foo', v::intType()->setName('Wrapped')))->assert(['foo' => 12]),
5454
'Wrapped must not be an integer',
5555
'- Wrapped must not be an integer',
5656
['foo' => 'Wrapped must not be an integer'],
@@ -65,9 +65,9 @@
6565

6666
test('With wrapper name, missing key', expectAll(
6767
fn() => v::key('foo', v::intType())->setName('Wrapper')->assert([]),
68-
'foo must be present',
69-
'- foo must be present',
70-
['foo' => 'foo must be present'],
68+
'Wrapper must be present',
69+
'- Wrapper must be present',
70+
['foo' => 'Wrapper must be present'],
7171
));
7272

7373
test('With wrapper name, inverted', expectAll(

tests/feature/Rules/PropertyTest.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,22 @@
3636
));
3737

3838
test('With wrapped name, missing property', expectAll(
39-
fn() => v::property('foo', v::intType()->setName('Wrapped'))->setName('Wrapper')->assert(new stdClass()),
40-
'Wrapped must be present',
41-
'- Wrapped must be present',
42-
['foo' => 'Wrapped must be present'],
39+
fn() => v::property('foo', v::intType()->setName('Wrapped'))->assert(new stdClass()),
40+
'foo must be present',
41+
'- foo must be present',
42+
['foo' => 'foo must be present'],
4343
));
4444

4545
test('With wrapped name, default', expectAll(
46-
fn() => v::property('foo', v::intType()->setName('Wrapped'))->setName('Wrapper')->assert((object) ['foo' => 'string']),
46+
fn() => v::property('foo', v::intType()->setName('Wrapped'))->assert((object) ['foo' => 'string']),
4747
'Wrapped must be an integer',
4848
'- Wrapped must be an integer',
4949
['foo' => 'Wrapped must be an integer'],
5050
));
5151

5252
test('With wrapped name, inverted', expectAll(
5353
fn() => v::not(
54-
v::property('foo', v::intType()->setName('Wrapped'))->setName('Wrapper'),
54+
v::property('foo', v::intType()->setName('Wrapped')),
5555
)
5656
->setName('Not')
5757
->assert((object) ['foo' => 12]),
@@ -68,7 +68,7 @@
6868
));
6969

7070
test('With wrapper name, missing property', expectAll(
71-
fn() => v::property('foo', v::intType())->setName('Wrapper')->assert(new stdClass()),
71+
fn() => v::property('foo', v::intType()->setName('Wrapper'))->assert(new stdClass()),
7272
'foo must be present',
7373
'- foo must be present',
7474
['foo' => 'foo must be present'],

0 commit comments

Comments
 (0)