kosmcode/simple-dto

Package to create simple flexible DTOs

1.0.0 2023-04-20 17:16 UTC

This package is not auto-updated.

Last update: 2024-10-04 22:58:20 UTC


README

License: MIT

Package to create simple&flexible DTOs - just magic :)

Create DTOs 'on the fly'

How to use

Create and extend class by AbstractDTO.. and this is all! You can define properties, but you don't have to.

Example class:

use Kosmcode\SimpleDto\AbstractDTO;

/**
 * @method bool getExistsField()
 * @method self setExistsField(bool $value)
 */
class ExampleDTO extends AbstractDTO
{
}

...

$exampleDTO = (new ExampleDTO())
    ->setExistsField(true);

$exampleDTO->getExistsField();
// true

$exampleDTO->isExistsField();
// true

$exampleDTO->hasNotExistsField();
// false

$exampleDTO->setNewFieldInFly('yes!');

$exampleDTO->hasNewFieldInFly();
// true

$exampleDTO->getNewFieldInFly();
// yes!

If the logic of any of magic methods doesn't suit you - just overwrite it ;)

abstract class \Kosmcode\SimpleDto\AbstractDTO {
    protected function getFieldValue(string $methodName): mixed
    
    protected function setFieldValue(string $methodName, mixed $value): self
    
    protected function isFieldValue(string $methodName): bool
    
    protected function hasFieldValue(string $methodName): bool
}