Skip to content

Commit 7ba5b93

Browse files
committed
split instance implementation from constructor
1 parent 2680311 commit 7ba5b93

File tree

3 files changed

+69
-90
lines changed

3 files changed

+69
-90
lines changed

src/Constraint.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,19 @@ public static function of(callable $assert): self
5151
return new self(Of::callable($assert));
5252
}
5353

54+
/**
55+
* @template A of object
56+
* @psalm-pure
57+
*
58+
* @param class-string<A> $class
59+
*
60+
* @return self<mixed, A>
61+
*/
62+
public static function instance(string $class): self
63+
{
64+
return new self(Constraint\Instance::of($class));
65+
}
66+
5467
/**
5568
* @psalm-pure
5669
* @template A

src/Constraint/Instance.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace Innmind\Validation\Constraint;
5+
6+
use Innmind\Validation\Failure;
7+
use Innmind\Immutable\Validation;
8+
9+
/**
10+
* @internal
11+
* @template-covariant T of object
12+
* @implements Implementation<mixed, T>
13+
* @psalm-immutable
14+
*/
15+
final class Instance implements Implementation
16+
{
17+
/** @var class-string<T> */
18+
private string $class;
19+
20+
/**
21+
* @param class-string<T> $class
22+
*/
23+
private function __construct(string $class)
24+
{
25+
$this->class = $class;
26+
}
27+
28+
#[\Override]
29+
public function __invoke(mixed $value): Validation
30+
{
31+
/** @var Validation<Failure, T> */
32+
return match ($value instanceof $this->class) {
33+
true => Validation::success($value),
34+
false => Validation::fail(Failure::of("Value is not an instance of {$this->class}")),
35+
};
36+
}
37+
38+
/**
39+
* @internal
40+
* @template A of object
41+
* @psalm-pure
42+
*
43+
* @param class-string<A> $class
44+
*
45+
* @return self<A>
46+
*/
47+
public static function of(string $class): self
48+
{
49+
return new self($class);
50+
}
51+
}

src/Instance.php

Lines changed: 5 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,13 @@
33

44
namespace Innmind\Validation;
55

6-
use Innmind\Immutable\{
7-
Validation,
8-
Predicate,
9-
};
10-
116
/**
12-
* @template-covariant T of object
13-
* @implements Constraint\Provider<mixed, T>
147
* @psalm-immutable
158
*/
16-
final class Instance implements Constraint\Provider
9+
final class Instance
1710
{
18-
/** @var class-string<T> */
19-
private string $class;
20-
21-
/**
22-
* @param class-string<T> $class
23-
*/
24-
private function __construct(string $class)
11+
private function __construct()
2512
{
26-
$this->class = $class;
27-
}
28-
29-
/**
30-
* @return Validation<Failure, T>
31-
*/
32-
public function __invoke(mixed $value): Validation
33-
{
34-
/** @var Validation<Failure, T> */
35-
return match ($value instanceof $this->class) {
36-
true => Validation::success($value),
37-
false => Validation::fail(Failure::of("Value is not an instance of {$this->class}")),
38-
};
39-
}
40-
41-
#[\Override]
42-
public function toConstraint(): Constraint
43-
{
44-
/** @psalm-suppress InvalidArgument */
45-
return Constraint::of($this(...));
4613
}
4714

4815
/**
@@ -51,62 +18,10 @@ public function toConstraint(): Constraint
5118
*
5219
* @param class-string<A> $class
5320
*
54-
* @return self<A>
55-
*/
56-
public static function of(string $class): self
57-
{
58-
return new self($class);
59-
}
60-
61-
/**
62-
* @template V
63-
*
64-
* @param Constraint<T, V> $constraint
65-
*
66-
* @return Constraint<mixed, V>
67-
*/
68-
public function and(Constraint $constraint): Constraint
69-
{
70-
return $this
71-
->toConstraint()
72-
->and($constraint);
73-
}
74-
75-
/**
76-
* @template V
77-
*
78-
* @param Constraint<mixed, V> $constraint
79-
*
80-
* @return Constraint<mixed, T|V>
81-
*/
82-
public function or(Constraint $constraint): Constraint
83-
{
84-
return $this
85-
->toConstraint()
86-
->or($constraint);
87-
}
88-
89-
/**
90-
* @template V
91-
*
92-
* @param callable(T): V $map
93-
*
94-
* @return Constraint<mixed, V>
95-
*/
96-
public function map(callable $map): Constraint
97-
{
98-
return $this
99-
->toConstraint()
100-
->map($map);
101-
}
102-
103-
/**
104-
* @return Predicate<T>
21+
* @return Constraint<mixed, A>
10522
*/
106-
public function asPredicate(): Predicate
23+
public static function of(string $class): Constraint
10724
{
108-
return $this
109-
->toConstraint()
110-
->asPredicate();
25+
return Constraint::instance($class);
11126
}
11227
}

0 commit comments

Comments
 (0)