Skip to content

Commit c9add44

Browse files
committed
split of implementation from constructor
1 parent 7ba5b93 commit c9add44

File tree

4 files changed

+61
-35
lines changed

4 files changed

+61
-35
lines changed

src/Constraint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function __invoke(mixed $input): Validation
4848
*/
4949
public static function of(callable $assert): self
5050
{
51-
return new self(Of::callable($assert));
51+
return new self(Constraint\Of::callable($assert));
5252
}
5353

5454
/**

src/Constraint/Of.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 A
12+
* @template-covariant B
13+
* @implements Implementation<A, B>
14+
* @psalm-immutable
15+
*/
16+
final class Of implements Implementation
17+
{
18+
/** @var pure-callable(A): Validation<Failure, B> */
19+
private $assert;
20+
21+
/**
22+
* @param pure-callable(A): Validation<Failure, B> $assert
23+
*/
24+
private function __construct(callable $assert)
25+
{
26+
$this->assert = $assert;
27+
}
28+
29+
#[\Override]
30+
public function __invoke(mixed $value): Validation
31+
{
32+
return ($this->assert)($value);
33+
}
34+
35+
/**
36+
* @internal
37+
* @template T
38+
* @template U
39+
* @psalm-pure
40+
*
41+
* @param pure-callable(T): Validation<Failure, U> $assert
42+
*
43+
* @return self<T, U>
44+
*/
45+
public static function callable(callable $assert): self
46+
{
47+
return new self($assert);
48+
}
49+
}

src/Is.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,12 @@ public static function associativeArray(
187187
*
188188
* @param ?non-empty-string $message
189189
*
190-
* @return Implementation<Maybe<V>, V>
190+
* @return Constraint<Maybe<V>, V>
191191
*/
192-
public static function just(?string $message = null): Implementation
192+
public static function just(?string $message = null): Constraint
193193
{
194194
/** @psalm-suppress MixedArgumentTypeCoercion */
195-
return Of::callable(static fn(Maybe $value) => $value->match(
195+
return Constraint::of(static fn(Maybe $value) => $value->match(
196196
Validation::success(...),
197197
static fn() => Validation::fail(Failure::of(
198198
$message ?? 'No value was provided',
@@ -207,11 +207,11 @@ public static function just(?string $message = null): Implementation
207207
* @param V $value
208208
* @param ?non-empty-string $message
209209
*
210-
* @return Implementation<mixed, V>
210+
* @return Constraint<mixed, V>
211211
*/
212-
public static function value(mixed $value, ?string $message = null): Implementation
212+
public static function value(mixed $value, ?string $message = null): Constraint
213213
{
214-
return Of::callable(static fn(mixed $in) => match ($in) {
214+
return Constraint::of(static fn(mixed $in) => match ($in) {
215215
$value => Validation::success($value),
216216
default => Validation::fail(Failure::of(
217217
$message ?? \sprintf(

src/Of.php

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,12 @@
66
use Innmind\Immutable\Validation;
77

88
/**
9-
* @template-covariant A
10-
* @template-covariant B
11-
* @implements Constraint\Implementation<A, B>
12-
* @implements Constraint\Provider<A, B>
139
* @psalm-immutable
1410
*/
15-
final class Of implements Constraint\Implementation, Constraint\Provider
11+
final class Of
1612
{
17-
/** @var pure-callable(A): Validation<Failure, B> */
18-
private $assert;
19-
20-
/**
21-
* @param pure-callable(A): Validation<Failure, B> $assert
22-
*/
23-
private function __construct(callable $assert)
24-
{
25-
$this->assert = $assert;
26-
}
27-
28-
#[\Override]
29-
public function __invoke(mixed $value): Validation
30-
{
31-
return ($this->assert)($value);
32-
}
33-
34-
#[\Override]
35-
public function toConstraint(): Constraint
13+
private function __construct()
3614
{
37-
return Constraint::build($this);
3815
}
3916

4017
/**
@@ -44,10 +21,10 @@ public function toConstraint(): Constraint
4421
*
4522
* @param pure-callable(T): Validation<Failure, U> $assert
4623
*
47-
* @return self<T, U>
24+
* @return Constraint<T, U>
4825
*/
49-
public static function callable(callable $assert): self
26+
public static function callable(callable $assert): Constraint
5027
{
51-
return new self($assert);
28+
return Constraint::of($assert);
5229
}
5330
}

0 commit comments

Comments
 (0)