1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\Restful\Resource; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\Restful\IResource; |
7
|
|
|
use Stringable; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Link representation in resource |
12
|
|
|
* @package kalanis\Restful\Resource |
13
|
|
|
*/ |
14
|
1 |
|
class Link implements IResource, Stringable |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** Link pointing on self */ |
18
|
|
|
public const SELF = 'self'; |
19
|
|
|
/** Link pointing on next page */ |
20
|
|
|
public const NEXT = 'next'; |
21
|
|
|
/** Link pointing on previous page */ |
22
|
|
|
public const PREVIOUS = 'prev'; |
23
|
|
|
/** Link pointing on last page */ |
24
|
|
|
public const LAST = 'last'; |
25
|
|
|
|
26
|
1 |
|
public function __construct( |
27
|
|
|
private readonly string $href, |
28
|
|
|
private readonly string $rel = self::SELF, |
29
|
|
|
) |
30
|
|
|
{ |
31
|
1 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Get link URL |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
|
|
public function getHref(): string |
38
|
|
|
{ |
39
|
1 |
|
return $this->href; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Create link with new href |
44
|
|
|
* @param string $href |
45
|
|
|
*/ |
46
|
|
|
public function setHref(string $href): self |
47
|
|
|
{ |
48
|
1 |
|
return new Link($href, $this->rel); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get link rel |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public function getRel(): string |
56
|
|
|
{ |
57
|
1 |
|
return $this->rel; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Create link with new rel |
62
|
|
|
* @param string $rel |
63
|
|
|
*/ |
64
|
|
|
public function setRel(string $rel): self |
65
|
|
|
{ |
66
|
1 |
|
return new Link($this->href, $rel); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Converts link to string |
71
|
|
|
*/ |
72
|
|
|
public function __toString(): string |
73
|
|
|
{ |
74
|
1 |
|
return '<' . $this->href . '>;rel="' . $this->rel . '"'; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get element value or array data |
79
|
|
|
* @return array<string, string> |
80
|
|
|
*/ |
81
|
|
|
public function getData(): array |
82
|
|
|
{ |
83
|
|
|
return [ |
84
|
1 |
|
'href' => $this->href, |
85
|
1 |
|
'rel' => $this->rel, |
86
|
|
|
]; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|