type-lang / mapper
Library for mapping variable types to other types
Installs: 4 253
Dependents: 1
Suggesters: 0
Security: 0
Stars: 33
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-11-15 14:05:48 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 |
|---|---|---|---|---|
| TypeLangStrictAttributesBench | 1.929mb | 91.180μs | 105.264μs | ±6.99% |
| TypeLangStrictPhpDocBench | 2.048mb | 98.860μs | 108.488μs | ±4.40% |
| TypeLangPhpDocBench | 2.050mb | 103.380μs | 106.407μs | ±5.91% |
| TypeLangAttributesBench | 1.930mb | 104.490μs | 113.736μs | ±4.23% |
| JMSAttributesBench | 3.141mb | 137.040μs | 142.373μs | ±6.78% |
| ValinorBench | 2.701mb | 227.370μs | 252.458μs | ±4.59% |
| SymfonyPHPStanExtractorBench | 3.678mb | 371.650μs | 392.908μs | ±2.86% |
| SymfonyPhpDocExtractorBench | 4.027mb | 379.740μs | 387.303μs | ±3.21% |
Normalization
Normalization: For example, conversion from PHP Object to JSON.
| benchmark | memory | min | mode | rstdev |
|---|---|---|---|---|
| ValinorBench | 1.966mb | 62.570μs | 69.184μs | ±3.99% |
| TypeLangStrictAttributesBench | 1.845mb | 81.090μs | 88.343μs | ±6.75% |
| TypeLangPhpDocBench | 1.965mb | 81.800μs | 90.504μs | ±5.99% |
| TypeLangAttributesBench | 1.891mb | 85.320μs | 89.900μs | ±5.15% |
| TypeLangStrictPhpDocBench | 1.967mb | 89.920μs | 94.865μs | ±2.62% |
| SymfonyPHPStanExtractorBench | 2.086mb | 93.160μs | 100.173μs | ±5.41% |
| SymfonyPhpDocExtractorBench | 2.405mb | 96.290μs | 98.729μs | ±4.17% |
| JMSAttributesBench | 3.770mb | 131.420μs | 150.310μs | ±5.85% |