Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
62.50% covered (warning)
62.50%
5 / 8
66.67% covered (warning)
66.67%
4 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Iri
62.50% covered (warning)
62.50%
5 / 8
66.67% covered (warning)
66.67%
4 / 6
13.27
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
3
 toNTriples
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 equals
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 localName
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 namespace
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 startsWith
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace LambdaTwelve\OneRecord\Rdf;
6
7use InvalidArgumentException;
8
9final readonly class Iri implements Term
10{
11    public function __construct(public string $value)
12    {
13        if ($value === '' || preg_match('/[\s<>"{}|\\\\^`]/', $value) === 1) {
14            throw new InvalidArgumentException(\sprintf('Not a valid IRI: "%s".', $value));
15        }
16    }
17
18    public function toNTriples(): string
19    {
20        return '<' . $this->value . '>';
21    }
22
23    public function equals(Term $other): bool
24    {
25        return $other instanceof self && $other->value === $this->value;
26    }
27
28    /**
29     * The part after the last '#' or '/': "Piece" for cargo:Piece. Used for
30     * generated constant names and compact JSON-LD keys.
31     */
32    public function localName(): string
33    {
34        $cut = max((int) strrpos($this->value, '#'), (int) strrpos($this->value, '/'));
35
36        return substr($this->value, $cut + 1);
37    }
38
39    public function namespace(): string
40    {
41        return substr($this->value, 0, \strlen($this->value) - \strlen($this->localName()));
42    }
43
44    public function startsWith(string $prefix): bool
45    {
46        return str_starts_with($this->value, $prefix);
47    }
48}