PHP Classes

File: README.RU.md

Recommend this page to a friend!
  Classes of Andrey Iatsenko   PHP Plain Object with Class Variables Set from Array Values   README.RU.md   Download  
File: README.RU.md
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: PHP Plain Object with Class Variables Set from Array Values
Initialize data objects from arrays of values
Author: By
Last change:
Date: 11 months ago
Size: 11,755 bytes
 

Contents

Class file image Download

ClassTransformer

Packagist Version GitHub Workflow Status Coverage License Packagist Downloads Packagist Downloads

??? ?????????? ???????? ??? ????? ????????????? ????? ????? ?????? ? ?????? ??? ??????. ?? ??? ?? ????????? ?????? ????????? ???????, ??????????? ?? ?? ??????? ??????? ? ?.?. ??????? ?????? ? ??????? - ?????? ?????? ? ?????? ?????.

??????? ????????? ????????? ????????? ???? ???????????? ?? ????????? ??????? ? ???????????. ??? ??????????? ?? ???????, ???????? ????, ????????? ???? ? ?.?. ??? ???????? ?????? ????? ??????, ??? ???????, ???????????? ?????? DataTransfer Object (DTO). DTO - ??? ??????, ??????? ????????? ??? ???????????? ?????? ? ???????? ?? ?? ????? ?????????? ?????????? ? ??????.

????? ???????, ???????/?????? ???????? ? ?????????? ???????? ? ??????? ??????????? ??? ????. ??? ???? ???????, ?????? ??? ?????? ???? ???????? - ??? ????? ???? http ??????, ??, ???? ? ?.?.

??????????????, ??? ?????? ?????? ??????? ??? ?????????? ???????????????? ?????? DTO. ?? ???????????? ?????? ??? ?????? ??????? - ????????????, ? ??????????? ?? ????????????? ????, ???????? ???? ?????? ???????.

????? ?? ?????? ???????? ?????? ?????, ??????? ????? ?? ???? ??? ?????? ? ???????? ? ?????????????? ??????????? DTO.

:scroll: ?????????

????? ????? ???? ?????????? ? ??????? composer:

??????????? ????????? yzen.dev/plain-to-class

> ??????????: ??????? ?????? ?????? ???????????? ?????? PHP 8.1 +.

> ??? PHP ?????? 7.4 ?? ?????? ???????????? ? ????????????? > ? ?????? v0.*. > >??? PHP ?????? 8.0 ?? ?????? ???????????? ? ????????????? > ? ?????? v1.*.

:scroll: ?????????????

????? ??????? ?????????????:

:scroll: Base

namespace DTO;

class CreateUserDTO
{
    public string $email;
    public float $balance;
}

$data = [
    'email' => 'test@mail.com',
    'balance' => 128.41,
];
$dto = ClassTransformer::transform(CreateUserDTO::class, $data);
var_dump($dto);

Result:

object(\LoginDTO)
  'email' => string(13) "test@mail.com"
  'balance' => float(128.41) 

????? ? ?????? php 8 ?? ?????? ?????????? ??????????? ?????????:

$dto = ClassTransformer::transform(CreateUserDTO::class,
        email: 'test@mail.com',
        balance: 128.41
      );

???? ???????? ?? ???????? ????????? ?????, ? ? ???? ???? ?????? ?????, ??? ????? ????????????? ?????????? ? ???? ?????????.

class ProductDTO
{
    public int $id;
    public string $name;
}

class PurchaseDTO
{
    public ProductDTO $product;
    public float $cost;
}

$data = [
    'product' => ['id' => 1, 'name' => 'phone'],
    'cost' => 10012.23,
];

$purchaseDTO = ClassTransformer::transform(PurchaseDTO::class, $data);
var_dump($purchaseDTO);

?????????:

object(PurchaseDTO)
  public ProductDTO 'product' => 
    object(ProductDTO)
      public int 'id' => int 1
      public string 'name' => string 'phone' (length=5)
  public float 'cost' => float 10012.23

:scroll: ?????????

???? ? ??? ???? ?????? ???????? ????????????? ??????, ?? ?? ?????? ??????? ??? ???? ??????? ConvertArray, ??????? ??? ? ????? ????? ??? ????? ???????? ????????.

????? ????? ??????? ????? ? PHP DOC, ?? ????? ??? ????? ???????? ?????? ???? ? ????? ?????? array <\DTO\ProductDTO>. ??? ???????? ??? ????, ????? ????? ?????, ????? ????????? ????? ???????. ????????? Reflection ?? ????????????? ??????? ??????? ??? ????????? ????? use. ?????? use, ?? ?????? ??????? ????????? ? ??? ????? ??????? ?????????. ??????:


class ProductDTO
{
    public int $id;
    public string $name;
}

class PurchaseDTO
{
    #[ConvertArray(ProductDTO::class)]
    public array $products;
}

$data = [
    'products' => [
        ['id' => 1, 'name' => 'phone',],
        ['id' => 2, 'name' => 'bread',],
    ],
];
$purchaseDTO = ClassTransformer::transform(PurchaseDTO::class, $data);

:scroll: ????????? ?????????

