-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path03.custom-value-printer.php
More file actions
57 lines (46 loc) · 1.87 KB
/
03.custom-value-printer.php
File metadata and controls
57 lines (46 loc) · 1.87 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
<?php
declare(strict_types=1);
use TypeLang\Mapper\Exception\Runtime\RuntimeException;
use TypeLang\Mapper\Exception\Value\PHPValuePrinter;
use TypeLang\Mapper\Exception\Value\SymfonyValuePrinter;
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 all values using PHP-compatible types
$e->template->values = new PHPValuePrinter();
// Before: Passed value in "values" of {"values": 42} must be of type
// list<ExampleDTO>, but 42 given at $.values[1].values
// After#1: Passed value in string of stdClass must be of type
// list<ExampleDTO>, but int given at $.values[1].values
var_dump($e->getMessage());
// 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();
// Before: Passed value in "values" of {"values": 42} must be of type
// list<ExampleDTO>, but 42 given at $.values[1].values
// After#1: Passed value in string of stdClass must be of type
// list<ExampleDTO>, but int given at $.values[1].values
// After#2: Passed value in "values" of {#394
// +"values": 42
// } must be of type list<ExampleDTO>, but 42 given at $.values[1].values
var_dump($e->getMessage());
}
}