|
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\LinkWithAliasesInterface; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @package Neomerx\JsonApi |
|
26
|
|
|
*/ |
|
27
|
|
|
class LinkWithAliases extends Link implements LinkWithAliasesInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
private $aliases; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var bool |
|
36
|
|
|
*/ |
|
37
|
|
|
private $hasAliases; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param bool $isSubUrl |
|
41
|
|
|
* @param string $value |
|
42
|
|
|
* @param iterable $aliases |
|
43
|
|
|
* @param bool $hasMeta |
|
44
|
|
|
* @param null $meta |
|
45
|
|
|
*/ |
|
46
|
5 |
|
public function __construct(bool $isSubUrl, string $value, iterable $aliases, bool $hasMeta, $meta = null) |
|
47
|
|
|
{ |
|
48
|
5 |
|
$aliasesArray = []; |
|
49
|
5 |
|
foreach ($aliases as $name => $alias) { |
|
50
|
5 |
|
\assert(\is_string($name) === true && empty($name) === false); |
|
51
|
5 |
|
\assert(\is_string($alias) === true && empty($alias) === false); |
|
52
|
5 |
|
$aliasesArray[$name] = $alias; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
5 |
|
$this->aliases = $aliasesArray; |
|
56
|
5 |
|
$this->hasAliases = !empty($aliasesArray); |
|
57
|
|
|
|
|
58
|
5 |
|
parent::__construct($isSubUrl, $value, $hasMeta, $meta); |
|
59
|
5 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @inheritdoc |
|
63
|
|
|
*/ |
|
64
|
5 |
|
public function canBeShownAsString(): bool |
|
65
|
|
|
{ |
|
66
|
5 |
|
return parent::canBeShownAsString() && $this->hasAliases === false; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @inheritdoc |
|
71
|
|
|
*/ |
|
72
|
5 |
|
public function getArrayRepresentation(string $prefix): array |
|
73
|
|
|
{ |
|
74
|
5 |
|
$linkRepresentation = parent::canBeShownAsString() === true ? [ |
|
|
|
|
|
|
75
|
5 |
|
DocumentInterface::KEYWORD_HREF => $this->buildUrl($prefix), |
|
76
|
5 |
|
] : parent::getArrayRepresentation($prefix); |
|
77
|
|
|
|
|
78
|
5 |
|
if ($this->hasAliases === true) { |
|
79
|
5 |
|
$linkRepresentation[DocumentInterface::KEYWORD_ALIASES] = $this->aliases; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
5 |
|
return $linkRepresentation; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.