1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Neomerx\JsonApi\Schema; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2019 [email protected] |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
use Neomerx\JsonApi\Contracts\Schema\DocumentInterface; |
22
|
|
|
use Neomerx\JsonApi\Contracts\Schema\LinkInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @package Neomerx\JsonApi |
26
|
|
|
*/ |
27
|
|
|
class Link implements LinkInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var bool |
31
|
|
|
*/ |
32
|
|
|
private $isSubUrl; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $value; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var bool |
41
|
|
|
*/ |
42
|
|
|
private $hasMeta; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var mixed |
46
|
|
|
*/ |
47
|
|
|
private $meta; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param bool $isSubUrl |
51
|
|
|
* @param string $value |
52
|
|
|
* @param bool $hasMeta |
53
|
|
|
* @param mixed $meta |
54
|
|
|
*/ |
55
|
66 |
|
public function __construct(bool $isSubUrl, string $value, bool $hasMeta, $meta = null) |
56
|
|
|
{ |
57
|
66 |
|
$this->isSubUrl = $isSubUrl; |
58
|
66 |
|
$this->value = $value; |
59
|
66 |
|
$this->hasMeta = $hasMeta; |
60
|
66 |
|
$this->meta = $meta; |
61
|
66 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @inheritdoc |
65
|
|
|
*/ |
66
|
62 |
|
public function canBeShownAsString(): bool |
67
|
|
|
{ |
68
|
62 |
|
return $this->hasMeta() === false; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @inheritdoc |
73
|
|
|
*/ |
74
|
62 |
|
public function getStringRepresentation(string $prefix): string |
75
|
|
|
{ |
76
|
62 |
|
assert($this->canBeShownAsString() === true); |
77
|
|
|
|
78
|
62 |
|
return $this->buildUrl($prefix); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @inheritdoc |
83
|
|
|
*/ |
84
|
1 |
|
public function getArrayRepresentation(string $prefix): array |
85
|
|
|
{ |
86
|
1 |
|
assert($this->canBeShownAsString() === false); |
87
|
|
|
|
88
|
|
|
return [ |
89
|
1 |
|
DocumentInterface::KEYWORD_HREF => $this->buildUrl($prefix), |
90
|
1 |
|
DocumentInterface::KEYWORD_META => $this->getMeta(), |
91
|
|
|
]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* If link contains sub-URL value and URL prefix should be added. |
96
|
|
|
* |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
62 |
|
private function isSubUrl(): bool |
100
|
|
|
{ |
101
|
62 |
|
return $this->isSubUrl; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get link’s URL value (full URL or sub-URL). |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
62 |
|
private function getValue(): string |
110
|
|
|
{ |
111
|
62 |
|
return $this->value; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* If link has meta information. |
116
|
|
|
* |
117
|
|
|
* @return bool |
118
|
|
|
*/ |
119
|
62 |
|
private function hasMeta(): bool |
120
|
|
|
{ |
121
|
62 |
|
return $this->hasMeta; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get meta information. |
126
|
|
|
* |
127
|
|
|
* @return mixed |
128
|
|
|
*/ |
129
|
1 |
|
private function getMeta() |
130
|
|
|
{ |
131
|
1 |
|
assert($this->hasMeta()); |
132
|
|
|
|
133
|
1 |
|
return $this->meta; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $prefix |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
62 |
|
protected function buildUrl(string $prefix): string |
142
|
|
|
{ |
143
|
62 |
|
return $this->isSubUrl() ? $prefix . $this->getValue() : $this->getValue(); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|