gerfey/mapper

Map nested Json|Array structures.

dev-master 2021-05-08 03:54 UTC

This package is auto-updated.

Last update: 2024-11-08 11:44:34 UTC


README

Source Code Software License Total Downloads

gerfey/mapper is a PHP 7.1+ library for map nested Json/Array structures onto PHP classes.

Installation

The preferred method of installation is via Packagist and Composer. Run the following command to install the package and add it as a requirement to your project's composer.json:

composer require gerfey/mapper @dev

Usage

Entity class

<?php

namespace App\Entity;

use Gerfey\Mapper\Annotation\Field;
use Gerfey\Mapper\Annotation\Rule;

class User
{
    /**
     * @Field(type="int")
     * @Rule(name="limit", params={0,100})
     */
    public $id;

    /**
     * @Field(name="first_name", type="string")
     */
    public $name;

    /**
     * @Field(type="object", passIn="Address")
     */
    public $address;
}
<?php

namespace App\Entity;

class Address
{
    public $street;

    public $city;
}

Map an array of objects:

use App\Entity\User;
use Gerfey\Mapper\Format\ArrayMapper;

$mapper = new ArrayMapper();
$entity = $mapper->map(User::class, [
    'id' => "1",
    'first_name' => "Александр",
    'address' => [
        'street' => 'просп. имени газеты Красноярский Рабочий',
        'city' => 'Красноярск'
    ]
]);
dd($entity);

Result:

User {#337 ▼
  +id: 1
  +name: "Александр"
  +address: Address {#344 ▼
    +street: "просп. имени газеты Красноярский Рабочий"
    +city: "Красноярск"
  }
}

Copyright and License

The gerfey/mapper library is copyright © Alexander Levchenkov and licensed for use under the MIT License (MIT). Please see LICENSE for more information.