2017 © Pedro Peláez
 

library array-to-dto

image

simonisme/array-to-dto

  • Sunday, September 3, 2017
  • by SimonIsMe
  • Repository
  • 1 Watchers
  • 0 Stars
  • 3 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

json-to-dto

This is simple library to convert array to specified value object classes., (*1)

Example

Simple object



class EmailAddress { public $email; public function __construct(string $email) { $this->email = $email; } } $arrayToDtoParse = new ArrayToDtoParser(); $result = $arrayToDtoParse->parseToDto(EmailAddress::class, [ 'address@domain.com'; ]); // $result variable contains correct EmailAddress object echo $result->email; // address@domain.com

Nested object


class User { public $emailAddress; public $number; public $birthDate; public function __construct(EmailAddress $emailAddress, string $name, \DateTime $birthDate) { $this->emailAddress = $emailAddress; $this->number = $name; $this->birthDate = $birthDate; } } class Nested { public $user; public $emailAddress; public function __construct(User $user, EmailAddress $emailAddress) { $this->user = $user; $this->emailAddress = $emailAddress; } } class EmailAddress { public $email; public function __construct(string $email) { $this->email = $email; } } class Dto { public $user; public $nested; public function __construct(User $user, Nested $nested) { $this->user = $user; $this->nested = $nested; } } $arrayToDtoParse = new ArrayToDtoParser(); $result = $arrayToDtoParse->parseToDto(Dto::class, [ 'user' => [ 'emailAddress' => 'address@domain.com', 'name' => 'Simon', 'birthDate' => '1991-01-02', ], 'nested' => [ 'user' => [ 'emailAddress' => 'address2@domain.com', 'name' => 'Simon', 'birthDate' => '1991-01-02', ], 'emailAddress' => 'address3@domain.com' ] ]);

For more examples please look into /tests directory., (*2)

Important

Keys in the array and constructor's parameters have to have the same names!, (*3)

The Versions

03/09 2017

dev-master

9999999-dev https://github.com/SimonIsMe/array-to-dto

  Sources   Download

MIT

The Requires

  • php >=7.1.0

 

The Development Requires

03/09 2017

2.0.2

2.0.2.0 https://github.com/SimonIsMe/array-to-dto

  Sources   Download

MIT

The Requires

  • php >=7.1.0

 

The Development Requires

26/08 2017