Skip to content

Commit

Permalink
Suggest alternative using Attributes to register value objects
Browse files Browse the repository at this point in the history
  • Loading branch information
yann-eugone committed Jul 8, 2024
1 parent e95fda0 commit abc91bc
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,38 @@ class Kernel extends BaseKernel
}
```

You can also leverage the power of attributes,
along with [`olvlvl/composer-attribute-collector`](https://github.com/olvlvl/composer-attribute-collector),
to automatically register all your value objects at once.

```php
use Doctrine\DBAL\Types\Type;
use Yokai\DoctrineValueObject\Doctrine\Types;

#[\Attribute(\Attribute::TARGET_CLASS)]
final readonly class AsValueObject
{
public function __construct(
public string $name,
) {
}
}

#[AsValueObject(MyValueObject::DOCTRINE_TYPE_NAME)]
final class MyValueObject implements \Yokai\DoctrineValueObject\StringValueObject
{
public const DOCTRINE_TYPE_NAME = 'doctrine_type_name';
}

$types = [];
foreach (Attributes::findTargetClasses(AsValueObject::class) as $target) {
/** @var AsValueObject $attribute */
$attribute = $target->attribute;
$types[$attribute->name] = $target->name;
}
(new Types($types))->register(Type::getTypeRegistry());
```


## Usage

Expand All @@ -65,9 +97,7 @@ use Doctrine\ORM\Mapping as ORM;

final class Entity
{
/**
* @ORM\Column(type="doctrine_type_name")
*/
#[ORM\Column(type="doctrine_type_name")]
public MyValueObject $status;
}
```
Expand Down

0 comments on commit abc91bc

Please sign in to comment.