type-lang / mapper
Library for mapping variable types to other types
0.3.0
2024-12-09 13:16 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.3
- type-lang/printer: ^1.2
Requires (Dev)
- behat/behat: ^3.14
- friendsofphp/php-cs-fixer: ^3.53
- jetbrains/phpstorm-attributes: ^1.0
- justinrainbow/json-schema: ^6.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
Suggests
- justinrainbow/json-schema: (^5.3|^6.0) Required for configuration drivers validation
- type-lang/phpdoc-standard-tags: (^1.0) Required for DocBlockDriver mapping driver support
This package is auto-updated.
Last update: 2024-12-20 14:50:36 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).