DevCraft Документации
РазработкиDevCraft Dev Tools

Начало работы v1.0.0

Установка DevCraft Dev Tools, выбор базового класса, fluent builder и DTO.

Пакет devcraftclub/dev-tools — библиотека PHP 8.3 с двумя взаимодополняющими возможностями:

  1. Fluent mutation#[With] / #[WithItem], AbstractWith, runtime-методы with*.
  2. DTO mappingAbstractReflection, гидратация из массива, валидация атрибутами, toArray() / toJson().

Эти базовые классы рассчитаны на разные модели свойств и не предназначены для объединения через наследование.

Архитектура

Fluent API                         DTO Mapping
───────────                        ───────────
#[With] / #[WithItem]              public typed properties
        │                                  │
        ▼                                  ▼
 AbstractWith ──__call──► WithHandler   AbstractReflection


                                        ReflectionMapper


                                        PropertyValidator

                          Filter / Range / Regex / ArrayOf

Разделы документации

Быстрый пример AbstractWith

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

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

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

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

    public function page(): ?int
    {
        return $this->page;
    }

    public function tags(): array
    {
        return $this->tags;
    }

    public function labels(): array
    {
        return $this->labels;
    }
}

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

$query->page();   // 1
$query->tags();   // ['proxy']
$query->labels(); // ['status' => 'ready']

Быстрый пример AbstractReflection

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

Выбор базового класса

НужноНаследоватьСтиль свойств
Fluent builders / query objectsAbstractWithPrivate (или protected), non-static, non-readonly
API response / request DTOAbstractReflectionPublic, typed, hydratable

Не комбинируйте их через inheritance. Если нужны оба формы — composition или два отдельных класса.

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