Download the PHP package tuxonice/transfer-objects without Composer
On this page you can find all versions of the php package tuxonice/transfer-objects. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tuxonice/transfer-objects
More information about tuxonice/transfer-objects
Files in tuxonice/transfer-objects
Package transfer-objects
Short Description Build data transfer objects from json definition
License MIT
Informations about the package transfer-objects
Bare simple data transfer object
In the ever-evolving landscape of software development, efficient and structured data communication is paramount. DTO package is designed to streamline and enhance the way data is transferred between different layers of your application, promoting clarity, maintainability, and robustness.
Whether you're building a RESTful API, a microservices architecture, or a traditional web application, Data Transfer Objects package empowers you to manage data flow with elegance and precision. Elevate your code quality and simplify your data handling processes with our intuitive and developer-friendly DTO solution.
Installation
You can install the package via composer:
Setup
The goal of this package is to create data transfer objects from json definitions as easy as possible.
-
In your project create a folder to hold on the definition files.
-
Create a folder to hold on the generated data transfer objects
-
Create a command to generate the DTO's. If you are using symfony console could be something like:
-
For Laravel projects:
- Define the dto through the definition file
This definition will generate the following transfer class:
Usage
Creating and set data
Get data
Creating from array
When creating from array its possible to use both camelCase or snake_case as array keys
Export to array
will return:
The toArray()
method has two parameters
isRecursive
when true will also export child transfer objects to an array
will return:
snakeCaseKeys
when true will also export array with snake_case keys (by default is camelCase)
will return:
Create definition file(s)
You can define one or more transfer objects definitions for each json file. Start by creating a json object that will contain your definitions:
and inside the transfers
array define your transfer:
Available fields
- Class
Field | Type | Required | Default | Description |
---|---|---|---|---|
name | string | yes | -- | The transfer object name. The result class name will be this name concatenated with "Transfer". E.g. CustomerTransfer |
properties | array | yes | -- | An array of objects with definition of each class property |
immutable | bool | no | false | Remove setters from the class. In this case the class name will end with "TransferImmutable" |
deprecationDescription | string | no | "" | If present and not empty, will add an annotation with @deprecated, to mark this class as deprecated |
- Class properties
Field | Type | Required | Default | Description |
---|---|---|---|---|
name | string | yes | -- | field name in camelCase |
type | string | yes | -- | The field type. Can be a native type (string, int, float, bool), or any other class. If the type ends with [], will mark the property as an array |
deprecationDescription | string | no | "" | If present and with a text, will add an annotation with @deprecated, to mark this field as deprecated |
nullable | bool | no | false | Set if the property can be null. Can not be set to true when the type is an array |
namespace | string | yes if the type is another class | -- | Namespace for the class in case the property type is another class (except another transfer object) |
singular | string | yes if the type is an array | -- | Singular form of the property if the type is an array |
Example of property definitions
-
integer
-
nullable string
-
another transfer object as property
-
DateTime property
-
Array of strings
-
Array of transfer objects
- Symfony Response
License
The MIT License (MIT). Please see License File for more information.