jetcod/data-transport

Efficient PHP Data Transport with DTOs

v1.1.1 2024-01-07 21:03 UTC

This package is not auto-updated.

Last update: 2024-05-26 22:59:52 UTC


README

Actions Status

Latest Stable Version License

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.

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:

Function Description
__set(string $key, $val) sets the value of the specified attribute
__get(string $key) returns the value of the specified attribute
__isset(string $key) determines if the attribute has been set
__unset(string $key) unsets the specified attribute

License

This project is licensed under the MIT License - see the LICENSE file for details.