dkorzhenkov/model-with-preset-data
最新稳定版本:v3.0.0
Composer 安装命令:
composer require dkorzhenkov/model-with-preset-data
包简介
README 文档
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();
统计信息
- 总下载量: 188
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2025-05-11