type-lang / mapper
Library for mapping variable types to other types
0.1.0
2024-10-15 20:58 UTC
Requires
- php: ^8.1
- psr/simple-cache: ^1.0|^2.0|^3.0
- type-lang/parser: ^1.2
- type-lang/printer: ^1.2
- type-lang/reader: ^1.1
Requires (Dev)
- cuyz/valinor: ^1.13
- friendsofphp/php-cs-fixer: ^3.53
- jetbrains/phpstorm-attributes: ^1.0
- jms/serializer: ^3.30
- phpbench/phpbench: ^1.3
- phpdocumentor/reflection-docblock: ^5.4
- phpdocumentor/type-resolver: ^1.8
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^1.11
- phpstan/phpstan-strict-rules: ^1.6
- phpunit/phpunit: ^10.5|^11.0
- rector/rector: ^1.1
- symfony/cache: ^5.4|^6.0|^7.0
- symfony/property-access: ^7.1
- symfony/serializer: ^7.1
- symfony/var-dumper: ^7.1
- type-lang/phpdoc: ^1.0
- type-lang/phpdoc-standard-tags: ^1.0
This package is auto-updated.
Last update: 2024-11-09 22:07:38 UTC
README
The best PHP mapper you've ever seen =)
You can see some examples here:
Full documentation in progress...
Installation
Mapper package is available as Composer repository and can be installed using the following command in a root of your project:
composer require type-lang/mapper
Benchmarks
Sample: An object that contains a collection of objects, which contains another collection of objects.
ExampleObject{ name: string, items: list<ExampleObject> }
Denormalization
Denormalization: Transformation from raw payload (array) to concrete object.
Normalization
Normalization: Transformation from object to raw payload (array).
Quick Start
use TypeLang\Mapper\Mapping\MapType; class ExampleObject { public function __construct( #[MapType('list<non-empty-string>')] public readonly array $names, ) {} } $mapper = new \TypeLang\Mapper\Mapper(); $result = $mapper->normalize( new ExampleObject(['Example']) ); // Expected Result: // // array:1 [ // "names" => array:1 [ // 0 => "Example" // ] // ] $result = $mapper->denormalize([ 'names' => ['first', 'second'] ], ExampleObject::class); // Expected Result: // // ExampleObject {#324 // +names: array:2 [ // 0 => "first" // 1 => "second" // ] // } $result = $mapper->denormalize([ 'names' => ['first', 'second', ''], ], ExampleObject::class); // Expected Result: // // InvalidFieldTypeValueException: Passed value of field "names" must be of type // list<non-empty-string>, but array(3)["first", "second", ""] given at $.names[2]