codebooth/data-transfer-object

Data Transfer Object is all about how the data is represented

dev-master 2019-08-10 05:28 UTC

This package is auto-updated.

Last update: 2024-05-10 15:53:53 UTC


README

Software License Build Status SensioLabsInsight CodeClimate

A Data Transfer Object (DTO) is an object used to pass data between different layers in your application. It holds no business data, but only the minimum required data to transfer between layers or applications.

The DTOs can help to put your unstructured arrays into a clean structure:

  • You clearly see what fields are available, and what type that field is (using IDE you get fully type-hinting/autocomplete to be super quick here).
  • You get a clear exception/error on fields that are either unexpected or missing where you want this kind of information – early on instead of somewhere down the line.

Getting Started

You can install the package via composer:

composer require codebooth/data-transfer-object

Example

Let's consider the following scenario - you recieve following data from a HTTP POST request:

$input = [
    'url' => 'https://github.com',
    'number' => 1234,
    'state' => 'open',
    'title' => 'new-feature',
];

A data transfer object of the above entity would look something as shown below:

use CodeBooth\DataTransferObject\DataTransferObject;

class ExampleObject extends DataTransferObject
{
    public $url;
    
    public $number;
    
    public $state;
    
    public $title;
}

Now the data transfer object could be constructed like this:

$object = new ExampleObject($input)

echo $object->url; // outputs 'https://github.com'
echo $object->number; // outputs 1234
echo $object->state; // outputs 'open'

License

Copyright (c) 2019 CodeBooth. Released under the MIT License.