DevCraft Документации
English

Getting started v1.0.1

Overview of DevCraft Dev Tools, architecture, requirements, and first steps.

Package devcraftclub/dev-tools is a PHP 8.3 library with two complementary capabilities:

  1. Fluent mutation and accessors#[With] / #[WithItem], #[Getter] / #[Setter] (lombok-php), AbstractWith.
  2. DTO mappingAbstractReflection, array hydration, attribute validation, toArray() / toJson().

These base classes use different property models and are not meant to be combined through inheritance.

Architecture

Fluent API                              DTO Mapping
───────────                             ───────────
#[With] / #[WithItem]                   public typed properties
#[Getter] / #[Setter] (lombok-php)               │
        │                                        ▼
        ▼                                 AbstractReflection
 AbstractWith ──__call──► WithHandler            │
        │            (with* first)               ▼
        └──parent──► Lombok\Helper        ReflectionMapper
                     (get* / set* / is*)         │

                                          PropertyValidator

                            Filter / Range / Regex / ArrayOf

Documentation sections

Quick start

use Lombok\Getter;
use Devcraft\Abstracts\AbstractWith;
use Devcraft\Attributes\With;
use Devcraft\Attributes\WithItem;

#[Getter]
final class Query extends AbstractWith
{
    #[With]
    private ?int $page = null;

    #[With, WithItem('string')]
    private array $tags = [];

    #[WithItem('string', ['string', 'null'])]
    private array $labels = [];
}

$query = (new Query())
    ->withPage(1)
    ->withTagsItem('proxy')
    ->withLabelsItem('status', 'ready');

$query->getPage();  // 1
$query->getTags();  // ['proxy']
$query->getLabels(); // ['status' => 'ready']

DTO mapping:

use Devcraft\Abstracts\AbstractReflection;
use Devcraft\Attributes\ArrayOf;
use Devcraft\Attributes\Range;
use Devcraft\Attributes\Regex;

final class Address extends AbstractReflection
{
    public string $city;
}

final class Proxy extends AbstractReflection
{
    #[Regex('/^[0-9a-f-]{36}$/i')]
    public string $id;

    #[Range(min: 1, max: 65535)]
    public int $port;

    public Address $address;

    #[ArrayOf(Address::class)]
    public array $locations = [];
}

$proxy = Proxy::fromArray([
    'id' => '550e8400-e29b-41d4-a716-446655440000',
    'port' => '8080',
    'address' => ['city' => 'Berlin'],
    'locations' => [
        ['city' => 'Berlin'],
        ['city' => 'Paris'],
    ],
]);

$proxy->port;              // int 8080 (string coerced)
$proxy->address->city;     // 'Berlin'
$proxy->toArray();         // nested arrays
echo $proxy->toJson();     // pretty-printed JSON

Choosing a base class

NeedExtendProperty style
Fluent with* / query objects, plus get* / set*AbstractWithPrivate (or protected), non-static, non-readonly
API response / request DTOAbstractReflectionPublic, typed, hydratable

Do not combine them through inheritance. If you need both shapes, use composition or two separate classes.

Requirements

Next steps

На этой странице