https://github.com/nuxtifyts/php-dto
A Data objects library simplifies data transfer, ensuring structure, validation, and separation of concerns.
https://github.com/nuxtifyts/php-dto
data-deserialization data-serialization data-transfer-object php84
Last synced: 8 months ago
JSON representation
A Data objects library simplifies data transfer, ensuring structure, validation, and separation of concerns.
- Host: GitHub
- URL: https://github.com/nuxtifyts/php-dto
- Owner: nuxtifyts
- License: mit
- Created: 2024-12-22T21:24:20.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2025-02-04T18:39:15.000Z (8 months ago)
- Last Synced: 2025-02-04T19:37:26.997Z (8 months ago)
- Topics: data-deserialization, data-serialization, data-transfer-object, php84
- Language: PHP
- Homepage:
- Size: 420 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Support: docs/SupportedTypes.md
Awesome Lists containing this project
README
# PHP Pure Data objects



[](https://github.com/nuxtifyts/php-dto/actions/workflows/phpstan-tests.yml)
[](https://github.com/nuxtifyts/php-dto/actions/workflows/php-tests.yml)
[](https://packagist.org/packages/nuxtifyts/phpdto)This package enabled the creation of data objects which can be used in various ways.
Using modern PHP syntax, it allows you to hydrate data from arrays, objects, and other data sources.
As well as carrying out the data, type validation and serialize the data for any purpose.To create a `data` class, you will need to declare a `readonly` class that extends `Data` class.
Then you can define the properties of the class and their types.```php
use Nuxtifyts\PhpDto\Data;
use Nuxtifyts\PhpDto\Attributes\Property\Aliases;
use Nuxtifyts\PhpDto\Attributes\Property\Computed;final readonly class UserData extends Data
{
#[Computed]
public string $fullName;public function __construct(
public string $firstName,
#[Aliases('familyName')]
public string $lastName
) {
$this->fullName = "$this->firstName $this->lastName";
}
}
```You can then create an instance of the class from a mixed value. The DTO will then attempt to hydrate the object with the given data.
```php
$data = [
'firstName' => 'John',
'lastName' => 'Doe',
];$user = UserData::from($data);
```DTOs can also be serialized to an array:
```php
$user = new UserData('John', 'Doe');
$userData = $user->toArray();
// Or transform to a JSON string
$userData = $user->toJson();
```
Check out the [Quick start](https://github.com/nuxtifyts/php-dto/blob/main/docs/Quickstart.md) guide for more information.
### Note
This package was inspired from the [spatie/data-transfer-object](https://github.com/spatie/laravel-data) package.
The main thing that I tried to focus on when creating this package is to make it outside of Laravel ecosystem,
meaning: no dependency on [illuminate/support](https://github.com/illuminate/support).### Requirements
- PHP 8.4 or higher
- That's it!### Installation
You can install the package via composer:
```bash
composer require nuxtifyts/php-dto
```