? ?????? ???? ??? ????? ????????????? ?????? ?????? ? ?????? ???????? ??????, ?? ?????? ??????????? ??? ? ??????? ?????? transformCollection.

$data = [
  ['id' => 1, 'name' => 'phone'],
  ['id' => 2, 'name' => 'bread'],
];
$products = ClassTransformer::transformCollection(ProductDTO::class, $data);

? ?????????? ????? ?? ???????? ?????? ???????? ProductDTO

array(2) {
  [0]=>
      object(ProductDTO) {
        ["id"]=> int(1)
        ["name"]=> string(5) "phone"
      }
  [1]=>
      object(ProductDTO) {
        ["id"]=> int(2)
        ["name"]=> string(5) "bread"
      }
} 

??? ????? ????? ????????????? ???????????? ?????????????? ???????. ? ????? ?????? ?? ?????? ???????? ?????? ???????, ??????? ????? ????? ????? ???????????

    $userData = ['id' => 1, 'email' => 'test@test.com', 'balance' => 10012.23];
    $purchaseData = [
        'products' => [
            ['id' => 1, 'name' => 'phone',],
            ['id' => 2, 'name' => 'bread',],
        ],
        'user' => ['id' => 3, 'email' => 'fake@mail.com', 'balance' => 10012.23,],
    ];

    $result = ClassTransformer::transformMultiple([UserDTO::class, PurchaseDTO::class], [$userData, $purchaseData]);
    
    [$user, $purchase] = $result;
    var_dump($user);
    var_dump($purchase);

Result:

object(UserDTO) (3) {
  ["id"] => int(1)
  ["email"]=> string(13) "test@test.com"
  ["balance"]=> float(10012.23)
}

object(PurchaseDTO) (2) {
  ["products"]=>
  array(2) {
    [0]=>
    object(ProductDTO)#349 (3) {
      ["id"]=> int(1)
      ["name"]=> string(5) "phone"
    }
    [1]=>
    object(ProductDTO)#348 (3) {
      ["id"]=> int(2)
      ["name"]=> string(5) "bread"
    }
  }
  ["user"]=>
  object(UserDTO)#332 (3) {
    ["id"]=> int(3)
    ["email"]=> string(13) "fake@mail.com"
    ["balance"]=> float(10012.23)
  }
}

:scroll: ????? ?????????

?????????? ???????? ?? ?????? ?????????, ????????, ? ???? ?????? ??? snake_case, ? ? ???? camelCase. ? ?? ????????? ????? ???-?? ????????????????. ????? ??????????? ?? ????, ??? ?????? ????? ??????? ??????? WritingStyle ? ????????:

class WritingStyleSnakeCaseDTO
{
    #[WritingStyle(WritingStyle::STYLE_CAMEL_CASE, WritingStyle::STYLE_SNAKE_CASE)]
    public string $contact_fio;

    #[WritingStyle(WritingStyle::STYLE_CAMEL_CASE)]
    public string $contact_email;
}


 $data = [
  'contactFio' => 'yzen.dev',
  'contactEmail' => 'test@mail.com',
];
$model = ClassTransformer::transform(WritingStyleSnakeCaseDTO::class, $data);
var_dump($model);

RESULT:

object(WritingStyleSnakeCaseDTO) (2) {
  ["contact_fio"]=> string(8) "yzen.dev"
  ["contact_email"]=> string(13) "test@mail.com"
}

:scroll: Alias

??? ???????? ????? ?????? ????????? ????????? alias'?, ??????? ????? ????? ???????? ? ????????? ??????. ??? ????? ???? ??????? ???? DTO ??????????? ?? ?????? ?????????? ??????.

class WithAliasDTO
{
    #[FieldAlias('userFio')]
    public string $fio;

    #[FieldAlias(['email', 'phone'])]
    public string $contact;
}

:scroll: ???????????? ????????

???? ???? ??????? ?????????????? ????????? ??? ??? ?????????????, ?? ?????? ?????????? ??? ??????. ??? ??? ???????? ? ?????? ????? ?????????? ??????? - set{$name}Attribute. ??????:

class UserDTO
{
    public int $id;
    public string $real_address;

    public function setRealAddressAttribute(string $value)
    {
        $this->real_address = strtolower($value);
    }
}

:scroll: ???? ?????????

?????? ?????? ?? ?????? ??????? ????? afterTransform, ??????? ????????? ????? ?? ?????????? ??????????????. ? ??? ?? ?????? ??????? ???? ?????????????? ?????? ???????? ??? ?????????????? ??????? ??? ? ?????????? ???????.

class UserDTO
{
    public int $id;
    public float $balance;

    public function afterTransform()
    {
        $this->balance = 777;
    }
}

:scroll: ????????? ??????????????

???? ??? ????????? ????????? ???? ??????????????, ?? ?? ?????? ? ?????? ??????? ????? transform. ? ????? ?????? ??????? ????????? ?????????? ?? ??????????, ??? ??????????????? ?????????????? ????????? ?? ??? ?????.

class CustomTransformUserDTOArray
{
    public string $email;
    public string $username;
    
    public function transform($args)
    {
        $this->email = $args['login'];
        $this->username = $args['fio'];
    }
}