|
| 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\Builder\PsrContainerTypeBuilder; |
| 13 | +use TypeLang\Mapper\Type\TypeInterface; |
| 14 | + |
| 15 | +require __DIR__ . '/../../vendor/autoload.php'; |
| 16 | + |
| 17 | + |
| 18 | +class Container implements ContainerInterface |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var array<non-empty-string, object> |
| 22 | + */ |
| 23 | + private array $services; |
| 24 | + |
| 25 | + public function __construct() |
| 26 | + { |
| 27 | + $this->services = [ |
| 28 | + MyNonEmptyStringType::class => new MyNonEmptyStringType() |
| 29 | + ]; |
| 30 | + } |
| 31 | + |
| 32 | + public function get(string $id) |
| 33 | + { |
| 34 | + return $this->services[$id] ?? throw new \RuntimeException("Service $id not found"); |
| 35 | + } |
| 36 | + |
| 37 | + public function has(string $id): bool |
| 38 | + { |
| 39 | + return array_key_exists($id, $this->services); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +// Add new type (must implement TypeInterface) |
| 45 | +class MyNonEmptyStringType implements TypeInterface |
| 46 | +{ |
| 47 | + public function match(mixed $value, Context $context): bool |
| 48 | + { |
| 49 | + return \is_string($value) && $value !== ''; |
| 50 | + } |
| 51 | + |
| 52 | + public function cast(mixed $value, Context $context): string |
| 53 | + { |
| 54 | + if (\is_string($value) && $value !== '') { |
| 55 | + return $value; |
| 56 | + } |
| 57 | + |
| 58 | + throw new InvalidValueException( |
| 59 | + value: $value, |
| 60 | + path: $context->getPath(), |
| 61 | + template: 'Passed value cannot be empty, but {{value}} given', |
| 62 | + ); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +$container = new Container(); |
| 67 | + |
| 68 | +$mapper = new Mapper(new DelegatePlatform( |
| 69 | + // Extend existing platform (StandardPlatform) |
| 70 | + delegate: new StandardPlatform(), |
| 71 | + types: [ |
| 72 | + // Additional type |
| 73 | + new PsrContainerTypeBuilder('custom-string', $container, MyNonEmptyStringType::class), |
| 74 | + ], |
| 75 | +)); |
| 76 | + |
| 77 | +$result = $mapper->normalize(['example', ''], 'list<custom-string>'); |
| 78 | + |
| 79 | +var_dump($result); |
| 80 | +// |
| 81 | +// expected exception: |
| 82 | +// TypeLang\Mapper\Exception\Mapping\InvalidIterableValueException: |
| 83 | +// Passed value "" on index 1 in ["example", ""] is invalid at $[1] |
| 84 | +// |
| 85 | +// previous exception: |
| 86 | +// TypeLang\Mapper\Exception\Mapping\InvalidValueException: |
| 87 | +// Passed value cannot be empty, but "" given at $[1] |
| 88 | +// |
0 commit comments