Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
7 / 7 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| UuidIriMinter | |
100.00% |
7 / 7 |
|
100.00% |
2 / 2 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| mint | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace LambdaTwelve\OneRecord\Model; |
| 6 | |
| 7 | use LambdaTwelve\OneRecord\Rdf\Iri; |
| 8 | |
| 9 | /** |
| 10 | * Logistics object URIs as {base}/logistics-objects/{uuid}. |
| 11 | * |
| 12 | * With a seed, ids are UUID v5 of (seed, local key): the same graph published |
| 13 | * for the same seed (a shipment reference, say) always gets the same URIs, |
| 14 | * which is what makes republishing idempotent. Without a seed, ids are |
| 15 | * random UUID v4 from the injected byte source. |
| 16 | */ |
| 17 | final class UuidIriMinter implements IriMinter |
| 18 | { |
| 19 | /** @var callable(int): string */ |
| 20 | private $randomBytes; |
| 21 | |
| 22 | /** |
| 23 | * @param ?callable(int): string $randomBytes defaults to random_bytes |
| 24 | */ |
| 25 | public function __construct( |
| 26 | private readonly string $baseUrl, |
| 27 | private readonly ?string $seed = null, |
| 28 | ?callable $randomBytes = null, |
| 29 | ) { |
| 30 | if (!str_starts_with($baseUrl, 'http://') && !str_starts_with($baseUrl, 'https://')) { |
| 31 | throw new ModelException(\sprintf('The base URL must be absolute: "%s".', $baseUrl)); |
| 32 | } |
| 33 | $this->randomBytes = $randomBytes ?? static fn(int $length): string => random_bytes(max(1, $length)); |
| 34 | } |
| 35 | |
| 36 | public function mint(string $localKey, array $types): Iri |
| 37 | { |
| 38 | $id = $this->seed === null |
| 39 | ? Uuid::v4($this->randomBytes) |
| 40 | : Uuid::v5(Uuid::NAMESPACE_URL, $this->seed . '|' . $localKey); |
| 41 | |
| 42 | return new Iri(rtrim($this->baseUrl, '/') . '/logistics-objects/' . $id); |
| 43 | } |
| 44 | } |