Skip to content

Commit 87f07f3

Browse files
author
Kirill Nesmeyanov
committed
Actualize examples
1 parent 84df1e3 commit 87f07f3

10 files changed

+99
-68
lines changed

example/01.normalization/05.typed-object-normalization.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,5 @@ public function __construct(
4141

4242
//
4343
// InvalidValueException: Passed value must be of type ChildDTO{name: string},
44-
// but int (42) given at $.children[2]
44+
// but 42 given at $.children[2]
4545
//

example/02.errors/01.custom-type-printer.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,19 @@ public function __construct(
2626
]
2727
], ExampleDTO::class);
2828
} catch (RuntimeException $e) {
29-
// Before: "list<ExampleDTO>"
29+
// Before
3030
var_dump($e->getMessage());
31-
// Passed value of field "values" must be of type list<ExampleDTO>,
32-
// but 42 given at $.values[1].values
31+
// - Type: "list<ExampleDTO>"
32+
// - Message: Passed value in "values" of {"values": 42} must be of type
33+
// list<ExampleDTO>, but 42 given at $.values[1].values
3334

3435
// Replace all "expected type" definition to PHP-supported printer
3536
// instead of PrettyPrinter
3637
$e->template->types = new \TypeLang\Printer\NativeTypePrinter();
3738

38-
// After: "array"
39+
// After
3940
var_dump($e->getMessage());
40-
// Passed value of field "values" must be of type array,
41-
// but 42 given at $.values[1].values
41+
// - Type: "array"
42+
// - Message: Passed value in "values" of {"values": 42} must be of type
43+
// array, but 42 given at $.values[1].values
4244
}

example/02.errors/02.extended-type-printer.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ public function __construct(
2828
],
2929
], ExampleDTO::class);
3030
} catch (RuntimeException $e) {
31-
// Before: "list<ExampleDTO>"
31+
// Before
3232
var_dump($e->getMessage());
33-
// Passed value of field "values" must be of type list<ExampleDTO>,
34-
// but 42 given at $.values[1].values
33+
// - Type: "list<ExampleDTO>"
34+
// - Message: Passed value in "values" of {"values": 42} must be of type
35+
// list<ExampleDTO>, but 42 given at $.values[1].values
3536

3637
// Print all NamedTypeNode AST statements as "!!!MODIFIED!!!" string
3738
$e->template->types = new class extends PrettyPrinter {
@@ -41,8 +42,9 @@ protected function printNamedTypeNode(NamedTypeNode $node): string
4142
}
4243
};
4344

44-
// After: "!!!MODIFIED!!!"
45+
// After
4546
var_dump($e->getMessage());
46-
// Passed value of field "values" must be of type !!!MODIFIED!!!,
47-
// but 42 given at $.values[1].values
47+
// - Type: "!!!MODIFIED!!!"
48+
// - Message: Passed value in "values" of {"values": 42} must be of type
49+
// !!!MODIFIED!!!, but 42 given at $.values[1].values
4850
}

example/02.errors/03.custom-path-printer.php

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use TypeLang\Mapper\Exception\Mapping\RuntimeException;
6+
use TypeLang\Mapper\Mapper;
7+
use TypeLang\Mapper\Mapping\MapType;
8+
use TypeLang\Mapper\Runtime\Value\PHPValuePrinter;
9+
use TypeLang\Mapper\Runtime\Value\SymfonyValuePrinter;
10+
11+
require __DIR__ . '/../../vendor/autoload.php';
12+
13+
class ExampleDTO
14+
{
15+
public function __construct(
16+
#[MapType(type: 'list<ExampleDTO>')]
17+
public readonly array $values = [],
18+
) {}
19+
}
20+
21+
$mapper = new Mapper();
22+
23+
try {
24+
$result = $mapper->denormalize([
25+
'values' => [
26+
['values' => []],
27+
['values' => 42],
28+
],
29+
], ExampleDTO::class);
30+
} catch (RuntimeException $e) {
31+
// Before
32+
var_dump($e->getMessage());
33+
// - Value#1: "of {"values": 42}"
34+
// - Value#2: "but 42 given"
35+
// - Message: Passed value in "values" of {"values": 42} must be of type
36+
// list<ExampleDTO>, but 42 given at $.values[1].values
37+
38+
// Print all values using PHP-compatible types
39+
$e->template->values = new PHPValuePrinter();
40+
41+
// After#1
42+
var_dump($e->getMessage());
43+
// - Value#1: "of array"
44+
// - Value#2: "but int given"
45+
// - Message: Passed value in "values" of array must be of type
46+
// list<ExampleDTO>, but int given at $.values[1].values
47+
48+
49+
// In case of symfony/var-dumper is installed, we can use it
50+
if (\Composer\InstalledVersions::isInstalled('symfony/var-dumper')) {
51+
// Print all values using SymfonyValuePrinter
52+
$e->template->values = new SymfonyValuePrinter();
53+
54+
// After#2
55+
var_dump($e->getMessage());
56+
// - Value#1: "of array:1 [
57+
// "values" => 42
58+
// ]"
59+
// - Value#2: "but 42 given"
60+
// - Message: Passed value in "values" of array:1 [
61+
// "values" => 42
62+
// ] must be of type list<ExampleDTO>, but 42 given at $.values[1].values
63+
}
64+
}
File renamed without changes.

