Skip to content

Commit 0f0ed42

Browse files
fancywebnicolas-grekas
authored andcommitted
[Uid] Add UuidFactory to create Ulid and Uuid from timestamps, namespaces and nodes
1 parent 802c096 commit 0f0ed42

File tree

5 files changed

+203
-2
lines changed

5 files changed

+203
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Deprecate `DoctrineTestHelper` and `TestRepositoryFactory`
88
* [BC BREAK] Remove `UuidV*Generator` classes
9+
* Add `UuidGenerator`
910

1011
5.2.0
1112
-----

IdGenerator/UlidGenerator.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,24 @@
1313

1414
use Doctrine\ORM\EntityManager;
1515
use Doctrine\ORM\Id\AbstractIdGenerator;
16+
use Symfony\Component\Uid\Factory\UlidFactory;
1617
use Symfony\Component\Uid\Ulid;
1718

1819
final class UlidGenerator extends AbstractIdGenerator
1920
{
21+
private $factory;
22+
23+
public function __construct(UlidFactory $factory = null)
24+
{
25+
$this->factory = $factory;
26+
}
27+
2028
public function generate(EntityManager $em, $entity): Ulid
2129
{
30+
if ($this->factory) {
31+
return $this->factory->create();
32+
}
33+
2234
return new Ulid();
2335
}
2436
}

IdGenerator/UuidGenerator.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\IdGenerator;
13+
14+
use Doctrine\ORM\EntityManager;
15+
use Doctrine\ORM\Id\AbstractIdGenerator;
16+
use Symfony\Component\Uid\Factory\UuidFactory;
17+
use Symfony\Component\Uid\Uuid;
18+
19+
final class UuidGenerator extends AbstractIdGenerator
20+
{
21+
private $protoFactory;
22+
private $factory;
23+
private $entityGetter;
24+
25+
public function __construct(UuidFactory $factory = null)
26+
{
27+
$this->protoFactory = $this->factory = $factory ?? new UuidFactory();
28+
}
29+
30+
public function generate(EntityManager $em, $entity): Uuid
31+
{
32+
if (null !== $this->entityGetter) {
33+
if (\is_callable([$entity, $this->entityGetter])) {
34+
return $this->factory->create($entity->{$this->entityGetter}());
35+
}
36+
37+
return $this->factory->create($entity->{$this->entityGetter});
38+
}
39+
40+
return $this->factory->create();
41+
}
42+
43+
/**
44+
* @param Uuid|string|null $namespace
45+
*
46+
* @return static
47+
*/
48+
public function nameBased(string $entityGetter, $namespace = null): self
49+
{
50+
$clone = clone $this;
51+
$clone->factory = $clone->protoFactory->nameBased($namespace);
52+
$clone->entityGetter = $entityGetter;
53+
54+
return $clone;
55+
}
56+
57+
/**
58+
* @return static
59+
*/
60+
public function randomBased(): self
61+
{
62+
$clone = clone $this;
63+
$clone->factory = $clone->protoFactory->randomBased();
64+
$clone->entityGetter = null;
65+
66+
return $clone;
67+
}
68+
69+
/**
70+
* @param Uuid|string|null $node
71+
*
72+
* @return static
73+
*/
74+
public function timeBased($node = null): self
75+
{
76+
$clone = clone $this;
77+
$clone->factory = $clone->protoFactory->timeBased($node);
78+
$clone->entityGetter = null;
79+
80+
return $clone;
81+
}
82+
}

Tests/IdGenerator/UlidGeneratorTest.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Doctrine\ORM\Mapping\Entity;
1515
use PHPUnit\Framework\TestCase;
1616
use Symfony\Bridge\Doctrine\IdGenerator\UlidGenerator;
17-
use Symfony\Component\Uid\AbstractUid;
17+
use Symfony\Component\Uid\Factory\UlidFactory;
1818
use Symfony\Component\Uid\Ulid;
1919

2020
class UlidGeneratorTest extends TestCase
@@ -25,8 +25,23 @@ public function testUlidCanBeGenerated()
2525
$generator = new UlidGenerator();
2626
$ulid = $generator->generate($em, new Entity());
2727

