-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path02.extended-type-printer.php
50 lines (42 loc) · 1.33 KB
/
02.extended-type-printer.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
<?php
declare(strict_types=1);
use TypeLang\Mapper\Exception\Mapping\RuntimeException;
use TypeLang\Mapper\Mapper;
use TypeLang\Mapper\Mapping\MapType;
use TypeLang\Parser\Node\Stmt\NamedTypeNode;
use TypeLang\Printer\PrettyPrinter;
require __DIR__ . '/../../vendor/autoload.php';
class ExampleDTO
{
public function __construct(
#[MapType(type: 'list<ExampleDTO>')]
public readonly array $values = [],
) {}
}
$mapper = new Mapper();
try {
$result = $mapper->denormalize([
'values' => [
['values' => []],
['values' => 42],
],
], ExampleDTO::class);
} catch (RuntimeException $e) {
// Before
var_dump($e->getMessage());
// - Type: "list<ExampleDTO>"
// - Message: Passed value in "values" of {"values": 42} must be of type
// list<ExampleDTO>, but 42 given at $.values[1].values
// Print all NamedTypeNode AST statements as "!!!MODIFIED!!!" string
$e->template->types = new class extends PrettyPrinter {
protected function printNamedTypeNode(NamedTypeNode $node): string
{
return '!!!MODIFIED!!!';
}
};
// After
var_dump($e->getMessage());
// - Type: "!!!MODIFIED!!!"
// - Message: Passed value in "values" of {"values": 42} must be of type
// !!!MODIFIED!!!, but 42 given at $.values[1].values
}