PHP Classes

File: docs/adr/ADR-002-replace-metadata-arrays-with-typed-dtos.md

Recommend this page to a friend!
  Packages of ?????? ???????   Adaptive Entity Manager   docs/adr/ADR-002-replace-metadata-arrays-with-typed-dtos.md   Download  
File: docs/adr/ADR-002-replace-metadata-arrays-with-typed-dtos.md
Role: Auxiliary data
Content type: text/markdown
Description: Auxiliary data
Class: Adaptive Entity Manager
Entity manager to work with multiple data sources
Author: By
Last change:
Date: 3 months ago
Size: 14,740 bytes
 

 

Contents

Class file image Download

ADR-002: ?????? ????? ???????? ?????????? ?? ?????????????? ?????????

| ???? | ???????? | |-----------------|---------------------------------------------------------------| | ?????? | Proposed | | ???? | 2026-05-05 | | ????? | Ruslan Kabirov | | ?????????? ????? | Metadata/AbstractClassMetadata.php, ??? ?????? *Metadata |

????????

? ??????? ?????????? ?????????? ???????? ???????????? ??? ????????????? ?????? ? ???????? $metadata ??????, ???????????? AbstractClassMetadata:

class UserMetadata extends AbstractClassMetadata
{
    protected array $metadata = [
        'id' => [
            'type'      => FieldTypeEnum::Integer,
            'column'    => 'user_id',
            'primary'   => true,
        ],
        'fields' => [
            'email' => [
                'type'    => FieldTypeEnum::String,
                'column'  => 'user_email',
                'length'  => 255,
                'nullable' => false,
            ],
            'balance' => [
                'type'   => FieldTypeEnum::ValueObject,
                'class'  => Money::class,
                'from'   => 'fromCents',
                'to'     => 'toCents',
            ],
        ],
        'hasOne' => [
            'profile' => [
                'class'       => Profile::class,
                'fetchMode'   => FetchModeEnum::LAZY,
                'joinColumn'  => 'profile_id',
            ],
        ],
        'hasMany' => [
            'orders' => [
                'class'       => Order::class,
                'fetchMode'   => FetchModeEnum::LAZY,
                'mappedBy'    => 'user_id',
            ],
        ],
        'dataAdapterClass' => UserDataAdapter::class,
        'repositoryClass'  => UserRepository::class,
        'lifecycleCallbacks' => [
            'prePersist' => ['onPrePersist'],
        ],
    ];
}

???? ?????? ????? ??? ????????? ???????????:

  1. ??? type-safety ?? ?????? PHP. ???????? ? ????? (`'hassOne'` ?????? `'hasOne'`) ?? ???????? ?????? ?? ??? ???????? ??????????, ?? ? ???????? ? ??? ????????? ????????????, ? ?????????? ?????? ?? ??????????????.
  2. ??? ?????????????? ? IDE. ???????????, ????????? ????? Metadata-?????, ?? ???????? ????????? ? ????????? ???????. ?????????? ?????????? ?? ????????????? ?????? ? ?????????, ??? ?? ????????? ???????????? ????.
  3. ??? ????????? ?????????. `AbstractClassMetadata` ?? ?????????, ??? ???????????? ????? ????????????, ??? ???? ?????????, ??? `class` ?????? ??? `ValueObject`-?????. ?????? ??????????? ?????? ? ???????? ??? ?????????? ??? ???????????? ? ? ?? ?? ?????? ?????.
  4. ????????Concerns. ?????????????, ????, ??????????, ???????, ???????????, lifecycle ? ??? ? ????? ??????? ??? ??????? ??????????. ??? ????? ????? ????? ?????? ?????????? ??????????.
  5. ???????????? ??? ????????????. ???? ????????? ????????? ????? ????? ???? (????????, `created_at`, `updated_at`), ?????????? ??????????? ?? ? ?????? Metadata-??????. PHP-???????????? `$metadata` ???????? ??????? ? ???????????.

???????

???????? ????????????? ?????? ?? ????? ?????????????? value objects (DTO), ?????? ?? ??????? ????????? ???? ?????? ?????????? ????????.

????????? DTO

namespace Kabiroman\AEM\Metadata\Structure;

