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
|
|
|
* If link contains sub-URL value and URL prefix should be added. |
31
|
|
|
* |
32
|
|
|
* @var bool |
33
|
|
|
*/ |
34
|
|
|
private $isSubUrl; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get link’s URL value (full URL or sub-URL). |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $value; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* If link has meta information. |
45
|
|
|
* |
46
|
|
|
* @var bool |
47
|
|
|
*/ |
48
|
|
|
private $hasMeta; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var mixed |
52
|
|
|
*/ |
53
|
|
|
private $meta; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param bool $isSubUrl |
57
|
|
|
* @param string $value |
58
|
|
|
* @param bool $hasMeta |
59
|
|
|
* @param mixed $meta |
60
|
|
|
*/ |
61
|
66 |
|
public function __construct(bool $isSubUrl, string $value, bool $hasMeta, $meta = null) |
62
|
|
|
{ |
63
|
66 |
|
$this->isSubUrl = $isSubUrl; |
64
|
66 |
|
$this->value = $value; |
65
|
66 |
|
$this->hasMeta = $hasMeta; |
66
|
66 |
|
$this->meta = $meta; |
67
|
66 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @inheritdoc |
71
|
|
|
*/ |
72
|
62 |
|
public function canBeShownAsString(): bool |
73
|
|
|
{ |
74
|
62 |
|
return $this->hasMeta === false; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @inheritdoc |
79
|
|
|
*/ |
80
|
62 |
|
public function getStringRepresentation(string $prefix): string |
81
|
|
|
{ |
82
|
62 |
|
\assert($this->canBeShownAsString() === true); |
83
|
|
|
|
84
|
62 |
|
return $this->buildUrl($prefix); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @inheritdoc |
89
|
|
|
*/ |
90
|
1 |
|
public function getArrayRepresentation(string $prefix): array |
91
|
|
|
{ |
92
|
1 |
|
\assert($this->canBeShownAsString() === false); |
93
|
|
|
|
94
|
1 |
|
\assert($this->hasMeta); |
95
|
|
|
|
96
|
|
|
return [ |
97
|
1 |
|
DocumentInterface::KEYWORD_HREF => $this->buildUrl($prefix), |
98
|
1 |
|
DocumentInterface::KEYWORD_META => $this->meta, |
99
|
|
|
]; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $prefix |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
62 |
|
protected function buildUrl(string $prefix): string |
108
|
|
|
{ |
109
|
62 |
|
return $this->isSubUrl ? $prefix . $this->value : $this->value; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|