dkorzhenkov/model-with-preset-data

There is no license information available for the latest version (v3.0.0) of this package.

v3.0.0 2025-05-28 07:35 UTC

This package is auto-updated.

Last update: 2025-05-28 07:37:37 UTC


README

Installation

composer require dkorzhenkov/model-with-preset-data

Usage

use DKorzhenkov\ModelWithPresetData\AbstractModelWithPresetData;
use DKorzhenkov\ModelWithPresetData\Traits\ToArrayExceptNullWithJsonSerializeTrait;
use Symfony\Component\Validator\Constraints as Assert;

class Message extends AbstractModelWithPresetData implements \JsonSerializable
{
    use ToArrayExceptNullWithJsonSerializeTrait;

    protected string $chatUuid;
    protected string $text;

    protected function getRules(array $data): Assert\Collection
    {
        return new Assert\Collection([
            'fields' => [
                'chatUuid' => [
                    new Assert\Type(['type' => 'string']),
                    new Assert\NotBlank(),
                    new Assert\Uuid(),
                ],
                'text' => [
                    new Assert\Type(['type' => 'string']),
                    new Assert\NotBlank(),
                ],
            ],
        ]);
    }
}

$apiClient->pushMessage(new Message([
    'chatUuid' => 'fa100d98-c260-4787-80b5-14c561034288',
    'text' => 'test',
]));

Error handling

use DKorzhenkov\ModelWithPresetData\Exception\InvalidDataException;

try {
    $message = new Message([
        'chatUuid' => 'invalid uuid',
        'text' => 'test',
    ]);
} catch (InvalidDataException $exception) {
    $exception->getViolationList();
    // ...
}

Casts

class MessageInfo extends AbstractModelWithPresetData
{
    protected Chat $chat;
    protected string $text;

    protected function getCasts(): array
    {
        return [
            'chat' => new ArrayToModelCast(Chat::class, false),
        ];
    }

    protected function getRules(array $data): Assert\Collection
    {
        return new Assert\Collection([
            'fields' => [
                'chat' => [
                    new Assert\NotNull(),
                    new Assert\Type(['type' => Chat::class]),
                ],
                'text' => [
                    new Assert\Type(['type' => 'string']),
                    new Assert\NotBlank(),
                ],
            ],
        ]);
    }
    
    public function getChat(): Chat
    {
        return $this->chat;
    }
}

class Chat extends AbstractModelWithPresetData
{
    protected string $uuid;

    protected function getRules(array $data): Assert\Collection
    {
        return new Assert\Collection([
            'fields' => [
                'uuid' => [
                    new Assert\Type(['type' => 'string']),
                    new Assert\NotBlank(),
                    new Assert\Uuid(),
                ],
            ],
        ]);
    }

    public function getUuid()
    {
        return $this->uuid;
    }
}

function handleApiResponse($response): MessageInfo
{
    /**
    * Response:
    * [
    *     'chat' => [
    *         'uuid' => 'fa100d98-c260-4787-80b5-14c561034288'
    *     ],
    *     'text' => 'test',
    * ]
    */
    return new MessageInfo($response->all());
}

$messageInfo = handleApiResponse($response);
$messageInfo->getChat()->getUuid();