Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
87.50% |
7 / 8 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
| LocalRef | |
87.50% |
7 / 8 |
|
80.00% |
4 / 5 |
8.12 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
3 | |||
| to | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| iri | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isLocal | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| keyOf | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace LambdaTwelve\OneRecord\Model; |
| 6 | |
| 7 | use LambdaTwelve\OneRecord\Rdf\Iri; |
| 8 | use LambdaTwelve\OneRecord\Spec\Namespaces; |
| 9 | |
| 10 | /** |
| 11 | * A reference to another object of the same graph by its local key, written |
| 12 | * as the IRI `local:<key>` until the graph is resolved and real URIs minted. |
| 13 | */ |
| 14 | final readonly class LocalRef |
| 15 | { |
| 16 | public function __construct(public string $key) |
| 17 | { |
| 18 | if ($key === '' || preg_match('/^[A-Za-z0-9][A-Za-z0-9_.-]*$/', $key) !== 1) { |
| 19 | throw new ModelException(\sprintf('"%s" is not a valid local key (letters, digits, "_", "-", ".").', $key)); |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | public static function to(string $key): self |
| 24 | { |
| 25 | return new self($key); |
| 26 | } |
| 27 | |
| 28 | public function iri(): Iri |
| 29 | { |
| 30 | return new Iri(Namespaces::LOCAL . $this->key); |
| 31 | } |
| 32 | |
| 33 | public static function isLocal(Iri $iri): bool |
| 34 | { |
| 35 | return str_starts_with($iri->value, Namespaces::LOCAL); |
| 36 | } |
| 37 | |
| 38 | public static function keyOf(Iri $iri): string |
| 39 | { |
| 40 | if (!self::isLocal($iri)) { |
| 41 | throw new ModelException(\sprintf('"%s" is not a local reference.', $iri->value)); |
| 42 | } |
| 43 | |
| 44 | return substr($iri->value, \strlen(Namespaces::LOCAL)); |
| 45 | } |
| 46 | } |