This package allows you to generate clean and independent DTO-classes (also called "value objects") by briefly declared schema in PHP-code.
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
Schema is a regular PHP-file anywhere in your project.
The file should return a Schema object from top level.
Example schema:
<?php
use KlkvskDtoGeneratorSchema as dto;
use KlkvskDtoGeneratorSchemaTypes as t;
return dtoschema(
namespace: 'MyProjectData',
objects: [
new dtoobject(
name: 'Person',
fields: [
dtofield('name', tstring(), required: true),
dtofield('age', tint(), required: true),
]
),
]
);Code generation is done with dto-gen command:
$ ./vendor/bin/dto-gen [schema-file]
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:
dtoschema(namespace: "MyProject\Data", outputDir: "src/generated/", ...);To generate code targeting some minimal version of PHP, use:
./vendor/bin/dto-gen --target 7.4
This option enables or disables some newer language features in the resulting code.
It is possible to generate not only DTOs, but related Enums too:
dtoschema(
objects: [
dtoenum(
name: 'PostStatus'
cases: [ 'draft', 'published', 'deleted' ]
),
dtoobject(
name: 'Post',
fields: [
dtofield('status', tenum('PostStatus'), required: true),
...
]
)
]
)For PHP >= 8.0 native enums will be generated, for older versions a very similar class-based implementation is used.
DTOs serve a purpose to keep your data strongly typed. Types for schema are:
tint, tbool, tstring, tfloat - basic scalar typestenum, tobject - for referencing other DTO objectstdate - using DateTimeImmutabletexternal - for referencing any other non-DTO classestlist_(T) - wraps around type T to declare T[]tmixed - if you really don't know(where t is an alias to KlkvskDtoGeneratorSchemaTypes)
Also, you can extend the abstract Type class for your needs.
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:
filter prepares data to be importedimporter casts value to correct type or instantiates a nested objectvalidator checks that imported value meets specified criteriafilter 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:
dtofield('age', tint(),
filters: [ fn ($x) => preg_replace('/[^0-9]+/', $x) ],
validators: [ fn ($x) => $x > 0 && $x < 100 ]
)
Closure of type importer is predefined for all types except tobject:
dtofield('file', texternal(SplFileInfo::class, fn ($x) => new SplFileInfo($x))
You can specify a custom importer if you extend Type for your own needs.
See /example/ dir for the example schema and generated classes for different PHP versions.
The MIT License (MIT). Please see License File for more information.