-
Notifications
You must be signed in to change notification settings - Fork 1
/
FreezesUuidTrait.php
71 lines (58 loc) · 1.82 KB
/
FreezesUuidTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
declare(strict_types=1);
namespace Xthiago\ValueObject\Id\Testing;
use ArrayIterator;
use Iterator;
use Ramsey\Uuid\Uuid;
use \Ramsey\Uuid\UuidInterface;
use Ramsey\Uuid\UuidFactory;
use RuntimeException;
trait FreezesUuidTrait
{
private function freezeUuidV4WithFixedValue(string $uuid): void
{
$factory = new class($uuid) extends UuidFactory {
/** @var string */
private $uuid;
public function __construct(string $uuid)
{
$this->uuid = $uuid;
parent::__construct();
}
public function uuid4(): UuidInterface
{
return Uuid::fromString($this->uuid);
}
};
Uuid::setFactory($factory);
}
private function freezeUuidV4WithKnownSequence(string ...$uuids): void
{
$sequence = new ArrayIterator($uuids);
$factory = new class($sequence) extends UuidFactory {
/** @var Iterator */
private $uuidSequence;
public function __construct(Iterator $uuidSequence)
{
$this->uuidSequence = $uuidSequence;
parent::__construct();
}
public function uuid4(): UuidInterface
{
if (false === $this->uuidSequence->valid()) {
throw new RuntimeException(
'The UUID sequence are over. Maybe more UUIDs were used than initially configured.'
);
}
$uuid = $this->uuidSequence->current();
$this->uuidSequence->next();
return Uuid::fromString($uuid);
}
};
Uuid::setFactory($factory);
}
private function unfreezeUuid(): void
{
Uuid::setFactory(new UuidFactory());
}
}