Skip to content

Commit 966a8fb

Browse files
author
Kirill Nesmeyanov
committed
Add more examples
1 parent 0e217d6 commit 966a8fb

26 files changed

+669
-204
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 string $value,
13+
) {}
14+
}
15+
16+
$mapper = new Mapper();
17+
18+
$result = $mapper->normalize(new ExampleDTO(value: 'testing'));
19+
20+
var_dump($result);
21+
22+
//
23+
// array:1 [
24+
// "value" => "testing"
25+
// ]
26+
//
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use TypeLang\Mapper\Mapper;
6+
7+
require __DIR__ . '/../../vendor/autoload.php';
8+
9+
$mapper = new Mapper();
10+
11+
// Normalize date object to default RFC3339 string.
12+
$result = $mapper->normalize(new \DateTimeImmutable());
13+
14+
var_dump($result);
15+
//
16+
// string(25) "2024-06-29T16:16:41+00:00"
17+
//
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use TypeLang\Mapper\Mapper;
6+
7+
require __DIR__ . '/../../vendor/autoload.php';
8+
9+
$mapper = new Mapper();
10+
11+
// Normalize date object to "d-m-Y" string.
12+
$result = $mapper->normalize(new \DateTimeImmutable(), 'datetime<"d-m-Y">');
13+
14+
var_dump($result);
15+
//
16+
// string(10) "15-10-2024"
17+
//

example/02.object-normalization.php renamed to example/01.normalization/04.untyped-object-normalization.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@
44

55
use TypeLang\Mapper\Mapper;
66

7-
require __DIR__ . '/../vendor/autoload.php';
8-
9-
// An example of converting an arbitrary object into primitive data.
10-
//
11-
// Please note that the type of the array (ExampleDTO::$children) is not
12-
// specified, which means it will be normalized "as is".
7+
require __DIR__ . '/../../vendor/autoload.php';
138

149
class ChildDTO
1510
{
@@ -36,6 +31,7 @@ public function __construct(
3631
));
3732

3833
var_dump($result);
34+
3935
//
4036
// array:1 [
4137
// "children" => array:3 [

example/03.typed-object-normalization.php renamed to example/01.normalization/05.typed-object-normalization.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use TypeLang\Mapper\Mapping\MapProperty;
88
use TypeLang\Mapper\Platform\StandardPlatform;
99

10-
require __DIR__ . '/../vendor/autoload.php';
10+
require __DIR__ . '/../../vendor/autoload.php';
1111

1212
// The attribute driver is used to specify default types. To specify a specific
1313
// type, just add the #[MapProperty] attribute.
@@ -29,9 +29,7 @@ public function __construct(
2929
) {}
3030
}
3131

32-
$mapper = new Mapper(new StandardPlatform(
33-
driver: new AttributeDriver(),
34-
));
32+
$mapper = new Mapper();
3533

