Skip to content

Commit 05dca00

Browse files
committed
Improve examples
1 parent c228724 commit 05dca00

32 files changed

+568
-286
lines changed

example/01.normalization/06.inherited-types-normalization.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function __construct(
3030

3131
$result = $mapper->normalize($value);
3232

33-
dd($result);
33+
var_dump($result);
3434
//
3535
// array:1 [
3636
// "items" => array:3 [

example/01.normalization/08.object-output-normalization.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,17 @@ public function __construct(
3737

3838
var_dump($result);
3939
//
40-
// object{
41-
// items: array:3 [
42-
// "key1" => object{
43-
// value: "first"
44-
// }
45-
// "key2" => object{
46-
// value: "second"
47-
// }
48-
// 0 => object{
49-
// value: "third"
50-
// }
40+
// array:1 [
41+
// "items" => array:3 [
42+
// "key1" => array:1 [
43+
// "value" => "first"
44+
// ]
45+
// "key2" => array:1 [
46+
// "value" => "second"
47+
// ]
48+
// 0 => array:1 [
49+
// "value" => "third"
50+
// ]
5151
// ]
52-
// }
52+
// ]
5353
//

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,15 @@ public function __construct(
2626
]
2727
], ExampleDTO::class);
2828
} catch (RuntimeException $e) {
29-
// Before
3029
var_dump($e->getMessage());
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
3430

3531
// Replace all "expected type" definition to PHP-supported printer
3632
// instead of PrettyPrinter
3733
$e->template->types = new \TypeLang\Printer\NativeTypePrinter();
3834

39-
// After
35+
// Before: Passed value in "values" of {"values": 42} must be of type
36+
// list<ExampleDTO>, but 42 given at $.values[1].values
37+
// After: Passed value in "values" of {"values": 42} must be of type
38+
// array, but 42 given at $.values[1].values
4039
var_dump($e->getMessage());
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
4440
}

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@ public function __construct(
2828
],
2929
], ExampleDTO::class);
3030
} catch (RuntimeException $e) {
31-
// Before
3231
var_dump($e->getMessage());
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
3632

3733
// Print all NamedTypeNode AST statements as "!!!MODIFIED!!!" string
3834
$e->template->types = new class extends PrettyPrinter {
@@ -42,9 +38,9 @@ protected function printNamedTypeNode(NamedTypeNode $node): string
4238
}
4339
};
4440

45-
// After
41+
// Before: Passed value in "values" of {"values": 42} must be of type
42+
// list<ExampleDTO>, but 42 given at $.values[1].values
43+
// After: Passed value in "values" of {"values": 42} must be of type
44+
// !!!MODIFIED!!!, but 42 given at $.values[1].values
4645
var_dump($e->getMessage());
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
5046
}

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

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,37 +28,30 @@ public function __construct(
2828
],
2929
], ExampleDTO::class);
3030
} catch (RuntimeException $e) {
31-
// Before
3231
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
3732

3833
// Print all values using PHP-compatible types
3934
$e->template->values = new PHPValuePrinter();
4035

41-
// After#1
36+
// Before: Passed value in "values" of {"values": 42} must be of type
37+
// list<ExampleDTO>, but 42 given at $.values[1].values
38+
// After#1: Passed value in string of stdClass must be of type
39+
// list<ExampleDTO>, but int given at $.values[1].values
4240
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
4741

4842

4943
// In case of symfony/var-dumper is installed, we can use it
5044
if (\Composer\InstalledVersions::isInstalled('symfony/var-dumper')) {
5145
// Print all values using SymfonyValuePrinter
5246
$e->template->values = new SymfonyValuePrinter();
5347

54-
// After#2
48+
// Before: Passed value in "values" of {"values": 42} must be of type
49+
// list<ExampleDTO>, but 42 given at $.values[1].values
50+
// After#1: Passed value in string of stdClass must be of type
51+
// list<ExampleDTO>, but int given at $.values[1].values
52+
// After#2: Passed value in "values" of {#394
53+
// +"values": 42
54+
// } must be of type list<ExampleDTO>, but 42 given at $.values[1].values
5555
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
6356
}
6457
}

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

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

3633
// Print full path using ">" delimiter
3734
$e->template->paths = new class implements PathPrinterInterface {
@@ -41,8 +38,10 @@ public function print(PathInterface $path): string
4138
}
4239
};
4340

