jetcod / data-transport
Efficient PHP Data Transport with DTOs
Installs: 1 103
Dependents: 1
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^7.4|^8.0
Requires (Dev)
- fakerphp/faker: ^1.20
- friendsofphp/php-cs-fixer: ^3.14
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^9.6
README
Overview
Data Transport is a PHP package that provides a simple and efficient way to transport data within your application. With Data Transport, you can easily define and manage your data structures, ensuring that your application's data is well-organized and easy to work with. Get started today and streamline your data transport process with Data Transport!
Check out our documentation for more details.
Installation
To install jetcod/data-transport
, you can use Composer, the dependency manager for PHP. Run the following command in your terminal:
composer require jetcod/data-transport
Usage
It is straightforward to employ jetcod/data-transport
. To begin, create a customized data object class that extends Jetcod\DataTransport\AbstractDTO
.
<?php namespace App\DTO; use Jetcod\DataTransport\AbstractDTO; class Student extends AbstractDTO { }
Next, construct your class and inject your array of data into it.
<?php $data = [ 'name' => 'John Doe', 'email' => 'john.doe@example.com', ]; $dto = new \App\DTO\Student($data);
Alternatively, you can assign values to individual class attributes:
<?php $dto = new \App\DTO\Student(); $dto->name = "John Doe"; $dto->email = 'john.doe@example.com';
In addition to the traditional method of creating a class, you can also utilize the convenient make function to create instances of the class. The make function simplifies the process and provides an alternative way to initialize objects.
<?php $dto = \App\DTO\Student::make($data);
By utilizing the data transfer object, one can prevent the occurrence of exceptions when attempting to access an undefined class attribute. In such cases, the data transfer object will consistently return null
.
$dto = new \App\DTO\Student(); var_dump($dto->name); // Returns null
Additionally, it offers the opportunity to specify custom data types.
Initialization Hook
Starting from version 1.1.2, an init()
method can be defined within any derived class for additional initialization logic. This method, if defined, will be automatically called after the attributes are assigned in the constructor. You can use this method to perform any necessary setup or initialization tasks.
class Student extends AbstractDTO { protected function init() { // Custom initialization logic } }
Functions
The following functions are available in the DTO class:
__construct(?array $attributes = [])
The constructor function creates a new instance of the DTO class and initializes it with the given data array.
has(string $key): bool
The has() function checks if the specified key exists in the DTO class data and returns a boolean value.
It works the same as isset()
php built-in function. For example if you have object:
$student = new Student([ 'first_name' => 'John', 'last_name' => 'Doe', 'email' => 'john.doe@example.com' ]);
then both functions will produce an identical outcome:
isset($student->email); // Returns true isset($student->phone); // Returns false
or
$student->has('email'); // Returns true $student->has('phone'); // Returns false
toArray()
The toArray() function returns an array representation of the DTO class data.
toJson(int $options = 0)
The toJson() function returns a JSON representation of the DTO class data.
Other than above functions, it has the following magic functions:
License
This project is licensed under the MIT License - see the LICENSE file for details.