Skip to content

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

License

php-type-language/mapper

Repository files navigation


PHP 8.1+ Latest Stable Version Latest Unstable Version License 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

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.

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

Contributors 2

  •  
  •  

Languages