https://github.com/imbue/data-transfer-object
Using getter/setter methods gives the advantage of type hinting all data being set.
https://github.com/imbue/data-transfer-object
data-transfer-object dto php
Last synced: 11 months ago
JSON representation
Using getter/setter methods gives the advantage of type hinting all data being set.
- Host: GitHub
- URL: https://github.com/imbue/data-transfer-object
- Owner: imbue
- License: mit
- Created: 2018-12-30T20:28:08.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-05-22T07:04:01.000Z (about 3 years ago)
- Last Synced: 2025-07-07T13:42:10.527Z (12 months ago)
- Topics: data-transfer-object, dto, php
- Language: PHP
- Homepage:
- Size: 13.7 KB
- Stars: 3
- Watchers: 0
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Data Transfer Object
[![Latest Version][ico-version]][link-version]
[![Software License][ico-license]](LICENSE.md)
[![Latest Version on Packagist][ico-packagist]][link-packagist]
[![Total Downloads][ico-downloads]][link-downloads]
A variation / interpretation of the Data Transfer Object (DTO) design pattern (Distribution Pattern). A DTO is nothing more than an object that can hold some data. Most commonly it is used for transporting that data between system layers.
## Installation
You can install the package via composer
```bash
composer require imbue/data-transfer-object
```
## Usage
- [Introduction](#introduction)
- [Accessors & Mutators](#getters-and-setters)
- [Defining A Getter](#defining-a-getter)
- [Defining A Setter](#defining-a-setter)
- [Serializing Objects & Collections](#serializing-objects-and-collections)
- [Serializing To Arrays](#serializing-to-arrays)
- [Serializing To JSON](#serializing-to-json)
- [Collections](#collections)
- [Helpers](#helpers)
- [Example](#example)
Using getter/setter methods gives the advantage of type hinting all data being set. Thus any data object will be transparent and easy to use without the need of additional documentation, for example the API client you're writing.
To define a getter, simply create a `get...()` method on your data object
```php
class BookData extends DataTransferObject
{
protected $title;
protected $author;
/**
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* @return AuthorData
*/
public function getAuthor(): AuthorData
{
return $this->author;
}
}
```
To define a setters, simply create a `set...()` method on your data object
```php
class BookData extends DataTransferObject
{
protected $title;
protected $author;
/**
* @param nullable|string $title
*/
public function setTitle(?string $title)
{
return $this->title;
}
/**
* @param AuthorData $author
*/
public function setAuthor(AuthorData $author)
{
return $this->author;
}
}
```
## Serializing Objects & Collections
To convert a value object and its nested objects/collections to an array, you can use the `toArray` method. This method is recursive, so all attributes will be converted to arrays:
```php
return $dataObject->toArray();
```
To convert a value object and its nested objects/collections to a JSON object, you can use the `toJson` method. This method is recursive, so all attributes will be converted into JSON:
```php
return $dataObject->toJson();
```
In some cases you may want to have a collection of multiple data objects. By extending the provided `DataTransferObjectColletion` class, you can easily set up a group of DTOs
```php
$collection = new BooksCollection([
$bookOne,
$bookTwo,
$bookThree
]);
$collection->toArray();
```
#### Auto completion for collections
By overriding the `current()` method and setting the return value, you can enable type hinting.
```php
class BooksCollection extends DataTransferObjectCollection
{
public function current(): BookData
{
return parent::current();
}
}
```
```php
foreach ($booksCollection as $bookData) {
$bookData-> // type hinting
}
```
There are a few helper methods to provide some extra functionality
#### only()
```php
$dataObject
->only('title')
->toArray();
```
#### except()
```php
$dataObject
->except('title')
->toArray();
```
##### Immutability
These helpers are immutable, which means they wont affect the original data transfer object.
Using the data objects is made as simple as possible
```php
$book = new BookData();
$book->setTitle('Harry Potter: The Goblet of Fire');
$author = new Author();
// ....
$book->setAuthor($author);
```
## Security
If you discover any security related issues, please use the issue tracker.
## Testing
```bash
composer test
```
## Credits
- This package is based on the [data-transfer-object package](https://github.com/spatie/data-transfer-object) from [Spatie](https://github.com/spatie).
- `Arr` class contains functions copied from [Laravel's](https://github.com/laravel) Arr helper.
[ico-version]: https://img.shields.io/github/release/imbue/data-transfer-object.svg?style=flat-square
[ico-packagist]: https://img.shields.io/packagist/v/imbue/data-transfer-object.svg?style=flat-square
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/imbue/data-transfer-object.svg?style=flat-square
[link-version]: https://github.com/imbue/data-transfer-object/releases
[link-packagist]: https://packagist.org/packages/imbue/data-transfer-object
[link-downloads]: https://packagist.org/packages/imbue/data-transfer-object