eboreum / caster-doctrine-entity-formatter
专门用于 Doctrine 实体的格式化工具(见 doctrine/orm)。
1.0.0
2022-06-24 18:00 UTC
Requires
- php: ^8.1
- ext-mbstring: *
- doctrine/annotations: ^1.0
- doctrine/orm: ^2.0
- eboreum/caster: ^1.0
- eboreum/exceptional: ^1.0
Requires (Dev)
- phpstan/phpstan: ^1.4
- phpunit/phpunit: ^9.5
README
专门用于 Doctrine 实体的格式化工具(见 doctrine/orm),具体来说。
要求
"php": "^8.1", "ext-mbstring": "*", "doctrine/annotations": "^1.0", "doctrine/orm": "^2.0", "eboreum/caster": "^1.0", "eboreum/exceptional": "^1.0"
更多详细信息,请参阅 composer.json
文件。
安装
通过 Composer (https://packagist.org.cn/packages/eboreum/caster-doctrine-entity-formatter)
composer require eboreum/caster-doctrine-entity-formatter
通过 GitHub
git clone git@github.com:eboreum/caster-doctrine-entity-formatter.git
基本原理
该库是 eboreum/caster 和 doctrine/orm 之间的桥梁。
该库处理实体类的格式化,这些类要么
- 具有
Doctrine\ORM\Mapping\Entity
属性(通常写作#[ORM\Entity]
)。 - 具有
Doctrine\ORM\Mapping\Entity
注解(通常在类的 docblock 中写作@ORM\Entity
)。
ID 属性(即 Doctrine\ORM\Mapping\Id
或 @ORM\Id
,作为属性或注解)始终被识别并包含在内。可以使用 Eboreum\Caster\Contract\DebugIdentifierAttributeInterface
以及随后的属性 Eboreum\Caster\Attribute\DebugIdentifier
来提供有关实体的额外信息。后者在以下场景中特别有用(通常在调试期间)
- 实体尚未持久化,因此尚未收到 ID(例如,通过自动生成)。通过在其他属性上使用
#[DebugIdentifier]
,这可以提供关键调试信息。 - 某些其他非 ID 属性对于调试等目的至关重要。
- 有些人希望在格式化实体时增加其详细程度。
有关 Doctrine 注解和/或属性及其使用的帮助,请参阅
- 属性参考:https://www.doctrine-project.org/projects/doctrine-orm/en/2.11/reference/attributes-reference.html
- 注解参考:https://www.doctrine-project.org/projects/doctrine-orm/en/2.11/reference/annotations-reference.html#annotations-reference
示例
基本
代码
<?php declare(strict_types=1); namespace SomeCustomNamespace_9c95fb43; use Doctrine\ORM\Mapping as ORM; use Eboreum\Caster\Attribute\DebugIdentifier; use Eboreum\Caster\Caster; use Eboreum\Caster\Collection\Formatter\ObjectFormatterCollection; use Eboreum\Caster\Contract\DebugIdentifierAttributeInterface; use Eboreum\CasterDoctrineEntityFormatter\EntityFormatter; #[ORM\Entity] class User implements DebugIdentifierAttributeInterface { #[ORM\Id] public ?int $id = null; #[DebugIdentifier] public string $name = 'foo'; } $user = new User(); $caster = Caster::create()->withCustomObjectFormatterCollection(new ObjectFormatterCollection([ new EntityFormatter(), ])); echo $caster->cast($user) . "\n"; $user->id = 42; $user->name = 'bar'; echo "\n"; echo $caster->cast($user) . "\n";
输出
\SomeCustomNamespace_9c95fb43\User {$id = (null) null, $name = (string(3)) "foo"}
\SomeCustomNamespace_9c95fb43\User {$id = (int) 42, $name = (string(3)) "bar"}
仅在 ID 未设置时渲染 DebugIdentifier
“未设置”一词表示 ID 可以是未初始化或 null
。
为什么?
通常,当你有一个某个东西的 ID 时,其他信息可能会变成噪音。此功能允许你减少此类噪音。
代码
<?php declare(strict_types=1); namespace SomeCustomNamespace_fd813f94; use Doctrine\ORM\Mapping as ORM; use Eboreum\Caster\Attribute\DebugIdentifier; use Eboreum\Caster\Caster; use Eboreum\Caster\Collection\Formatter\ObjectFormatterCollection; use Eboreum\Caster\Contract\DebugIdentifierAttributeInterface; use Eboreum\CasterDoctrineEntityFormatter\EntityFormatter; #[ORM\Entity] class User implements DebugIdentifierAttributeInterface { #[ORM\Id] public ?int $id; #[DebugIdentifier] public string $name = 'foo'; } $user = new User(); $entityFormatter = new EntityFormatter(); $entityFormatter = $entityFormatter->withIsRenderingDebugIdentifierOnlyWhenIdHasNotBeenSet(true); $caster = Caster::create()->withCustomObjectFormatterCollection(new ObjectFormatterCollection([ $entityFormatter, ])); echo $caster->cast($user) . "\n"; $user->id = null; echo "\n"; echo $caster->cast($user) . "\n"; $user->id = 42; echo "\n"; echo $caster->cast($user) . "\n";
输出
\SomeCustomNamespace_fd813f94\User {$id = (uninitialized), $name = (string(3)) "foo"}
\SomeCustomNamespace_fd813f94\User {$id = (null) null, $name = (string(3)) "foo"}
\SomeCustomNamespace_fd813f94\User {$id = (int) 42}
许可 & 声明
请参阅 LICENSE
文件。基本上:使用此库风险自负。
贡献
我们希望你在 https://github.com/eboreum/caster-doctrine-entity-formatter 上创建一个工单和/或拉取请求,并在此处讨论功能或错误。
致谢
作者
- Kasper Søfren (kafoso)
电子邮件:soefritz@gmail.com
首页: https://github.com/kafoso - 卡尔森·约根森 (corex)
电子邮件: dev@corex.dk
首页: https://github.com/corex