yago-o / simple-dto
通过PHPDoc注释构建PHP DTO对象的简单类。
1.0.4
2019-01-10 14:01 UTC
Requires
- php: ~7.1
Requires (Dev)
- phpunit/phpunit: ^7.1.3
This package is auto-updated.
Last update: 2024-09-11 02:22:56 UTC
README
这是一个通过PHPDoc注释构建简单DTO对象的类。包括标量、数组和类型验证。
示例
class DtoClass extends SimpleDto { /** @var integer */ private $id; /** @var string|null */ private $name; /** @var int[] */ private $idList; /** @var TypeClass */ private $typeClass; /** @var float */ private $rating = 3.5; /** @var bool */ private $isAdmin = false; /** * @return int */ public function getId(): int { return $this->id; } /** * @return null|string */ public function getName() { return $this->name; } /** * @return int[] */ public function getIdList(): array { return $this->idList; } /** * @return TypeClass */ public function getTypeClass(): TypeClass { return $this->typeClass; } /** * @return float */ public function getRating(): float { return $this->rating; } /** * @return bool */ public function isAdmin(): bool { return $this->isAdmin; } } class TypeClass { } $dto = new DtoClass([ 'id' => 5, 'name' => 'Ivan', 'idList' => [1,2,3,4,5], 'typeClass' => new TypeClass(), 'isAdmin' => true, ]); $id = $dto->getId(); // 5 $name = $dto->getName(); // 'Ivan' $idList = $dto->getIdList(); // [1,2,3,4,5] $rating = $dto->getRating(); // 3.5 $typeClass = $dto->getTypeClass(); // TypeClass object $isAdmin = $dto->isAdmin(); // true
安装
composer require yago-o/simple-dto
注意事项
- 所有类型都可以是可数组的。
- 所有类属性都必须通过PHPDoc注释进行描述。
- 要使用属性上的自定义类,你必须写出包括命名空间在内的完整类名。