anfischer / dto
A PHP implementation of the Data Transfer Object pattern (https://martinfowler.com/eaaCatalog/dataTransferObject.html)
Requires
- php: >=7.1
Requires (Dev)
- phpunit/phpunit: >=7.2.6
- squizlabs/php_codesniffer: ^2.3
This package is auto-updated.
Last update: 2024-10-27 17:28:36 UTC
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.