skrepr/id-type

Symfony bundle for generating and validating ID types

Installs: 417

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 3

Forks: 1

Open Issues: 0

Type:symfony-bundle

1.2 2024-02-13 07:23 UTC

This package is not auto-updated.

Last update: 2024-04-23 11:38:04 UTC


README

skrepr_logo

Skrepr ID Types

Releases LICENSE Issues Stars

Symfony bundle for generating and validating ID types

Prerequisites

This version of the project requires:

  • PHP 8.2+
  • Symfony 6.0+

Installation

You can install the library through composer:

$ composer require skrepr/id-type

The bundle should be enabled by syfmony/flex but if not:

// config/bundles.php

<?php

return [
    Skrepr\IdType\SkreprIdTypeBundle::class => ['all' => true],
];

Usage

To generate an UuidType:

bin/console make:id-type <id_name>

Where id_name is something like "user_id".

With this maker command, two files are generated (src/ValueObject/UserId.php and src/Persistence/Doctrine/UserIdType.php) and also the new type is added to config/packages/doctrine.yaml.

To use this new id in your entity:

<?php

declare(strict_types=1);

namespace App\Entity;

use App\ValueObject\UserId;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class User
{
    #[ORM\Id]
    #[ORM\Column(type: 'user_id')]
    public readonly UserId $id;
    
    #[ORM\Column(type: 'string')]
    public string $name;
    
    public function __construct(string $name)
    {
        $this->id = UserId::generate();
        $this->name = $name;
    }
}