skrepr / id-type
Symfony bundle for generating and validating ID types
Installs: 1 767
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 3
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: >=8.3.0
- doctrine/doctrine-bundle: ^2.5
- doctrine/orm: ^2.10 || ^3.0
- symfony/framework-bundle: ^6.4 || ^7.0
- symfony/uid: ^6.4 || ^7.0
Requires (Dev)
- phpunit/phpunit: ^10.2
- roave/security-advisories: dev-latest
- skrepr/dto-tester: ^0.2
- symfony/maker-bundle: ^1.48
- symfony/phpunit-bridge: ^6.4 || ^7.0
- symfony/var-dumper: ^6.4 || ^7.0
This package is auto-updated.
Last update: 2024-10-31 09:30:59 UTC
README
Skrepr ID Types
Symfony bundle for generating and validating ID types
Prerequisites
This version of the project requires:
- PHP 8.3+
- Symfony 6.4+
Installation
You can install the library through composer:
composer require skrepr/id-type
The bundle should be enabled by symfony/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 [--register] <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 if the --register
option is given, also the new type is added to config/packages/doctrine.yaml
.
Registering is not needed if you are using autoconfigure because of the service tag "skrepr.id-type" will automatically register the type to doctrine.
To use this new id in your entity (example:
<?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: UserId::TYPE)] public readonly UserId $id; #[ORM\Column(type: 'string')] public string $name; public function __construct(string $name) { $this->id = UserId::generate(); $this->name = $name; } }
See the example directory for the generated files by make:id-type.
To generate a new ID you can use the static generate
-function:
$newId = UserId::generate();
You can also feed a UUID or string to the constructor:
$userId = new UserId('00000000-0000-0000-0000-000000000000'); // or $userId = new UserId( \Symfony\Component\Uid\Uuid::v4() );
AutoConfigure
By default, the created ID's can be used with AutoConfigure from Symfony. To upgrade existing ID's to this system you have to do the following (in this example we use "UserId").
- Remove the 'user_id'-line from config/packages/doctrine.yaml
doctrine: dbal: types: user_id: App\Persistence\Doctrine\UserIdType
- Add the service tag 'skrepr.id-type' to App\Persistence\Doctrine\UserIdType
#[AutoconfigureTag('skrepr.id-type')] class UserIdType extends AbstractUuidType
- (optional) Remove the function App\Persistence\Doctrine\UserIdType::getName
- (optional) Add a constant to App\ValueObject\TestId (required if step 3 is done)
public const string TYPE = 'user_id';
Instead of step 2, you can also add the tag to all your custom types at once, because the compiler pass of skrepr/id-type will check for a subclass of AbstractUuidType:
# config/service.yaml App\Persistence\Doctrine\: resource: '../src/Persistence/Doctrine/' tags: - { name: 'skrepr.id-type' }