nameBased example


        $em = (new \ReflectionClass(EntityManager::class))->newInstanceWithoutConstructor();
        $generator = new UuidGenerator();
        $this->assertInstanceOf(UuidV6::class$generator->generate($emnew Entity()));

        $generator = $generator->randomBased();
        $this->assertInstanceOf(UuidV4::class$generator->generate($emnew Entity()));

        $generator = $generator->timeBased();
        $this->assertInstanceOf(UuidV6::class$generator->generate($emnew Entity()));

        $generator = $generator->nameBased('prop1', Uuid::NAMESPACE_OID);
        $this->assertEquals(Uuid::v5(new Uuid(Uuid::NAMESPACE_OID), '3')$generator->generate($emnew Entity()));

        $generator = $generator->nameBased('prop2', Uuid::NAMESPACE_OID);
        $this->assertEquals(Uuid::v5(new Uuid(Uuid::NAMESPACE_OID), '2')$generator->generate($emnew Entity()));

        $generator = $generator->nameBased('getProp4', Uuid::NAMESPACE_OID);
        $this->assertEquals(Uuid::v5(new Uuid(Uuid::NAMESPACE_OID), '4')$generator->generate($emnew Entity()));

        $factory = new UuidFactory(6, 6, 5, 5, null, Uuid::NAMESPACE_OID);
        $generator = new UuidGenerator($factory);
        $generator = $generator->nameBased('prop1');
        
use Symfony\Component\Uid\Uuid;
use Symfony\Component\Uid\UuidV1;
use Symfony\Component\Uid\UuidV3;
use Symfony\Component\Uid\UuidV4;
use Symfony\Component\Uid\UuidV5;
use Symfony\Component\Uid\UuidV6;

final class UuidFactoryTest extends TestCase
{
    public function testCreateNamedDefaultVersion()
    {
        $this->assertInstanceOf(UuidV5::class(new UuidFactory())->nameBased('6f80c216-0492-4421-bd82-c10ab929ae84')->create('foo'));
        $this->assertInstanceOf(UuidV3::class(new UuidFactory(6, 6, 3))->nameBased('6f80c216-0492-4421-bd82-c10ab929ae84')->create('foo'));
    }

    public function testCreateNamed()
    {
        $uuidFactory = new UuidFactory();

        // Test custom namespace         $uuid1 = $uuidFactory->nameBased('6f80c216-0492-4421-bd82-c10ab929ae84')->create('foo');
        $this->assertInstanceOf(UuidV5::class$uuid1);
        $this->assertSame('d521ceb7-3e31-5954-b873-92992c697ab9', (string) $uuid1);

        


            return $this->factory->create($entity->{$this->entityGetter});
        }

        return $this->factory->create();
    }

    public function nameBased(string $entityGetter, Uuid|string $namespace = null)static
    {
        $clone = clone $this;
        $clone->factory = $clone->protoFactory->nameBased($namespace);
        $clone->entityGetter = $entityGetter;

        return $clone;
    }

    public function randomBased()static
    {
        $clone = clone $this;
        $clone->factory = $clone->protoFactory->randomBased();
        $clone->entityGetter = null;

        
try {
                        $namespace = Uuid::fromString($namespace);
                    } catch (\InvalidArgumentException $e) {
                        $io->error(sprintf('Invalid namespace "%s": %s', $namespace$e->getMessage()));

                        return 1;
                    }
                }

                $create = function D) use ($namespace$name): Uuid {
                    try {
                        $factory = $this->factory->nameBased($namespace);
                    } catch (\LogicException) {
                        throw new \InvalidArgumentException('Missing namespace: use the "--namespace" option or configure a default namespace in the underlying factory.');
                    }

                    return $factory->create($name);
                };
                break;

            case $random:
                $create = $this->factory->randomBased()->create(...);
                break;

            
Home | Imprint | This part of the site doesn't use cookies.