-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Link.php
93 lines (81 loc) · 2.3 KB
/
Link.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
declare(strict_types=1);
namespace SonsOfPHP\Component\Link;
use InvalidArgumentException;
use Psr\Link\LinkInterface;
/**
* @author Joshua Estes <[email protected]>
*/
class Link implements LinkInterface
{
protected array $rels = [];
public function __construct(
protected string $href = '',
array|string $rels = null,
protected array $attributes = [],
) {
if ('' === $href && !$this instanceof EvolvableLink) {
throw new InvalidArgumentException('MUST pass in href');
}
if (null === $rels) {
return;
}
if (is_array($rels)) {
foreach ($rels as $rel) {
$this->rels[$rel] = $rel;
}
}
if (is_string($rels)) {
$this->rels[$rels] = $rels;
}
}
/**
* Returns the target of the link.
*
* The target link must be one of:
* - An absolute URI, as defined by RFC 5988.
* - A relative URI, as defined by RFC 5988. The base of the relative link
* is assumed to be known based on context by the client.
* - A URI template as defined by RFC 6570.
*
* If a URI template is returned, isTemplated() MUST return True.
*/
public function getHref(): string
{
return $this->href;
}
/**
* Returns whether or not this is a templated link.
*
* @return bool
* True if this link object is templated, False otherwise.
*/
public function isTemplated(): bool
{
return str_contains($this->href, '{') && str_contains($this->href, '}');
}
/**
* Returns the relationship type(s) of the link.
*
* This method returns 0 or more relationship types for a link, expressed
* as an array of strings.
*
* @return string[]
*/
public function getRels(): array
{
return array_values($this->rels);
}
/**
* Returns a list of attributes that describe the target URI.
*
* @return array
* A key-value list of attributes, where the key is a string and the value
* is either a PHP primitive or an array of PHP strings. If no values are
* found an empty array MUST be returned.
*/
public function getAttributes(): array
{
return $this->attributes;
}
}