Download the PHP package klkvsk/dto-generator without Composer

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

Latest Version on Packagist GitHub GitHub last commit GitHub Repo stars

Generate DTO classes with zero runtime dependencies

This package allows you to generate clean and independent DTO-classes (also called "value objects") by briefly declared schema in PHP-code.

Installation

You only need this package in development environment, since generated classes do not use anything from this library's code. That is why the preferred way is to include it as require-dev:

$ composer require --dev klkvsk/dto-generator

Requirements

Usage

1. Create schema-file

Schema is a regular PHP-file anywhere in your project. The file should return a Schema object from top level.

Example schema:

2. Generate files

Code generation is done with dto-gen command:

By default, generator searches for files named or ending with dto.schema.php, but you can provide schema files manually as arguments.

Generator will try to guess the right path for output by looking at autoload paths in composer.json. If it states PSR-4 mapping for "MyProject\\": "src/" then the file above will be placed in src/Data/Person.php.

To override this behaviour, you can specify outputDir directly:

Features

Back-compatibility

To generate code targeting some minimal version of PHP, use:

This option enables or disables some newer language features in the resulting code.

Enumerations

It is possible to generate not only DTOs, but related Enums too:

For PHP >= 8.0 native enums will be generated, for older versions a very similar class-based implementation is used.

Type system

DTOs serve a purpose to keep your data strongly typed. Types for schema are:

(where t is an alias to Klkvsk\DtoGenerator\Schema\Types)

Also, you can extend the abstract Type class for your needs.

Hydration and validation

DTOs can be created with a regular constructor or with <DTO>::create(array $data) method.

Method create accepts an associative array with data. That data is then filtered (if it needs some cleaning up beforehand) and imported (converted to proper types). After that, the method calls a default constructor, passing imported fields to it. The constructor get fields validated not only by type, but by a custom logic.

So, there are three stages of data manipulation, each can be described in schema with callables:

  1. filter prepares data to be imported
  2. importer casts value to correct type or instantiates a nested object
  3. validator checks that imported value meets specified criteria

filter and importer closures should return processed value. If a null is returned, further closures are not called.

validator returns true/false, and on false an InvalidArgumentException is thrown automatically. Also, you can throw your own exception and don't return anything.

Closures of type filter and validator are defined in schema:

Closure of type importer is predefined for all types except t\object:

You can specify a custom importer if you extend Type for your own needs.

Examples

See /example/ dir for the example schema and generated classes for different PHP versions.

License

The MIT License (MIT). Please see License File for more information.


All versions of dto-generator with dependencies

PHP Build Version
Package Version
Requires php Version >=8.1
nette/php-generator Version ^4.0
opis/closure Version ^3.6
psr/log Version ^3.0
ulrichsg/getopt-php Version ^4.0
splitbrain/php-cli Version ^1.2
spatie/php-cloneable Version ^1.0.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 klkvsk/dto-generator contains the following files

Loading the files please wait ....