Download the PHP package codebot/phpdto without Composer

On this page you can find all versions of the php package codebot/phpdto. 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 phpdto

Latest Stable Version Build Status Total Downloads License

About

A CLI tool for PHP Data Transfer Objects generation.

This utility gives an ability to generate PHP 8 DTO classes based on json pattern.

Installation

Install the package via composer:

composer require --dev codebot/phpdto 0.3.*

Please consider that the minimum PHP version required by this package is 8.0.

Initialization

vendor/bin/phpdto init

The current working directory is the directory from where you are invoking phpdto command.

This command will initialize the phpdto and create phpdto.json configuration file and phpdto_patterns directory in your current working directory.

Configuration

phpdto.json configuration file contains following variables:

PHP_DTO_PATTERNS_DIR - the directory, where you must store json patterns for DTOs.

PHP_DTO_NAMESPACE - the namespace of generated DTO classes.

PHP_DTO_CLASS_POSTFIX - postfix of DTO classes, e.g. Item (no postfix), ItemDto (the postfix is "Dto").

These variables are stored as environment variables.

Usage

To generate DTO class you must create a pattern, which is a json file that contains information about the generated class.

DTO JSON Pattern

An example of DTO pattern:

class

A class name that will be combined with PHP_DTO_CLASS_POSTFIX specified in phpdto.json config file.

So, if the class name is item, and the class postfix config value is Dto, then the generated class name will be ItemDto.

namespace_postfix

A postfix of the generated DTO class namespace that will be combined with PHP_DTO_NAMESPACE specified in phpdto.json config file.

So, if the namespace postfix is \User, and the default DTO namespace is App\Dto, then the namespace of the generated class will be App\Dto\User.

You can leave namespace postfix empty.

props

This object contains information about DTO class properties and methods. Keys will be cast to class properties. Values contain information about getters return types.

"description" : "?string" - due to this pair $_description property will be added to DTO class with private ?string $_description property and getDescription(): ?string method, that expects return type "string" and allows null.

Generating DTO

Given you have already created pattern as json file named item.json in the phpdto_patterns folder.

Run vendor/bin/phpdto -f=item to have your DTO class generated. It will be stored under namespace specified in the phpdto.json config file combined with namespace postfix specified in your pattern.

Given you are generating DTO class from the pattern shown in "DTO JSON Pattern" section, then you will have following class generated.

How to use

There are 2 mapper methods:

static function mapArray(array $items, bool $shouldSerialize = false): array

static function mapSingle(array $item, bool $shouldSerialize = false): Dto|stdClass

Use mapArray when you need to map multidimensional array, otherwise use mapSingle.

Mapping example:

Single
Multidimensional

CONSIDER REFACTORING THE CONSTRUCTOR OF GENERATED DTO CLASS DEPENDING ON THE DATA STRUCTURE OF THE ARRAY YOU WANT TO MAP.

Sometimes you may want to have DTOs as objects that you could pass in AJAX response or whatever you need for.

The second parameter of the mapper methods is a flag that decides if the data should be serialized.

ItemDto::mapSingle( $itemData, true ) - this will return you the DTO as a serialized object:

Same is true for mapArray method.

DTO Faker

You can generate fake data for your DTOs easily using PhpDto\Services\DtoFaker class.

Now your item looks like this:

All the values are randomly generated, even the boolean value for isActive field.

You can fake multidimensional array via DtoFaker::fakeArray method.

In the example below we want to fake data for 10 items.

Second parameter of the Dto::fakeArray method is the count of generated items.

Dto::fakeSingle and Dto::fakeArray methods are using PHP Reflection API to get information about properties and getters.

Alternatively you can use Dto::fakeSingeFromPattern and Dto::fakeArrayFromPattern methods. You must pass them full path to your json pattern:
Dto::fakeArrayFromPattern('/full/path/to/pattern.json').

ToArray trait and toArray(): array method.

When you need to cast your DTO object to array, you can use the toArray method.

The output will be:

toArray method accepts two parameters: toSnakeCase and includeNulls:

If you want to keep array keys format to be same as class fields, you can pass toSnakeCase: false parameter: $arr = $dto->toArray(toSnakeCase: false);

If you want to include keys with null values, you can pass includeNulls: true parameter: $arr = $dto->toArray(includeNulls: true);

The output then will be:

So, now the isTrue key is camelCase.


All versions of phpdto with dependencies

PHP Build Version
Package Version
Requires php Version >=8.0
ext-json Version *
codebot/phpenum Version ^0.1.2
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 codebot/phpdto contains the following files

Loading the files please wait ....