Skip to content

⚡ The best PHP mapper (hydrator/serializer) you've ever seen!

License

NotificationsYou must be signed in to change notification settings

php-type-language/mapper

Repository files navigation


PHP 8.1+Latest Stable VersionLatest Unstable VersionLicense MITMetaStorm

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

Results here like this.

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.

benchmarkmemoryminmaxmoderstdev
TypeLangAttributesBench1.444mb141.800μs156.050μs145.760μs±2.13%
JMSAttributesBench1.429mb144.400μs157.100μs146.736μs±2.12%
TypeLangDocBlockBench1.642mb144.800μs153.850μs148.059μs±1.29%
ValinorBench1.344mb217.550μs229.150μs220.319μs±1.41%
SymfonyDocBlockBench2.163mb495.350μs507.950μs499.492μs±0.72%
SymfonyPHPStanBench1.426mb506.650μs544.500μs510.798μs±1.53%

Denormalization + Cache

benchmarkmemoryminmaxmoderstdev
TypeLangDocBlockBench1.544mb113.250μs125.350μs115.831μs±2.64%
JMSAttributesBench1.429mb125.850μs148.750μs128.718μs±3.68%
TypeLangAttributesBench1.436mb170.100μs182.200μs173.155μs±1.70%
ValinorBench1.257mb341.000μs374.450μs346.891μs±1.94%
SymfonyPHPStanBench1.370mb583.600μs609.050μs590.473μs±0.88%
SymfonyDocBlockBench2.163mb644.350μs686.550μs651.617μs±1.32%

Normalization

Normalization: Transformation from object to raw payload (array).

benchmarkmemoryminmaxmoderstdev
JMSAttributesBench1.476mb93.550μs125.100μs112.011μs±9.21%
TypeLangDocBlockBench1.643mb110.650μs133.000μs112.881μs±4.25%
SymfonyPHPStanBench1.370mb112.850μs121.850μs115.140μs±1.89%
TypeLangAttributesBench1.444mb117.300μs127.250μs120.649μs±2.43%
ValinorBench1.251mb127.300μs135.350μs129.379μs±1.72%
SymfonyDocBlockBench2.163mb153.000μs161.800μs155.170μs±1.39%

Normalization + Cache

benchmarkmemoryminmaxmoderstdev
TypeLangAttributesBench1.447mb65.850μs94.650μs91.945μs±6.51%
TypeLangDocBlockBench1.544mb91.950μs97.250μs93.070μs±1.49%
JMSAttributesBench1.429mb88.150μs105.600μs100.956μs±3.31%
SymfonyPHPStanBench1.370mb136.050μs147.900μs138.879μs±1.96%
ValinorBench1.256mb114.450μs161.600μs152.558μs±5.88%
SymfonyDocBlockBench2.163mb164.300μs221.300μs212.265μs±5.18%