3634
$result = $mapper->normalize(new ExampleDTO(
3735
children: [
@@ -41,6 +39,8 @@ public function __construct(
4139
]
4240
));
4341

42+
var_dump($result);
43+
4444
//
4545
// InvalidValueException: Passed value must be of type ChildDTO{name: string},
4646
// but int (42) given at $.children[2]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use TypeLang\Mapper\Mapper;
6+
7+
require __DIR__ . '/../../vendor/autoload.php';
8+
9+
class ItemDTO
10+
{
11+
public function __construct(
12+
public readonly string $value,
13+
) {}
14+
}
15+
16+
class ItemsResultDTO
17+
{
18+
public function __construct(
19+
public readonly array $items,
20+
) {}
21+
}
22+
23+
$mapper = new Mapper();
24+
25+
$value = new ItemsResultDTO([
26+
'key1' => new ItemDTO(value: 'first'),
27+
'key2' => new ItemDTO(value: 'second'),
28+
new ItemDTO(value: 'third'),
29+
]);
30+
31+
$result = $mapper->normalize($value);
32+
33+
dd($result);
34+
//
35+
// array:1 [
36+
// "items" => array:3 [
37+
// "key1" => array:1 [
38+
// "value" => "first"
39+
// ]
40+
// "key2" => array:1 [
41+
// "value" => "second"
42+
// ]
43+
// 0 => array:1 [
44+
// "value" => "third"
45+
// ]
46+
// ]
47+
// ]
48+
//
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use TypeLang\Mapper\Mapper;
6+
7+
require __DIR__ . '/../../vendor/autoload.php';
8+
9+
class ItemDTO
10+
{
11+
public function __construct(
12+
public readonly string $value,
13+
) {}
14+
}
15+
16+
class ItemsCollection implements \IteratorAggregate
17+
{
18+
public function __construct(
19+
private readonly array $items,
20+
) {}
21+
22+
public function getIterator(): Traversable
23+
{
24+
return new \ArrayIterator($this->items);
25+
}
26+
}
27+
28+
$mapper = new Mapper();
29+
30+
$value = new ItemsCollection([
31+
'key1' => new ItemDTO(value: 'first'),
32+
'key2' => new ItemDTO(value: 'second'),
33+
new ItemDTO(value: 'third'),
34+
]);
35+
36+
// Transform collection of ItemDTO to list (array without keys)
37+
// of normalized ItemDTO (key-val array)
38+
$result = $mapper->normalize($value, 'list<ItemDTO>');
39+
40+
var_dump($result);
41+
//
42+
// array:3 [
43+
// 0 => array:1 [
44+
// "value" => "first"
45+
// ]
46+
// 1 => array:1 [
47+
// "value" => "second"
48+
// ]
49+
// 2 => array:1 [
50+
// "value" => "third"
51+
// ]
52+
// ]
53+
//
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use TypeLang\Mapper\Mapper;
6+
7+
require __DIR__ . '/../../vendor/autoload.php';
8+
9+
class ItemDTO
10+
{
11+
public function __construct(
12+
public readonly string $value,
13+
) {}
14+
}
15+
16+
class ItemsResultDTO
17+
{
18+
public function __construct(
19+
public readonly array $items,
20+
) {}
21+
}
22+
23+
$mapper = (new Mapper())
24+
->withObjectsAsArrays(false);
25+
26+
$value = new ItemsResultDTO([
27+
'key1' => new ItemDTO(value: 'first'),
28+
'key2' => new ItemDTO(value: 'second'),
29+
new ItemDTO(value: 'third'),
30+
]);
31+
32+
$result = $mapper->normalize($value);
33+
34+
var_dump($result);
35+
//
36+
// object{
37+
// items: array:3 [
38+
// "key1" => object{
39+
// value: "first"
40+
// }
41+
// "key2" => object{
42+
// value: "second"
43+
// }
44+
// 0 => object{
45+
// value: "third"
46+
// }
47+
// ]
48+
// }
49+
//

example/01.simple-normalization.php

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use TypeLang\Mapper\Exception\Mapping\MappingExceptionInterface;
6+
use TypeLang\Mapper\Mapper;
7+
use TypeLang\Mapper\Mapping\MapProperty;
8+
use TypeLang\Parser\Node\Name;
9+
10+
require __DIR__ . '/../../vendor/autoload.php';
11+
12+
class ExampleDTO
13+
{
14+
public function __construct(
15+
#[MapProperty(type: 'list<ExampleDTO>')]
16+
public readonly array $values = [],
17+
) {}
18+
}
19+
20+
$mapper = new Mapper();
21+
22+
try {
23+
$result = $mapper->denormalize([
24+
'values' => [
25+
['values' => []],
26+
['values' => 42],
27+
]
28+
], ExampleDTO::class);
29+
} catch (MappingExceptionInterface $e) {
30+
// Before: "list<ExampleDTO>"
31+
var_dump($e->getMessage());
32+
// Passed value of field "values" must be of type list<ExampleDTO>,
33+
// but 42 given at $.values[1].values
34+
35+
// Replace all internal type names with common "object" keyword.
36+
$e = $e->explain(fn (Name $name): ?Name => \class_exists($name->toString())
37+
? new Name('object')
38+
: null);
39+
40+
// After: "list<object>"
41+
var_dump($e->getMessage());
42+
// Passed value of field "values" must be of type list<object>,
43+
// but 42 given at $.values[1].values
44+
}

0 commit comments

Comments
 (0)