PHPackages                             codebot/phpdto - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [CLI &amp; Console](/categories/cli)
4. /
5. codebot/phpdto

ActiveLibrary[CLI &amp; Console](/categories/cli)

codebot/phpdto
==============

CLI tool for PHP data transfer objects generation.

0.3.0(2y ago)231.2k↓30.3%1[10 issues](https://github.com/hakobyansen/phpdto/issues)MITPHPPHP &gt;=8.0CI failing

Since Aug 20Pushed 2y agoCompare

[ Source](https://github.com/hakobyansen/phpdto)[ Packagist](https://packagist.org/packages/codebot/phpdto)[ RSS](/packages/codebot-phpdto/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (11)Used By (0)

[![Latest Stable Version](https://camo.githubusercontent.com/0bb0dc89b9303df5c07b8d71f440229674d986f33239f7ff212be92395e17867/68747470733a2f2f706f7365722e707567782e6f72672f636f6465626f742f70687064746f2f762f737461626c65)](https://packagist.org/packages/codebot/phpdto)[![Build Status](https://camo.githubusercontent.com/b5e0c0540bde67af008621e68e844631d815324133d7c1d54ee080e43410114a/68747470733a2f2f7472617669732d63692e636f6d2f633064336230742f70687064746f2e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/c0d3b0t/phpdto)[![Total Downloads](https://camo.githubusercontent.com/8d25d46000b08967aa6addf4fdcf25f60dbfc7d2380a2dcbc0aa3ead9f96d57a/68747470733a2f2f706f7365722e707567782e6f72672f636f6465626f742f70687064746f2f646f776e6c6f616473)](https://packagist.org/packages/codebot/phpdto)[![License](https://camo.githubusercontent.com/f41a9ea1f2b8833b2aaef9858bedc6339c2334d82103f640b7ddff0657c527a4/68747470733a2f2f706f7365722e707567782e6f72672f636f6465626f742f70687064746f2f6c6963656e7365)](https://packagist.org/packages/codebot/phpdto)

About
-----

[](#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
------------

[](#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
--------------

[](#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
-------------

[](#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
-----

[](#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

[](#dto-json-pattern)

An example of DTO pattern:

```
{
  "class": "item",
  "namespace_postfix": "",
  "props": {
    "id": "int",
    "count": "?int",
    "name": "string",
    "description": "?string",
    "is_active": "bool"
  }
}
```

**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

[](#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.

```
