2017-25 © Pedro Peláez
 

library dto

A PHP implementation of the Data Transfer Object pattern (https://martinfowler.com/eaaCatalog/dataTransferObject.html)

image

anfischer/dto

A PHP implementation of the Data Transfer Object pattern (https://martinfowler.com/eaaCatalog/dataTransferObject.html)

  • Tuesday, June 26, 2018
  • by anfischer
  • Repository
  • 1 Watchers
  • 1 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

DTO - PHP Data Transfer Object

Latest Version on Packagist ![Software License][ico-license] Build Status ![Coverage Status][ico-scrutinizer] Quality Score ![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)

The Versions

26/06 2018

dev-master

9999999-dev https://github.com/anfischer/dto

A PHP implementation of the Data Transfer Object pattern (https://martinfowler.com/eaaCatalog/dataTransferObject.html)

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

dto anfischer

25/06 2018

v0.1.0

0.1.0.0 https://github.com/anfischer/dto

A PHP implementation of the Data Transfer Object pattern (https://martinfowler.com/eaaCatalog/dataTransferObject.html)

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

dto anfischer