28-
$this->assertInstanceOf(AbstractUid::class, $ulid);
2928
$this->assertInstanceOf(Ulid::class, $ulid);
3029
$this->assertTrue(Ulid::isValid($ulid));
3130
}
31+
32+
/**
33+
* @requires function \Symfony\Component\Uid\Factory\UlidFactory::create
34+
*/
35+
public function testUlidFactory()
36+
{
37+
$ulid = new Ulid('00000000000000000000000000');
38+
$em = new EntityManager();
39+
$factory = $this->createMock(UlidFactory::class);
40+
$factory->expects($this->any())
41+
->method('create')
42+
->willReturn($ulid);
43+
$generator = new UlidGenerator($factory);
44+
45+
$this->assertSame($ulid, $generator->generate($em, new Entity()));
46+
}
3247
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\IdGenerator;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
16+
use Symfony\Component\Uid\Factory\UuidFactory;
17+
use Symfony\Component\Uid\NilUuid;
18+
use Symfony\Component\Uid\Uuid;
19+
use Symfony\Component\Uid\UuidV4;
20+
use Symfony\Component\Uid\UuidV6;
21+
22+
/**
23+
* @requires function \Symfony\Component\Uid\Factory\UuidFactory::create
24+
*/
25+
class UuidGeneratorTest extends TestCase
26+
{
27+
public function testUuidCanBeGenerated()
28+
{
29+
$em = new EntityManager();
30+
$generator = new UuidGenerator();
31+
$uuid = $generator->generate($em, new Entity());
32+
33+
$this->assertInstanceOf(Uuid::class, $uuid);
34+
}
35+
36+
public function testCustomUuidfactory()
37+
{
38+
$uuid = new NilUuid();
39+
$em = new EntityManager();
40+
$factory = $this->createMock(UuidFactory::class);
41+
$factory->expects($this->any())
42+
->method('create')
43+
->willReturn($uuid);
44+
$generator = new UuidGenerator($factory);
45+
46+
$this->assertSame($uuid, $generator->generate($em, new Entity()));
47+
}
48+
49+
public function testUuidfactory()
50+
{
51+
$em = new EntityManager();
52+
$generator = new UuidGenerator();
53+
$this->assertInstanceOf(UuidV6::class, $generator->generate($em, new Entity()));
54+
55+
$generator = $generator->randomBased();
56+
$this->assertInstanceOf(UuidV4::class, $generator->generate($em, new Entity()));
57+
58+
$generator = $generator->timeBased();
59+
$this->assertInstanceOf(UuidV6::class, $generator->generate($em, new Entity()));
60+
61+
$generator = $generator->nameBased('prop1', Uuid::NAMESPACE_OID);
62+
$this->assertEquals(Uuid::v5(new Uuid(Uuid::NAMESPACE_OID), '3'), $generator->generate($em, new Entity()));
63+
64+
$generator = $generator->nameBased('prop2', Uuid::NAMESPACE_OID);
65+
$this->assertEquals(Uuid::v5(new Uuid(Uuid::NAMESPACE_OID), '2'), $generator->generate($em, new Entity()));
66+
67+
$generator = $generator->nameBased('getProp4', Uuid::NAMESPACE_OID);
68+
$this->assertEquals(Uuid::v5(new Uuid(Uuid::NAMESPACE_OID), '4'), $generator->generate($em, new Entity()));
69+
70+
$factory = new UuidFactory(6, 6, 5, 5, null, Uuid::NAMESPACE_OID);
71+
$generator = new UuidGenerator($factory);
72+
$generator = $generator->nameBased('prop1');
73+
$this->assertEquals(Uuid::v5(new Uuid(Uuid::NAMESPACE_OID), '3'), $generator->generate($em, new Entity()));
74+
}
75+
}
76+
77+
class Entity
78+
{
79+
public $prop1 = 1;
80+
public $prop2 = 2;
81+
82+
public function prop1()
83+
{
84+
return 3;
85+
}
86+
87+
public function getProp4()
88+
{
89+
return 4;
90+
}
91+
}

0 commit comments

Comments
 (0)