Home

Awesome

<a href="https://github.com/php-type-language" target="_blank"> <img align="center" src="https://github.com/php-type-language/.github/blob/master/assets/dark.png?raw=true"> </a>
<p align="center"> <a href="https://packagist.org/packages/type-lang/mapper"><img src="https://poser.pugx.org/type-lang/mapper/require/php?style=for-the-badge" alt="PHP 8.1+"></a> <a href="https://packagist.org/packages/type-lang/mapper"><img src="https://poser.pugx.org/type-lang/mapper/version?style=for-the-badge" alt="Latest Stable Version"></a> <a href="https://packagist.org/packages/type-lang/mapper"><img src="https://poser.pugx.org/type-lang/mapper/v/unstable?style=for-the-badge" alt="Latest Unstable Version"></a> <a href="https://raw.githubusercontent.com/php-type-language/mapper/blob/master/LICENSE"><img src="https://poser.pugx.org/type-lang/mapper/license?style=for-the-badge" alt="License MIT"></a> </p> <p align="center"> <a href="https://github.com/php-type-language/mapper/actions"><img src="https://github.com/php-type-language/mapper/workflows/tests/badge.svg"></a> </p>

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

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>
}

Denormalization

Denormalization: Transformation from raw payload (array) to concrete object.

subjectrevsitsmem_peakmoderstdev
benchJmsWithAttributes50204.466mb32.151μs±1.70%
benchTypeLangWithDocBlocks50203.870mb34.175μs±1.61%
benchTypeLangWithAttributes50203.870mb34.379μs±1.77%
benchValinorWithPhpStan50203.870mb119.750μs±1.21%
benchSymfonyWithDocBlock50203.870mb123.889μs±1.35%
benchSymfonyWithPhpStan50203.870mb126.807μs±1.94%

Normalization

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

subjectrevsitsmem_peakmoderstdev
benchTypeLangWithDocBlocks50203.870mb36.473μs±1.69%
benchTypeLangWithAttributes50203.870mb36.735μs±1.68%
benchSymfonyWithPhpStan50203.870mb37.050μs±1.20%
benchSymfonyWithDocBlock50203.870mb37.125μs±1.50%
benchValinorWithPhpStan50203.870mb38.229μs±1.38%
benchJmsWithAttributes50203.870mb44.614μs±1.92%

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]