pchapl/ids

Symfony bundle for doctrine custom types autoconfiguration

Maintainers

Details

github.com/pchapl/ids

Source

Issues

Installs: 28

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:symfony-bundle

2.0.0 2023-10-06 15:42 UTC

This package is auto-updated.

Last update: 2024-04-06 16:56:13 UTC


README

github workflow status

latest tag

license

code coverage

Usage

composer require pchapl/ids

Enable bundle in config/bundles.php:

    PChapl\DoctrineIdBundle\DoctrineIdBundle::class => ['all' => true],

Create Id classes like

namespace App\Data;

use PChapl\DoctrineIdBundle\Id\Base;

final class AccountId extends Base
{
}

Configure the bundle with types in the config/packages/doctrine_id.yaml:

doctrine_id:
    types:
        account_id: App\Data\AccountId
        user_id: App\Data\UserId

Then just use in entities

#[ORM\Entity]
class Account
{
    #[ORM\Column(type="account_id")]
    private AccountId $id;

    public function __construct(AccountId $id)
    {
        $this->id = $id;
    }

    public function getId(): AccountId
    {
        return $this->id;
    }

There are two ways to instantiate ids: implement Factory interface for Base::new method or just call Base::fromValue with generated string id:

/** @var \Hidehalo\Nanoid\Client $nanoidClient */

$factory = new class($nanoidClient) implements \PChapl\DoctrineIdBundle\Id\Factory {
    public function __construct(private \Hidehalo\Nanoid\Client $nano)
    {
    }

    public function generate(): string
    {
        return $this->nano->generateId();
    }
};

$account1 = new Account(AccountId::new($factory));
$account2 = new Account(AccountId::fromValue($nanoidClient->generateId()));
$account3 = new Account(AccountId::fromValue(\Ramsey\Uuid\Uuid::uuid4()->toString()));