bermudaphp / dto-factory
v2.0
2024-07-26 00:06 UTC
Requires
- php: ^8.3
- bermudaphp/config: ^1.3
- bermudaphp/validation: ^1.0
- cocur/slugify: ^4.5.1
README
composer require bermudaphp/dto-factory
Usage
readonly class CreatePostData { public function __construct( public string $title, #[From('title', true)] #[Cast(Slugify::class)] public string $slug, #[Cast('json')] public array $content, public UserReference|User $author, #[From('categoryId')] #[Cast([CategoryReference::class, 'fromId'])] public null|CategoryReference|Category $category, #[Cast([TagsCollection::class, 'fromJsonString'])] #[Defaults(null)] public readonly ?iterable $tags, #[Cast('json')] public ?array $metadata, #[Cast(Boolean::class)] #[Defaults(true)] public bool $commentable, #[Defaults(false)] public bool $pinned, #[Invoke('now')] public CarbonInterface $createdAt, #[Cast([Clock::class, 'create'])] public ?CarbonInterface $publishedAt = null, public ?CarbonInterface $updatedAt = null, public ?CarbonInterface $deletedAt = null, ) { } } $factory = new ObjectFactory; $dto = factory->make(CreatePostData::class, [ 'title' => 'My First Post', 'content' => 'valid json string', 'author' => new User(1), 'categoryId' => 2 ], true)