Skip to content

jhmhubdnc/php-dto-mapper

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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']];
    }
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%