-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path03.custom-type-template-arguments.php
More file actions
111 lines (93 loc) · 2.9 KB
/
03.custom-type-template-arguments.php
File metadata and controls
111 lines (93 loc) · 2.9 KB
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
106
107
108
109
110
111
<?php
declare(strict_types=1);
use TypeLang\Mapper\Context\BuildingContext;
use TypeLang\Mapper\Context\RuntimeContext;
use TypeLang\Mapper\Exception\Runtime\InvalidValueException;
use TypeLang\Mapper\Mapper;
use TypeLang\Mapper\Platform\StandardPlatform;
use TypeLang\Mapper\Type\Builder\Builder;
use TypeLang\Mapper\Type\MatchedResult;
use TypeLang\Mapper\Type\TypeInterface;
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 $stmt): bool
{
// Expects type with name "non-empty"
return $stmt instanceof NamedTypeNode
&& $stmt->name->toLowerString() === 'non-empty';
}
public function build(TypeStatement $stmt, BuildingContext $context): TypeInterface
{
// Shape fields not allowed (like: "non-empty{...}")
$this->expectNoShapeFields($stmt);
// Expects only template argument (like: "non-empty<T>", but NOT "non-empty<T, U>")
$this->expectTemplateArgumentsCount($stmt, 1);
$innerArgument = $stmt->arguments->first();
// inner type of TypeInterface
$type = $context->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, RuntimeContext $context): ?MatchedResult
{
return MatchedResult::successIf($value, !empty($value));
}
public function cast(mixed $value, RuntimeContext $context): mixed
{
if (!empty($value)) {
return $this->type->cast($value, $context);
}
throw InvalidValueException::createFromContext($context);
}
}
$mapper = new Mapper(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
// ]
//
try {
var_dump($mapper->normalize([], 'non-empty<string>'));
} catch (\Throwable $e) {
echo $e->getMessage() . "\n";
}
//
// InvalidValueException: Passed value [] is invalid
//
try {
var_dump($mapper->normalize('example', 'non-empty'));
} catch (\Throwable $e) {
echo $e->getMessage() . "\n";
}
//
// MissingTemplateArgumentsException: Type "non-empty" expects at least 1
// template argument(s), but 0 were passed
//
try {
var_dump($mapper->normalize('', 'non-empty<string>'));
} catch (\Throwable $e) {
echo $e->getMessage() . "\n";
}
//
// InvalidValueException: Passed value "" is invalid
//