DownloadADR-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'],
],
];
}
???? ?????? ????? ??? ????????? ???????????:
-
??? type-safety ?? ?????? PHP. ???????? ? ????? (`'hassOne'` ?????? `'hasOne'`) ?? ???????? ?????? ?? ??? ???????? ??????????, ?? ? ???????? ? ??? ????????? ????????????, ? ?????????? ?????? ?? ??????????????.
-
??? ?????????????? ? IDE. ???????????, ????????? ????? Metadata-?????, ?? ???????? ????????? ? ????????? ???????. ?????????? ?????????? ?? ????????????? ?????? ? ?????????, ??? ?? ????????? ???????????? ????.
-
??? ????????? ?????????. `AbstractClassMetadata` ?? ?????????, ??? ???????????? ????? ????????????, ??? ???? ?????????, ??? `class` ?????? ??? `ValueObject`-?????. ?????? ??????????? ?????? ? ???????? ??? ?????????? ??? ???????????? ? ? ?? ?? ?????? ?????.
-
????????Concerns. ?????????????, ????, ??????????, ???????, ???????????, lifecycle ? ??? ? ????? ??????? ??? ??????? ??????????. ??? ????? ????? ????? ?????? ?????????? ??????????.
-
???????????? ??? ????????????. ???? ????????? ????????? ????? ????? ???? (????????, `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)
-
??????? namespace `Metadata\Structure` ? DTO-????????.
-
???????? ????? `defineMetadata(): EntityMetadataStructure` ? `AbstractClassMetadata` ? ????????? ???????????, ????????? `LogicException`.
-
????????? ????????? `$metadata` array ? `AbstractClassMetadata` ?????????, ????????????? ?? `defineMetadata()`, ? ???? ??? ? ?????????? ?????? ?????? (? deprecation warning ????? `trigger_error(E_USER_DEPRECATED)`).
-
???????? ?????????? ????????? `ArrayMetadataConverter`, ??????? ????????? ?????? ?????? ? `EntityMetadataStructure` ??? ?????????????.
-
???????? ???????????? ? ????? ???????????.
???? 2: ??????? (????????? minor release)
-
???????? PHPStan-???????, ????????????? ????????????? `$metadata` array.
-
???????? ??? ??????? ? ????? ?? ????? ?????????.
-
?????????, ??? >= 80% ?????? ?????????? DTO-?????????.
???? 3: ???????? (major release)
-
??????? ????????? `$metadata` array.
-
??????? `defineMetadata()` ??????????? ???????.
-
??????? `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.
|