Skip to content

Commit d388c19

Browse files
committed
Added example
1 parent 57c5888 commit d388c19

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ You can see some [examples here](/example):
3737
- [02.custom-type](/example/03.types/02.custom-type.php)
3838
- [03.custom-type-template-arguments](/example/03.types/03.custom-type-template-arguments.php)
3939
- [04.custom-platform](/example/03.types/04.custom-platform.php)
40+
- [05.custom-type-callable](/example/03.types/05.custom-type-callable.php)
4041
- [04.mapping](/example/04.mapping)
4142
- [01.reflection-mapping](/example/04.mapping/01.reflection-mapping.php)
4243
- [02.attribute-mapping](/example/04.mapping/02.attribute-mapping.php)
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Psr\Container\ContainerInterface;
6+
use TypeLang\Mapper\Exception\Mapping\InvalidValueException;
7+
use TypeLang\Mapper\Mapper;
8+
use TypeLang\Mapper\Platform\DelegatePlatform;
9+
use TypeLang\Mapper\Platform\StandardPlatform;
10+
use TypeLang\Mapper\Runtime\Context;
11+
use TypeLang\Mapper\Type\Builder\CallableTypeBuilder;
12+
use TypeLang\Mapper\Type\TypeInterface;
13+
14+
require __DIR__ . '/../../vendor/autoload.php';
15+
16+
17+
class Container implements ContainerInterface
18+
{
19+
/**
20+
* @var array<non-empty-string, object>
21+
*/
22+
private array $services;
23+
24+
public function __construct()
25+
{
26+
$this->services = [
27+
'my-non-empty-type' => new MyNonEmptyStringType()
28+
];
29+
}
30+
31+
public function get(string $id)
32+
{
33+
return $this->services[$id] ?? throw new \RuntimeException("Service $id not found");
34+
}
35+
36+
public function has(string $id): bool
37+
{
38+
return array_key_exists($id, $this->services);
39+
}
40+
}
41+
42+
43+
44+
45+
// Add new type (must implement TypeInterface)
46+
class MyNonEmptyStringType implements TypeInterface
47+
{
48+
public function match(mixed $value, Context $context): bool
49+
{
50+
return \is_string($value) && $value !== '';
51+
}
52+
53+
public function cast(mixed $value, Context $context): string
54+
{
55+
if (\is_string($value) && $value !== '') {
56+
return $value;
57+
}
58+
59+
throw new InvalidValueException(
60+
value: $value,
61+
path: $context->getPath(),
62+
template: 'Passed value cannot be empty, but {{value}} given',
63+
);
64+
}
65+
}
66+
67+
$container = new Container();
68+
69+
$mapper = new Mapper(new DelegatePlatform(
70+
// Extend existing platform (StandardPlatform)
71+
delegate: new StandardPlatform(),
72+
types: [
73+
// Additional type
74+
new CallableTypeBuilder('custom-string', static fn (): TypeInterface => $container->get('my-non-empty-type')),
75+
],
76+
));
77+
78+
$result = $mapper->normalize(['example'], 'list<custom-string>');
79+
80+
var_dump($result);
81+
//
82+
// expected exception:
83+
// TypeLang\Mapper\Exception\Mapping\InvalidIterableValueException:
84+
// Passed value "" on index 1 in ["example", ""] is invalid at $[1]
85+
//
86+
// previous exception:
87+
// TypeLang\Mapper\Exception\Mapping\InvalidValueException:
88+
// Passed value cannot be empty, but "" given at $[1]
89+
//

0 commit comments

Comments
 (0)