Download the PHP package adventure-tech/data-transfer-object without Composer

On this page you can find all versions of the php package adventure-tech/data-transfer-object. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package data-transfer-object

Data transfer object

This is a strict and opinionated implementation of data transfer objects. One of the goals of this package is to create DTO's that are as predictable as PHP will allow.

Where other implementations are very general purpose, this package comes with a lot of rules and conventions on how to create and instantiate DTO's. It follows a sort of 'Create with care, use with ease' sort of mentality.

This package is created for Laravel, but can easily be modified to work in other frameworks, or with vanilla PHP.

Requirements

Installation

Install the package via composer:

Usage

Instantiating a DTO

Instantiating a new DTO is very simple. There is one, and only one way of doing it, namely by calling the static method from() on the DTO class like this:

The method accepts one argument which is the source array or object that your DTO will map it's properties from. The argument can be one of three types:

Creating new DTO classes

Here is a basic example of a new DTO called User:

This is the simplest, but also the most strict definition of a DTO. There are a few rules and assumptions you need to be aware of when creating a new DTO. Note that the some of the default behaviour can be modified by using one of the provided attributes

Rules, conventions and how to modify them

Property type declaration

Every property must be declared with a type. Always.

Visibility modifiers

Every property you want to be auto assigned from the source needs to be public.

Nullable properties

You can make a property nullable, e.g ?string, but the DTO will still expect the corresponding property on the source to be present, even though it's value is null.

By using the attribute #[Optional] on a property, you can modify this behaviour. The DTO will no longer care if the property is instantiated, present on the source or instantiated with a null value (given the field is declared nullable).

Naming properties

The DTO will expect the corresponding class property name or array key on the source to be named exactly the same as the DTO property.

By using the attribute #[MapFrom] you can override which property name or key the DTO will be looking for on the source. See example below.

Default values

By using the attribute #[DefaultValue] you can define a default value for the property.

If the DTO property is declared nullable, the default value will be assigned if the source property is null.

If the DTO property is non nullable, the default value will work as a fallback if the corresponding source property is not present, or it's value is null.

Handling dates

If your DTO property is declared with the type Carbon, the DTO will automatically cast the source date/datetime value to Carbon before assigning it.

Boolean properties

If your database uses int/tinyint to represent boolean values, they will automatically be cast to bool as long as the DTO property is declared with the bool type.

Mapping from JSON

The package uses the JsonMapper library to work with database json fields. The library allows us to map entire json structures to a nested DTO structure.

The way this works is that you need to specify a root class on your DTO and annotate it with the #[JsonMapper] attribute. This root class should not be an instance of the DataTransferObject, but instead be a POPO (Plain Old Php Object). Each sub class matching the JSON structure should also be a POPO with docblock annotations according to the library api. Visit the library's GitHub page to read more on how use nested structures.

Mapping from Enum

If your database field is of the type enum, you can create a corresponding enum class in you application and use it as type for your DTO property, and the value will automatically be cast to the correct enum value. It is important to note that your enum class must be backed by int or string to make this work. Read more about backed enums here.

Immutable fields

Sadly PHP doesn't support immutable objects (yet). The readonly declaration is not supported by this package. The reason is that the parent DataTransferObject relies on Reflection to assign values to whichever child class being instantiated. PHP does not allow a parent class to assign readonly properties to it's children.

PhpStorm comes with the attribute #[Immutable] built into the IDE. You have the opportunity to use this attribute on your DTO properties, but all it accomplishes is a warning in the IDE when you are about to assign a value to an immutable property. It does not prevent you from doing so.

Triggers

The #[Trigger] attribute provides a way do initialize properties after other properties mapped from source have been initialized. Typical use of this attribute is that you want to do some computation on the DTO data, and store it as a property.

The attribute requires one argument, namely the method that will be triggered to initialize the property. Let's take a look at an example where we want to concatenate the user's name and store it as a new property.

Laravel Artisan

The package comes with an artisan command to create new DTO classes:

Examples

Laravel example

Define the DTO:

Use the DTO:


All versions of data-transfer-object with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
laravel/framework Version ^10.0
nesbot/carbon Version ^2.0
netresearch/jsonmapper Version ^4.1
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package adventure-tech/data-transfer-object contains the following files

Loading the files please wait ....