-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path03.custom-type-template-arguments.php
105 lines (86 loc) · 2.87 KB
/
03.custom-type-template-arguments.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
declare(strict_types=1);
use TypeLang\Mapper\Exception\Mapping\InvalidValueException;
use TypeLang\Mapper\Mapper;
use TypeLang\Mapper\Platform\DelegatePlatform;
use TypeLang\Mapper\Platform\Standard\Builder\Builder;
use TypeLang\Mapper\Platform\Standard\Type\TypeInterface;
use TypeLang\Mapper\Platform\StandardPlatform;
use TypeLang\Mapper\Runtime\Context;
use TypeLang\Mapper\Runtime\Parser\TypeParserInterface;
use TypeLang\Mapper\Runtime\Repository\TypeRepositoryInterface;
use TypeLang\Parser\Node\Stmt\NamedTypeNode;
use TypeLang\Parser\Node\Stmt\TypeStatement;
require __DIR__ . '/../../vendor/autoload.php';
// Create custom type builder
class MyNonEmptyTypeBuilder extends Builder
{
public function isSupported(TypeStatement $statement): bool
{
// Expects type with name "non-empty"
return $statement instanceof NamedTypeNode
&& $statement->name->toLowerString() === 'non-empty';
}
public function build(
TypeStatement $statement,
TypeRepositoryInterface $types,
TypeParserInterface $parser,
): TypeInterface {
// Shape fields not allowed (like: "non-empty{...}")
$this->expectNoShapeFields($statement);
// Expects only template argument (like: "non-empty<T>", but NOT "non-empty<T, U>")
$this->expectTemplateArgumentsCount($statement, 1);
$innerArgument = $statement->arguments->first();
// inner type of TypeInterface
$type = $types->getTypeByStatement($innerArgument->value);
return new MyNonEmptyType($type);
}
}
// Create custom type
class MyNonEmptyType implements TypeInterface
{
public function __construct(
private readonly TypeInterface $type,
) {}
public function match(mixed $value, Context $context): bool
{
return !empty($value);
}
public function cast(mixed $value, Context $context): mixed
{
if (!empty($value)) {
return $this->type->cast($value, $context);
}
throw InvalidValueException::createFromContext($value, $context);
}
}
$mapper = new Mapper(new DelegatePlatform(
delegate: new StandardPlatform(),
// Extend by custom "MyNonEmptyTypeBuilder" type builder
types: [new MyNonEmptyTypeBuilder()],
));
var_dump($mapper->normalize('example', 'non-empty<string>'));
//
// string(7) "example"
//
var_dump($mapper->normalize([1, 2, 3], 'non-empty<array>'));
//
// array:3 [
// 0 => 1
// 1 => 2
// 2 => 3
// ]
//
var_dump($mapper->normalize([], 'non-empty<string>'));
//
// InvalidValueException: Passed value [] is invalid
//
var_dump($mapper->normalize('example', 'non-empty'));
//
// MissingTemplateArgumentsException: Type "non-empty" expects at least 1
// template argument(s), but 0 were passed
//
var_dump($mapper->normalize('', 'non-empty<string>'));
//
// InvalidValueException: Passed value "" is invalid
//