Giter VIP home page Giter VIP logo

php-dto-mapper's Introduction

Update 1.10

1. multi array bind with create Class

if $userList = [
                    ['user_nm' => 'A' , 'age' => 15],
                    ['user_nm' => 'B' , 'age' => 16],
                ];
/** @var User[] */
public array $userList;

// then  get_class($this->userList[0]) === User::class;
                

2. @separator

@separator
// if ?numbers="123,456,789"
/** @var int[] @separator ","  */
public array $numbers;

// then  $numbers = [123,456,789];
// if /** @var string[] @separator ","  */
// then  $numbers = ['123','456','789'];

3. @ParamName

// if ?number="123"
/** @ParamName("number") or @ParamName(number) or @ParamName('number')   */
public int $no;

// then  $this->no = 123;

4. @Column('name=')

// if $db = [ 'usr_nm' => 'seungtae.yoo' ];

/** @Column('usr_nm') or @Column(anyString = 'user_nm')   */
public string $userName;

// then  $this->userName = "seungtae.yoo";

What is PHP-DataTransferObject-Mapper?

This mapper is useful when converting data in an array into DTO.

It automatically manages the type when using the type hint in the latest PHP version (7.4 or higher) environment, so that developers do not care about the type.

External libraries are not required and only operate with PHP APIs.

If there is a provider layer in the framework, you can apply this mapper to implement it like a Command Object.

How to use

f you use it simply for data mapping, you only need to use one data transfer mapper.php file.

If you want to use it like a command object, use the whole thing.

<?php
// formData from Http Request
$_REQUEST = [
    'name' => 'styoo',
    'age' => '35'
];

class Controller
{
    // converting array data into DTO.
    public function example()
    {
        $mapper = new DataTransferObjectMapper();
        /** @var User $user */
        $user = $mapper->mapping($_REQUEST, User::class)->getClass();
        if ($mapper->hasErrors()) throw new Exception(var_export($mapper->getErrors(), true));
        $user->getName() === 'styoo';
        $user->getAge() === 35;
    }

    // If there is a provider layer in the framework,
    // you can apply this mapper to implement it like a Command Object.
    public function index(User $user)
    {
        if ($user->hasErrors()) throw new Exception(var_export($user->getErrors(), true));

        $user->getName() === 'styoo';
        $user->getAge() === 35;
    }
}

// Inheritance is only necessary when used as a command object.
class User extends CommandObject
{
    private string $name;
    private int $age;

    public function getName(): string
    {
        return $this->name;
    }

    public function getAge(): int
    {
        return $this->age;
    }

    protected function rule(): array
    {
        return ['name' => ['required'], 'age' => ['required', 'int']];
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.