vitalii.shveider/shveider-dto

1.0.4 2024-06-08 12:11 UTC

This package is auto-updated.

Last update: 2024-09-08 12:47:27 UTC


README

这个库可以帮助您管理您的DTO类。

有几种使用方法。

此外,您还可以使用\ShveiderDto\Attributes\\命名空间中的属性配置您的DTO。

DTO具有在接口DataTransferObjectInterface.php中描述的方法

    /** - Takes values from array and set it to defined properties in your data transfer object. */
    public function fromArray(array $data): static;

    /** - Takes properties in your data transfer object and returns it ass array key => value. */
    public function toArray(bool $recursive = false): array;

    /**
     *  - Takes modified properties in your data transfer object and returns it ass array key => value.
     *  - Modified properties: properties that was modified by fromArray and set* method.
     */
    public function modifiedToArray(): array;

    /** - Calls toArray method inside and convert it to json string. */
    public function toJson(bool $pretty = false): string;

库中的所有类都是可扩展的。您可以在项目级别扩展它并进行修改。

入门

属性

<?php

namespace ShveiderDtoTest\DTO\Module3\Transfers;

use DateTime;
use ShveiderDto\AbstractTransfer;
use ShveiderDto\Attributes\ArrayOf;
use ShveiderDtoTest\DTO\Module3\Transfers\Generated\UserCollectionTransferTrait;

class UserCollectionTransfer extends AbstractTransfer
{
    use UserCollectionTransferTrait;

    #[ArrayOf(type: '\ShveiderDtoTest\DTO\Module2\Transfers\UserTransfer', singular: 'user')]
    protected array $users;

    #[ArrayOf(type: DateTime::class, singular: 'date')]
    protected array $dates = [];

    #[ArrayOf('array', 'orderData')]
    protected array $ordersList = [];

    public function mapArrayToDates(array $date): void
    {
        $this->dates = [];

        foreach ($date as $item) {
            $this->dates[] = new DateTime(
                $item['date'] ?? 'now',
                    isset($item['timezone']) ? new \DateTimeZone($item['timezone']): null
            );
        }
    }
}

将为这些属性生成的特质

<?php

namespace ShveiderDtoTest\DTO\Module3\Transfers\Generated;

/** Auto generated class. Do not change anything here. */
trait UserCollectionTransferTrait
{
	protected array $__registered_vars = ['users', 'dates', 'ordersList'];

	protected array $__registered_transfers = [];

	protected array $__registered_array_transfers = ['users' => '\ShveiderDtoTest\DTO\Module2\Transfers\UserTransfer'];

	/** @return array<\ShveiderDtoTest\DTO\Module2\Transfers\UserTransfer> */
	public function getUsers(): array { return $this->users;}
	public function setUsers(array $v): static { $this->__modified['users'] = true; $this->users = $v; return $this;}
	/** @return array<\DateTime> */
	public function getDates(): array { return $this->dates;}
	public function setDates(array $v): static { $this->__modified['dates'] = true; $this->dates = $v; return $this;}
	/** @return array<array> */
	public function getOrdersList(): array { return $this->ordersList;}
	public function setOrdersList(array $v): static { $this->__modified['ordersList'] = true; $this->ordersList = $v; return $this;}
	public function addUser(\ShveiderDtoTest\DTO\Module2\Transfers\UserTransfer $v): static { $this->__modified['users'] = true; $this->users[] = $v; return $this;}
	public function addDate(\DateTime $v): static { $this->__modified['dates'] = true; $this->dates[] = $v; return $this;}
	public function addOrderData(array $v): static { $this->__modified['ordersList'] = true; $this->ordersList[] = $v; return $this;}
}

创建您的DTO并扩展AbstractTransfer。但您的DTO中包含多个配置。

示例