/
 * ???????? DTO ?????????? ????????.
 */
final class EntityMetadataStructure
{
    public function __construct(
        public readonly IdField $id,
        public readonly FieldMap $fields,
        public readonly ?AssociationMap $hasOne = null,
        public readonly ?AssociationMap $hasMany = null,
        public readonly ?string $dataAdapterClass = null,
        public readonly ?string $repositoryClass = null,
        public readonly ?LifecycleCallbackMap $lifecycleCallbacks = null,
    ) {}
}

final class IdField
{
    public function __construct(
        public readonly FieldTypeEnum $type,
        public readonly string $column,
        public readonly bool $primary = true,
    ) {}
}

final class FieldDefinition
{
    public function __construct(
        public readonly string $name,
        public readonly FieldTypeEnum $type,
        public readonly ?string $column = null,
        public readonly ?int $length = null,
        public readonly bool $nullable = false,
        public readonly ?string $class = null,       // ??? ValueObject
        public readonly ?string $from = null,         // ??? ValueObject
        public readonly ?string $to = null,           // ??? ValueObject
        public readonly ?array $values = null,        // ??? Boolean mapping
    ) {}
}

/
 * @extends Map<string, FieldDefinition>
 */
final class FieldMap extends Map {}

final class AssociationDefinition
{
    public function __construct(
        public readonly string $name,
        public readonly string $class,
        public readonly FetchModeEnum $fetchMode = FetchModeEnum::LAZY,
        public readonly ?string $joinColumn = null,
        public readonly ?string $mappedBy = null,
    ) {}
}

/
 * @extends Map<string, AssociationDefinition>
 */
final class AssociationMap extends Map {}

/
 * @extends Map<string, list<string|callable|array{string, string}>>
 */
final class LifecycleCallbackMap extends Map {}

????? ?????? ??????????? ??????????

class UserMetadata extends AbstractClassMetadata
{
    protected function defineMetadata(): EntityMetadataStructure
    {
        return new EntityMetadataStructure(
            id: new IdField(
                type: FieldTypeEnum::Integer,
                column: 'user_id',
            ),
            fields: new FieldMap([
                'email' => new FieldDefinition(
                    name: 'email',
                    type: FieldTypeEnum::String,
                    column: 'user_email',
                    length: 255,
                ),
                'balance' => new FieldDefinition(
                    name: 'balance',
                    type: FieldTypeEnum::ValueObject,
                    class: Money::class,
                    from: 'fromCents',
                    to: 'toCents',
                ),
            ]),
            hasOne: new AssociationMap([
                'profile' => new AssociationDefinition(
                    name: 'profile',
                    class: Profile::class,
                    fetchMode: FetchModeEnum::LAZY,
                    joinColumn: 'profile_id',
                ),
            ]),
            dataAdapterClass: UserDataAdapter::class,
            repositoryClass: UserRepository::class,
        );
    }
}

????????? ??? ????????

?????? DTO ?????????? ???? ????????? ? ????????????:

final class FieldDefinition
{
    public function __construct(
        public readonly string $name,
        public readonly FieldTypeEnum $type,
        // ...
    ) {
        if ($type === FieldTypeEnum::ValueObject && $this->class === null) {
            throw new MetadataDefinitionException(
                "Field '{$name}' has type ValueObject but no 'class' specified."
            );
        }
    }
}

???????????

?????????????

  • ???????? ??????? ?? ????? ???????? ??????????, ? ?? ? ????????. `EntityMetadataFactory` ??? ???????????? ????????? ???????? ???????? ????????? ??? ??????????.
  • ?????????????? ? IDE ? ??????????? ????? ??? ????????? ???? ? ?? ???? ??? ???????? `FieldDefinition`, `AssociationDefinition` ? ?.?.
  • ??????? ????????? ?????????? ?????? ? ????????, ?????????? ??????? `class` ??? ValueObject-???? ??????????? ? ???????????? DTO, ? ?? ????????? ??????????.
  • ?????????? ? ??????????? ????????? PHP 8 ?????? ????????? ?????????? ???????????????????.
  • ????????????? ? ?????????? ?????? ??????? ?????????? (????????, `indexes`, `uniqueConstraints`) ? ??? ????? DTO-????????, ? ?? ???? ? ???????, ??????? ????? ??????.

