type-lang / mapper
Library for mapping variable types to other types
Installs: 4 244
Dependents: 1
Suggesters: 0
Security: 0
Stars: 32
Watchers: 1
Forks: 2
Open Issues: 2
pkg:composer/type-lang/mapper
Requires
- php: ^8.1
- psr/clock: ^1.0
- psr/log: ^1.0|^2.0|^3.0
- psr/simple-cache: ^1.0|^2.0|^3.0
- type-lang/parser: ^1.5
- type-lang/printer: ^1.3
Requires (Dev)
- doctrine/instantiator: ^2.0
- friendsofphp/php-cs-fixer: ^3.89
- jetbrains/phpstorm-attributes: ^1.2
- justinrainbow/json-schema: ^6.6
- marc-mabe/php-enum: ^4.7
- monolog/monolog: ^3.9
- nette/neon: ^3.4
- nikic/php-parser: ^5.6
- phpstan/phpstan: ^2.1
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^10.5|^11.0|^12.0
- symfony/cache: ^6.4|^7.0
- symfony/expression-language: ^6.4|^7.0
- symfony/property-access: ^6.4|^7.0
- symfony/stopwatch: ^6.4|^7.0
- symfony/var-dumper: ^6.4|^7.0
- symfony/yaml: ^6.4|^7.0
- type-lang/phpdoc: ^1.0
- type-lang/phpdoc-standard-tags: ^1.0
Suggests
- ext-json: Required for JSON mapping configuration files
- doctrine/instantiator: (^2.0) A faster way to create class instances
- justinrainbow/json-schema: (^6.0) Required for file-based configuration validation
- nette/neon: (^3.0) Required for NEON mapping configuration files
- symfony/yaml: (^6.4|^7.0) Required for YAML mapping configuration files
- type-lang/phpdoc-standard-tags: (^1.0) Required for PhpDoc mapping configuration
This package is auto-updated.
Last update: 2025-10-28 20:38: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
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.
object<ExampleObject>{
    name: string,
    items: list<ExampleObject>
}
The results are sorted by mode time.
Denormalization
Denormalization: For example, conversion from JSON to PHP Object.
| benchmark | memory | min | mode | rstdev | 
|---|---|---|---|---|
| JMSAttributesBench | 853.248kb | 41.890μs | 42.562μs | ±6.53% | 
| JMSAttributesWithSymfonyPsr6Bench | 718.816kb | 42.500μs | 42.926μs | ±0.46% | 
| TypeLangAttributesWithSymfonyPsr16Bench | 721.720kb | 52.590μs | 53.200μs | ±0.56% | 
| TypeLangDocBlockBench | 839.008kb | 52.490μs | 53.155μs | ±6.83% | 
| TypeLangDocBlockWithSymfonyPsr16Bench | 786.512kb | 52.500μs | 52.976μs | ±2.31% | 
| TypeLangAttributesWithSymfonyPsr6Bench | 717.960kb | 52.840μs | 53.031μs | ±0.87% | 
| TypeLangDocBlockWithSymfonyPsr6Bench | 783.312kb | 52.940μs | 53.203μs | ±0.24% | 
| TypeLangAttributesBench | 1.209mb | 53.160μs | 53.459μs | ±1.55% | 
| SymfonyPHPStanBench | 710.344kb | 140.460μs | 141.594μs | ±0.64% | 
| SymfonyDocBlockBench | 1.850mb | 140.910μs | 142.580μs | ±0.89% | 
| ValinorBench | 894.984kb | 142.120μs | 142.259μs | ±0.44% | 
| ValinorWithCustomPsr16Bench | 574.496kb | 143.380μs | 144.214μs | ±0.73% | 
| ValinorWithSymfonyPsr16Bench | 548.008kb | 145.680μs | 151.617μs | ±1.83% | 
| SymfonyDocBlockWithSymfonyPsr6Bench | 644.608kb | 200.810μs | 203.024μs | ±1.48% | 
| SymfonyPHPStanWithSymfonyPsr6Bench | 565.376kb | 202.000μs | 202.599μs | ±1.21% | 
Normalization
Normalization: For example, conversion from PHP Object to JSON.
| benchmark | memory | min | mode | rstdev | 
|---|---|---|---|---|
| ValinorWithCustomPsr16Bench | 885.496kb | 10.060μs | 10.107μs | ±0.79% | 
| ValinorBench | 866.440kb | 38.250μs | 38.419μs | ±2.14% | 
| SymfonyPHPStanBench | 647.632kb | 41.220μs | 43.365μs | ±2.17% | 
| SymfonyDocBlockBench | 1.850mb | 43.120μs | 43.370μs | ±1.11% | 
| TypeLangAttributesWithSymfonyPsr16Bench | 721.720kb | 50.520μs | 50.844μs | ±2.82% | 
| TypeLangAttributesWithSymfonyPsr6Bench | 717.960kb | 50.590μs | 50.952μs | ±1.46% | 
| TypeLangAttributesBench | 1.200mb | 50.610μs | 51.502μs | ±1.25% | 
| TypeLangDocBlockWithSymfonyPsr6Bench | 783.256kb | 50.800μs | 50.859μs | ±0.37% | 
| TypeLangDocBlockBench | 838.272kb | 50.810μs | 51.446μs | ±0.60% | 
| TypeLangDocBlockWithSymfonyPsr16Bench | 786.456kb | 51.960μs | 52.670μs | ±20.25% | 
| JMSAttributesBench | 927.920kb | 56.070μs | 56.750μs | ±2.30% | 
| JMSAttributesWithSymfonyPsr6Bench | 793.808kb | 56.170μs | 57.160μs | ±2.00% | 
| SymfonyDocBlockWithSymfonyPsr6Bench | 603.352kb | 77.490μs | 78.519μs | ±1.21% | 
| SymfonyPHPStanWithSymfonyPsr6Bench | 448.344kb | 78.220μs | 79.170μs | ±1.93% | 
| ValinorWithSymfonyPsr16Bench* | 436.760kb | ERROR | ERROR | ±31.62% | 
- ValinorWithSymfonyPsr16Bench- cuyz/valinor does not support PSR-16 cache (not compatible with other implementations): CuyZ/Valinor#623- Uncaught Error: Object of type CuyZ\Valinor\Normalizer\Transformer\EvaluatedTransformer is not callable