example/03.types/01.type-platforms.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@
1818

1919
var_dump($result);
2020
//
21-
// TypeNotFoundException: Type "string" is not registered
21+
// TypeNotFoundException: Type "string" is not defined
2222
//

example/03.types/02.custom-type.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
use TypeLang\Mapper\Exception\Runtime\InvalidValueException;
5+
use TypeLang\Mapper\Exception\Mapping\InvalidValueException;
66
use TypeLang\Mapper\Mapper;
77
use TypeLang\Mapper\Platform\DelegatePlatform;
88
use TypeLang\Mapper\Platform\StandardPlatform;
@@ -26,9 +26,10 @@ public function cast(mixed $value, Context $context): string
2626
return $value;
2727
}
2828

29-
throw InvalidValueException::createFromContext(
29+
throw new InvalidValueException(
3030
value: $value,
31-
context: $context,
31+
path: $context->getPath(),
32+
template: 'Passed value cannot be empty, but {{value}} given',
3233
);
3334
}
3435
}
@@ -46,5 +47,11 @@ public function cast(mixed $value, Context $context): string
4647

4748
var_dump($result);
4849
//
49-
// InvalidValueException: Passed value "" is invalid at $[1]
50+
// expected exception:
51+
// TypeLang\Mapper\Exception\Mapping\InvalidIterableValueException:
52+
// Passed value "" on index 1 in ["example", ""] is invalid at $[1]
53+
//
54+
// previous exception:
55+
// TypeLang\Mapper\Exception\Mapping\InvalidValueException:
56+
// Passed value cannot be empty, but "" given at $[1]
5057
//

example/03.types/03.custom-type-template-arguments.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
use TypeLang\Mapper\Exception\Runtime\InvalidValueException;
5+
use TypeLang\Mapper\Exception\Mapping\InvalidValueException;
66
use TypeLang\Mapper\Mapper;
77
use TypeLang\Mapper\Platform\DelegatePlatform;
88
use TypeLang\Mapper\Platform\StandardPlatform;
@@ -89,19 +89,17 @@ public function cast(mixed $value, Context $context): mixed
8989

9090
var_dump($mapper->normalize([], 'non-empty<string>'));
9191
//
92-
// InvalidValueException: Passed value must be of type non-empty, but
93-
// array given at root.
92+
// InvalidValueException: Passed value [] is invalid
9493
//
9594

9695

9796
var_dump($mapper->normalize('example', 'non-empty'));
9897
//
99-
// MissingTemplateArgumentsException: Type "non-empty" expects 1 template
100-
// argument, but only 0 were passed
98+
// MissingTemplateArgumentsException: Type "non-empty" expects at least 1
99+
// template argument(s), but 0 were passed
101100
//
102101

103102
var_dump($mapper->normalize('', 'non-empty<string>'));
104103
//
105-
// InvalidValueException: Passed value must be of type non-empty, but
106-
// null given at root.
104+
// InvalidValueException: Passed value "" is invalid
107105
//

example/03.types/04.custom-platform.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function __construct(
4848
var_dump($mapper->normalize(new ExampleDTO()));
4949
//
5050
// TypeRequiredException: Type "int" for property ExampleDTO::$value
51-
// is not registered.
51+
// is not defined
5252
//
5353

5454
var_dump($mapper->normalize([new ExampleDTO()], 'array<ExampleDTO>'));

0 commit comments

Comments
 (0)