-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path03.custom-value-printer.php
64 lines (53 loc) · 1.85 KB
/
03.custom-value-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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
declare(strict_types=1);
use TypeLang\Mapper\Exception\Mapping\RuntimeException;
use TypeLang\Mapper\Mapper;
use TypeLang\Mapper\Mapping\MapType;
use TypeLang\Mapper\Runtime\Value\PHPValuePrinter;
use TypeLang\Mapper\Runtime\Value\SymfonyValuePrinter;
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());
// - Value#1: "of {"values": 42}"
// - Value#2: "but 42 given"
// - Message: Passed value in "values" of {"values": 42} must be of type
// list<ExampleDTO>, but 42 given at $.values[1].values
// Print all values using PHP-compatible types
$e->template->values = new PHPValuePrinter();
// After#1
var_dump($e->getMessage());
// - Value#1: "of array"
// - Value#2: "but int given"
// - Message: Passed value in "values" of array must be of type
// list<ExampleDTO>, but int given at $.values[1].values
// In case of symfony/var-dumper is installed, we can use it
if (\Composer\InstalledVersions::isInstalled('symfony/var-dumper')) {
// Print all values using SymfonyValuePrinter
$e->template->values = new SymfonyValuePrinter();
// After#2
var_dump($e->getMessage());
// - Value#1: "of array:1 [
// "values" => 42
// ]"
// - Value#2: "but 42 given"
// - Message: Passed value in "values" of array:1 [
// "values" => 42
// ] must be of type list<ExampleDTO>, but 42 given at $.values[1].values
}
}