https://github.com/codebooth/data-transfer-object
Data Transfer Object is all about how the data is represented
https://github.com/codebooth/data-transfer-object
data-transfer-object dto php
Last synced: 5 months ago
JSON representation
Data Transfer Object is all about how the data is represented
- Host: GitHub
- URL: https://github.com/codebooth/data-transfer-object
- Owner: codebooth
- License: mit
- Created: 2019-08-04T15:20:38.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-08-10T05:28:45.000Z (almost 7 years ago)
- Last Synced: 2024-10-31T17:12:13.615Z (over 1 year ago)
- Topics: data-transfer-object, dto, php
- Language: PHP
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Data Transfer Object
[](LICENSE.md)
[](https://travis-ci.org/codebooth/data-transfer-object)
[](https://scrutinizer-ci.com/g/codebooth/data-transfer-object)
[](https://codeclimate.com/github/codebooth/data-transfer-object)
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:
```bash
composer require codebooth/data-transfer-object
```
## Example
Let's consider the following scenario - you recieve following data from
a HTTP POST request:
```php
$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:
```php
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:
```php
$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](LICENSE.md).