-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path04.custom-path-printer.php
More file actions
47 lines (39 loc) · 1.3 KB
/
04.custom-path-printer.php
File metadata and controls
47 lines (39 loc) · 1.3 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
<?php
declare(strict_types=1);
use TypeLang\Mapper\Context\Path\PathInterface;
use TypeLang\Mapper\Context\Path\Printer\PathPrinterInterface;
use TypeLang\Mapper\Exception\Runtime\RuntimeException;
use TypeLang\Mapper\Mapper;
use TypeLang\Mapper\Mapping\MapType;
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) {
var_dump($e->getMessage());
// Print full path using ">" delimiter
$e->template->paths = new class implements PathPrinterInterface {
public function print(PathInterface $path): string
{
return \implode(' > ', $path->toArray());
}
};
// Before: Passed value in "values" of {"values": 42} must be of type
// list<ExampleDTO>, but 42 given at $.values[1].values
// After: Passed value in "values" of {"values": 42} must be of type
// list<ExampleDTO>, but 42 given at ExampleDTO > values >
// 1 > ExampleDTO > values
var_dump($e->getMessage());
}