anfischer/dto
Composer 安装命令:
composer require anfischer/dto
包简介
A PHP implementation of the Data Transfer Object pattern (https://martinfowler.com/eaaCatalog/dataTransferObject.html)
关键字:
README 文档
README
A PHP implementation of the Data Transfer Object pattern (https://martinfowler.com/eaaCatalog/dataTransferObject.html).
The Data Transfer Object allows for public properties with forced validation of their data types.
This implementation supports all of PHP's eight primitive data types:
- String
- Integer
- Double (or by PHP-implementation floating point numbers)
- Boolean
- Array
- Object
- Null
- Resource
As well as forced boolean values of:
- True
and - False
Install
Via Composer
$ composer require anfischer/dto
Usage
The DTO class can be used to generate generic Data Transfer Objects which does not enforce initializing type, but guaranties strict types for initialized properties (e.g. a property which is first initialized as string can not be changed to integer later)
use Anfischer\Dto\Dto; class GenericDataTransferObject extends Dto { protected $someProperty; protected $anotherProperty; } $dto = new GenericDataTransferObject; $dto->someProperty = 1; $dto->anotherProperty = null; // ERROR - throws InvalidTypeException since type is changed from integer to string $dto->someProperty = 'foo'; // OK - since it was first initialized as null $dto->anotherProperty = 'foo';
The DTO class also allows for generating type hinted Data Transfer Objects.
When forcing types properties can not be initialized with other types than defined for the
properties (e.g. a property which is defined as string can not be initialized as integer)
use Anfischer\Dto\Dto; class TypeHintedDataTransferObject extends Dto { protected $stringProperty; protected $integerProperty; public function getPropertyType($property): string { switch ($property) { case 'stringProperty': return 'string'; case 'integerProperty': return 'integer'; } } } $dto = new TypeHintedDataTransferObject; $dto->stringProperty = 'foo'; $dto->integerProperty = 1; // ERROR - throws InvalidTypeException since type has to be initialized as string $dto->stringProperty = 1; // ERROR - throws InvalidTypeException since type has to be initialized as integer $dto->integerProperty = 'foo';
Finally, the DTO class allows for generating type hinted Data Transfer Objects with mixed types.
use Anfischer\Dto\Dto; class TypeHintedDataTransferObject extends Dto { protected $mixedProperty; public function getPropertyType($property): string { switch ($property) { case 'mixedProperty': return 'string|integer|array'; } } } $dto = new MixedTypeHintedDataTransferObject; $dto->mixedProperty = 'foo'; $dto->mixedProperty = 1; $dto->mixedProperty = ['foo', 'bar', 'baz']; // ERROR - throws InvalidTypeException since type has to be either string, integer or array $dto->mixedProperty = 1.1; // ERROR - throws InvalidTypeException since type has to be either string, integer or array $dto->mixedProperty = false;
Change log
Please see CHANGELOG for more information on what has changed recently.
Testing
$ composer test
Contributing
Please see CONTRIBUTING and CODE_OF_CONDUCT for details.
Security
If you discover any security related issues, please email kontakt@season.dk instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
anfischer/dto 适用场景与选型建议
anfischer/dto 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 06 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「dto」 「anfischer」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 anfischer/dto 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 anfischer/dto 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 anfischer/dto 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP library for the MUMSYS project
A simple library that allows transform any kind of data to native php data or whatever
Data transfer objects
Laravel Foundation - a framework inspired by Lucid Foundation
Convert doctrine dbal query result to dto
Git manager and platform provider for Laravel — facades, typed DTOs, test fakes, and scoped repositories.
统计信息
- 总下载量: 9
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-06-25