kosmcode / simple-dto
1.0.0
2023-04-20 17:16 UTC
Requires
- php: 7.4|^8.0|^8.1|^8.2
Requires (Dev)
- phpunit/phpunit: >=7
This package is not auto-updated.
Last update: 2024-09-20 22:48:26 UTC
README
创建简单且灵活的DTO的包 - 真是魔法 :)
动态创建DTO
如何使用
通过 AbstractDTO
创建和扩展类... 就这么简单!你可以定义属性,但不需要。
示例类
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!
如果任何魔法方法的逻辑不适合你 - 只需重写它 ;)
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
}