44-
// After: "at ExampleDTO > values > 1 > ExampleDTO > values"
41+
// Before: Passed value in "values" of {"values": 42} must be of type
42+
// list<ExampleDTO>, but 42 given at $.values[1].values
43+
// After: Passed value in "values" of {"values": 42} must be of type
44+
// list<ExampleDTO>, but 42 given at ExampleDTO > values >
45+
// 1 > ExampleDTO > values
4546
var_dump($e->getMessage());
46-
// Passed value of field "values" must be of type list<ExampleDTO>,
47-
// but 42 given at ExampleDTO > values > 1 > ExampleDTO > values
4847
}

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,30 @@ public function cast(mixed $value, Context $context): mixed
8787
// ]
8888
//
8989

90-
var_dump($mapper->normalize([], 'non-empty<string>'));
90+
try {
91+
var_dump($mapper->normalize([], 'non-empty<string>'));
92+
} catch (\Throwable $e) {
93+
echo $e->getMessage() . "\n";
94+
}
9195
//
9296
// InvalidValueException: Passed value [] is invalid
9397
//
9498

95-
96-
var_dump($mapper->normalize('example', 'non-empty'));
99+
try {
100+
var_dump($mapper->normalize('example', 'non-empty'));
101+
} catch (\Throwable $e) {
102+
echo $e->getMessage() . "\n";
103+
}
97104
//
98105
// MissingTemplateArgumentsException: Type "non-empty" expects at least 1
99106
// template argument(s), but 0 were passed
100107
//
101108

102-
var_dump($mapper->normalize('', 'non-empty<string>'));
109+
try {
110+
var_dump($mapper->normalize('', 'non-empty<string>'));
111+
} catch (\Throwable $e) {
112+
echo $e->getMessage() . "\n";
113+
}
103114
//
104115
// InvalidValueException: Passed value "" is invalid
105116
//

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,22 @@ public function __construct(
4545

4646
$mapper = new Mapper(new SimplePlatform());
4747

48-
var_dump($mapper->normalize(new ExampleDTO()));
48+
try {
49+
var_dump($mapper->normalize(new ExampleDTO()));
50+
} catch (\Throwable $e) {
51+
echo $e->getMessage() . "\n";
52+
}
4953
//
5054
// TypeRequiredException: Type "int" for property ExampleDTO::$value
5155
// is not defined
5256
//
5357

54-
var_dump($mapper->normalize([new ExampleDTO()], 'array<ExampleDTO>'));
58+
try {
59+
var_dump($mapper->normalize([new ExampleDTO()], 'array<ExampleDTO>'));
60+
} catch (\Throwable $e) {
61+
echo $e->getMessage() . "\n";
62+
}
5563
//
56-
// ParseException: Template arguments not allowed in "array<ExampleDTO>" at column 6
64+
// ParseException: Template arguments not allowed in "array<ExampleDTO>"
65+
// at column 6
5766
//

example/03.types/06.custom-type-psr-container.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use TypeLang\Mapper\Platform\DelegatePlatform;
99
use TypeLang\Mapper\Platform\StandardPlatform;
1010
use TypeLang\Mapper\Runtime\Context;
11-
use TypeLang\Mapper\Type\Builder\CallableTypeBuilder;
1211
use TypeLang\Mapper\Type\Builder\PsrContainerTypeBuilder;
1312
use TypeLang\Mapper\Type\TypeInterface;
1413

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use TypeLang\Mapper\Mapper;
6+
7+
require __DIR__ . '/../../vendor/autoload.php';
8+
9+
class ExampleDTO
10+
{
11+
public function __construct(
12+
public readonly array $value = [],
13+
) {}
14+
}
15+
16+
// Create standard platform with REFLECTION READER
17+
$platform = new \TypeLang\Mapper\Platform\StandardPlatform(
18+
meta: new \TypeLang\Mapper\Mapping\Reader\ReflectionReader(),
19+
);
20+
21+
$mapper = new Mapper($platform);
22+
23+
var_dump($mapper->denormalize([
24+
'value' => ['string', 'string 2'],
25+
], ExampleDTO::class));
26+
27+
//
28+
// Because NATIVE type hint is "array" that infers to "array<array-key, mixed>"
29+
//
30+
// object(ExampleDTO)#345 (1) {
31+
// ["value"] => array(2) {
32+
// [0] => string(6) "string"
33+
// [1] => string(8) "string 2"
34+
// }
35+
// }
36+
//
37+
38+

0 commit comments

Comments
 (0)