|
1
|
|
|
<?php namespace Neomerx\JsonApi\Document; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2015-2018 [email protected] |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
use Neomerx\JsonApi\Contracts\Document\DocumentInterface; |
|
20
|
|
|
use Neomerx\JsonApi\Contracts\Document\LinkInterface; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @package Neomerx\JsonApi |
|
24
|
|
|
* |
|
25
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
26
|
|
|
*/ |
|
27
|
|
|
class Link implements LinkInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
private $subHref; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var mixed|null |
|
36
|
|
|
*/ |
|
37
|
|
|
private $meta; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var bool |
|
41
|
|
|
*/ |
|
42
|
|
|
private $treatAsHref; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param string $subHref |
|
46
|
|
|
* @param mixed|null $meta |
|
47
|
|
|
* @param bool $treatAsHref If $subHref is a full URL and must not be concatenated with other URLs. |
|
48
|
|
|
* |
|
49
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
|
50
|
|
|
*/ |
|
51
|
86 |
|
public function __construct(string $subHref, $meta = null, bool $treatAsHref = false) |
|
52
|
|
|
{ |
|
53
|
86 |
|
$this->subHref = $subHref; |
|
54
|
86 |
|
$this->meta = $meta; |
|
55
|
86 |
|
$this->treatAsHref = $treatAsHref; |
|
56
|
86 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @inheritdoc |
|
60
|
|
|
*/ |
|
61
|
71 |
|
public function getSubHref(): string |
|
62
|
|
|
{ |
|
63
|
71 |
|
return $this->subHref; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @inheritdoc |
|
68
|
|
|
*/ |
|
69
|
71 |
|
public function getMeta() |
|
70
|
|
|
{ |
|
71
|
71 |
|
return $this->meta; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @inheritdoc |
|
76
|
|
|
*/ |
|
77
|
71 |
|
public function isTreatAsHref(): bool |
|
78
|
|
|
{ |
|
79
|
71 |
|
return $this->treatAsHref; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @inheritdoc |
|
84
|
|
|
*/ |
|
85
|
71 |
|
public function hasMeta(): bool |
|
86
|
|
|
{ |
|
87
|
71 |
|
return $this->getMeta() !== null; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @inheritdoc |
|
92
|
|
|
*/ |
|
93
|
71 |
|
public function getHref(string $prefix = null): string |
|
94
|
|
|
{ |
|
95
|
71 |
|
return $this->isTreatAsHref() === true ? $this->getSubHref() : $prefix . $this->getSubHref(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @inheritdoc |
|
100
|
|
|
*/ |
|
101
|
1 |
|
public function getHrefWithMeta(string $prefix = null): array |
|
102
|
|
|
{ |
|
103
|
|
|
return [ |
|
104
|
1 |
|
DocumentInterface::KEYWORD_HREF => $this->getHref($prefix), |
|
105
|
1 |
|
DocumentInterface::KEYWORD_META => $this->getMeta(), |
|
106
|
|
|
]; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|