rexlabsio / data-transfer-object

Conveniently map raw array data to strongly typed objects.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

data-transfer-object

Build Status

Overview

Use DataTransferObjects to map raw array data to strongly typed objects. The boundaries of many php applications send and receive associative arrays with no type safety. Adding typed objects in key locations adds stability and can expose faulty assumptions about the shape of your data.

When dealing with data from user input, it often pays to know if a value has been defined as null or if it wasn't defined at all. DataTransferObjects make defined and undefined "a thing" for php.

DataTransferObject uses flags to change the default behaviour of your types making them useful for a variety of use cases.

For new applications and newer versions of PHP we strongly recommend the use of plain php objects with read only properties where possible in libraries and Laravel Data, when appropriate, in apps.

Install

Via Composer

composer require rexlabs/data-transfer-object

Usage

Define a DTO class using the phpdoc to specify the allowed types for properties.

use Rexlabs\DataTransferObject\DataTransferObject;

/**
 * @property string $first_name
 * @property null|string $last_name
 * @property string $email
 * @property null|int $age
 * @property null|UserDto $parent
 * @property UserDto[] $children
 */
class UserDto extends DataTransferObject
{
}

$rawData = [
    'first_name' => 'James',
    'last_name' => 'Kirk',
    'email' => 'jim@starfleet.ufp',
    50,
];

$kirk = UserDto::make($rawData);

Guide

Data transfer objects are useful in many contexts and have additional features for convenience and refactoring.

Check the guide for details.

Upgrading from an older version?

Follow the Upgrade Guide.

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email lachlan.krautz@rexsoftware.com.au instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

Conveniently map raw array data to strongly typed objects.

License:MIT License


Languages

Language:PHP 100.0%