DTO - PHP Data Transfer Object
![Software License][ico-license]
![Coverage Status][ico-scrutinizer]
![Total Downloads][ico-downloads], (*1)
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., (*2)
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, (*3)
As well as forced boolean values of:
- True
and
- False, (*4)
Install
Via Composer, (*5)
``` bash
$ composer require anfischer/dto, (*6)
## 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)
``` php
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), (*7)
``` php
use Anfischer\Dto\Dto;, (*8)
class TypeHintedDataTransferObject extends Dto
{
protected $stringProperty;
protected $integerProperty;, (*9)
public function getPropertyType($property): string
{
switch ($property) {
case 'stringProperty':
return 'string';
case 'integerProperty':
return 'integer';
}
}
}, (*10)
$dto = new TypeHintedDataTransferObject;, (*11)
$dto->stringProperty = 'foo';
$dto->integerProperty = 1;, (*12)
// ERROR - throws InvalidTypeException since type has to be initialized as string
$dto->stringProperty = 1;, (*13)
// ERROR - throws InvalidTypeException since type has to be initialized as integer
$dto->integerProperty = 'foo';, (*14)
Finally, the DTO class allows for generating type hinted Data Transfer Objects with mixed types.
``` php
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., (*15)
Testing
bash
$ composer test
, (*16)
Contributing
Please see CONTRIBUTING and CODE_OF_CONDUCT for details., (*17)
Security
If you discover any security related issues, please email kontakt@season.dk instead of using the issue tracker., (*18)
Credits
License
The MIT License (MIT). Please see License File for more information., (*19)