type-lang / mapper
Library for mapping variable types to other types
0.2.0
2024-11-20 01:30 UTC
Requires
- php: ^8.1
- psr/log: ^1.0|^2.0|^3.0
- psr/simple-cache: ^1.0|^2.0|^3.0
- type-lang/parser: ^1.2
- type-lang/printer: ^1.2
Requires (Dev)
- behat/behat: ^3.14
- friendsofphp/php-cs-fixer: ^3.53
- jetbrains/phpstorm-attributes: ^1.0
- monolog/monolog: ^3.7
- phpstan/phpstan: ^2.0
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^10.5|^11.0
- symfony/cache: ^5.4|^6.0|^7.0
- symfony/expression-language: ^5.4|^6.0|^7.0
- symfony/property-access: ^5.4|^6.0|^7.0
- symfony/stopwatch: ^5.4|^6.0|^7.0
- symfony/var-dumper: ^5.4|^6.0|^7.0
- type-lang/phpdoc: ^1.0
- type-lang/phpdoc-standard-tags: ^1.0
This package is auto-updated.
Last update: 2024-11-20 02:21:30 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
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]
Benchmarks
Sample: An object that contains a collection of objects, which contains another collection of objects.
ExampleObject { name: string, items : list<ExampleObject> }
The results are sorted by mode time.
Denormalization
Denormalization: Transformation from raw payload (array) to concrete object.
Denormalization + Cache
Normalization
Normalization: Transformation from object to raw payload (array).