gerfey / mapper
Map nested Json|Array structures.
Installs: 3 588
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^7.1|^7.3|^7.4|^8.0
- ext-json: *
- ext-mbstring: *
- ext-reflection: *
- doctrine/annotations: ^1.7
Requires (Dev)
- ext-json: *
- ext-mbstring: *
- ext-reflection: *
This package is auto-updated.
Last update: 2024-11-08 11:44:34 UTC
README
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.