?????????????

  • Breaking change ? ??? ???????????? Metadata-?????? ?????? ???? ??????????. ??? ??????????? ??????? ???????????? ??????????.
  • Boilerplate ? DTO-?????? ???????????? ???????. ???????? ?????????? ??? ???????? ? 20 ?????? ????????? ?????? ????? ????.
  • ?????????????????? ??? ???????? ? ???????? ?????? DTO-???????? ?????? ??????? ???? ?????????. ?????? ?????????? ?????????? (`PhpArrayAdapter`), ??????? ??????? ?????????? ?????? ???????? ????? ?????? ????.
  • ?????????? ????? ??????? ? `EntityMetadataStructure`, `IdField`, `FieldDefinition`, `FieldMap`, `AssociationDefinition`, `AssociationMap`, `LifecycleCallbackMap`, `Map` ? 8 ????? ???????. ??? ???????? ??????????, ?? ??????????? ??????? API ??????????.

???? ????????

???? 1: ?????????? (minor release)

  1. ??????? namespace `Metadata\Structure` ? DTO-????????.
  2. ???????? ????? `defineMetadata(): EntityMetadataStructure` ? `AbstractClassMetadata` ? ????????? ???????????, ????????? `LogicException`.
  3. ????????? ????????? `$metadata` array ? `AbstractClassMetadata` ?????????, ????????????? ?? `defineMetadata()`, ? ???? ??? ? ?????????? ?????? ?????? (? deprecation warning ????? `trigger_error(E_USER_DEPRECATED)`).
  4. ???????? ?????????? ????????? `ArrayMetadataConverter`, ??????? ????????? ?????? ?????? ? `EntityMetadataStructure` ??? ?????????????.
  5. ???????? ???????????? ? ????? ???????????.

???? 2: ??????? (????????? minor release)

  1. ???????? PHPStan-???????, ????????????? ????????????? `$metadata` array.
  2. ???????? ??? ??????? ? ????? ?? ????? ?????????.
  3. ?????????, ??? >= 80% ?????? ?????????? DTO-?????????.

???? 3: ???????? (major release)

  1. ??????? ????????? `$metadata` array.
  2. ??????? `defineMetadata()` ??????????? ???????.
  3. ??????? `ArrayMetadataConverter`.

????????????

???????????? 1: ????????? ??????? ??? ????????? API

???????? MetadataValidator, ??????? ????????? ????????? ??????? ??? ???????? ??????????. ???????? ???????, ?? ?????????????? ? IDE ??-???????? ???????????.

?????: ?? ?????? ???????? ?????????? ? ?????????????. ????????? ? ??? ????????, ? ?? ???????.

???????????? 2: PHP Attributes (?????????)

???????????? PHP 8 attributes ??? ??????????? ?????????? ????? ?? ??????? ?????????:

#[Entity(adapter: UserDataAdapter::class)]
class User
{
    #[Id]
    #[Column(name: 'user_id', type: 'integer')]
    private int $id;

    #[Column(name: 'user_email', type: 'string', length: 255)]
    private string $email;
}

?????: ??? ???????????? ?????? ?????????????? ??????? ??????? ? ?? ???????????? ?????????/XML-???????. ????, ????????? ?????????? ? ???????? ???????, ??? ???????? ?????????? concerns ? hexagonal architecture.

???????????? 3: Fluent builder

protected function defineMetadata(): EntityMetadataStructure
{
    return EntityMetadataStructure::create()
        ->id('id', FieldTypeEnum::Integer, 'user_id')
        ->field('email', FieldTypeEnum::String, column: 'user_email', length: 255)
        ->field('balance', FieldTypeEnum::ValueObject, class: Money::class, from: 'fromCents', to: 'toCents')
        ->hasOne('profile', Profile::class, joinColumn: 'profile_id')
        ->build();
}

????: ?????? boilerplate, ?????????? ?????????. ?????: ???????? type-safety ?????????? (??? ?????? ? mixed), ??????? ???????????? ?? ????? ????????. ????? ?????????????: builder ?????????? DTO.