Skip to content

Repository files navigation


PHP 8.1+Latest Stable VersionLatest Unstable VersionLicense MIT

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

useTypeLang\Mapper\Mapping\MapType;
class ExampleObject
{
publicfunction__construct(
#[MapType('list<non-empty-string>')]
publicreadonlyarray$names,
) {}
}
$mapper = new \TypeLang\Mapper\Mapper();
$result = $mapper->normalize(
newExampleObject(['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

Results here like this.

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.

benchmarkmemoryminmoderstdev
TypeLangStrictAttributesBench1.929mb91.180μs105.264μs±6.99%
TypeLangStrictPhpDocBench2.048mb98.860μs108.488μs±4.40%
TypeLangPhpDocBench2.050mb103.380μs106.407μs±5.91%
TypeLangAttributesBench1.930mb104.490μs113.736μs±4.23%
JMSAttributesBench3.141mb137.040μs142.373μs±6.78%
ValinorBench2.701mb227.370μs252.458μs±4.59%
SymfonyPHPStanExtractorBench3.678mb371.650μs392.908μs±2.86%
SymfonyPhpDocExtractorBench4.027mb379.740μs387.303μs±3.21%

Normalization

Normalization: For example, conversion from PHP Object to JSON.

benchmarkmemoryminmoderstdev
ValinorBench1.966mb62.570μs69.184μs±3.99%
TypeLangStrictAttributesBench1.845mb81.090μs88.343μs±6.75%
TypeLangPhpDocBench1.965mb81.800μs90.504μs±5.99%
TypeLangAttributesBench1.891mb85.320μs89.900μs±5.15%
TypeLangStrictPhpDocBench1.967mb89.920μs94.865μs±2.62%
SymfonyPHPStanExtractorBench2.086mb93.160μs100.173μs±5.41%
SymfonyPhpDocExtractorBench2.405mb96.290μs98.729μs±4.17%
JMSAttributesBench3.770mb131.420μs150.310μs±5.85%

Releases

Used by

Contributors

Languages