1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Neomerx\JsonApi\Parser\RelationshipData; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2020 [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\Factories\FactoryInterface; |
22
|
|
|
use Neomerx\JsonApi\Contracts\Parser\EditableContextInterface; |
23
|
|
|
use Neomerx\JsonApi\Contracts\Parser\IdentifierInterface as ParserIdentifierInterface; |
24
|
|
|
use Neomerx\JsonApi\Contracts\Parser\RelationshipDataInterface; |
25
|
|
|
use Neomerx\JsonApi\Contracts\Parser\ResourceInterface; |
26
|
|
|
use Neomerx\JsonApi\Contracts\Schema\IdentifierInterface as SchemaIdentifierInterface; |
27
|
|
|
use Neomerx\JsonApi\Contracts\Schema\PositionInterface; |
28
|
|
|
use Neomerx\JsonApi\Contracts\Schema\SchemaContainerInterface; |
29
|
|
|
use Neomerx\JsonApi\Exceptions\LogicException; |
30
|
|
|
use function Neomerx\JsonApi\I18n\format as _; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @package Neomerx\JsonApi |
34
|
|
|
*/ |
35
|
|
|
class RelationshipDataIsIdentifier extends BaseRelationshipData implements RelationshipDataInterface |
36
|
|
|
{ |
37
|
|
|
/** @var string */ |
38
|
|
|
public const MSG_INVALID_OPERATION = 'Invalid operation.'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var mixed |
42
|
|
|
*/ |
43
|
|
|
private $identifier; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var null|ParserIdentifierInterface |
47
|
|
|
*/ |
48
|
|
|
private $parsedIdentifier = null; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param FactoryInterface $factory |
52
|
|
|
* @param SchemaContainerInterface $schemaContainer |
53
|
|
|
* @param EditableContextInterface $context |
54
|
|
|
* @param PositionInterface $position |
55
|
|
|
* @param SchemaIdentifierInterface $identifier |
56
|
|
|
*/ |
57
|
2 |
|
public function __construct( |
58
|
|
|
FactoryInterface $factory, |
59
|
|
|
SchemaContainerInterface $schemaContainer, |
60
|
|
|
EditableContextInterface $context, |
61
|
|
|
PositionInterface $position, |
62
|
|
|
SchemaIdentifierInterface $identifier |
63
|
|
|
) { |
64
|
2 |
|
parent::__construct($factory, $schemaContainer, $context, $position); |
65
|
|
|
|
66
|
2 |
|
$this->identifier = $identifier; |
67
|
2 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @inheritdoc |
71
|
|
|
*/ |
72
|
1 |
|
public function isCollection(): bool |
73
|
|
|
{ |
74
|
1 |
|
return false; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @inheritdoc |
79
|
|
|
*/ |
80
|
1 |
|
public function isNull(): bool |
81
|
|
|
{ |
82
|
1 |
|
return false; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @inheritdoc |
87
|
|
|
*/ |
88
|
2 |
|
public function isResource(): bool |
89
|
|
|
{ |
90
|
2 |
|
return false; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @inheritdoc |
95
|
|
|
*/ |
96
|
2 |
|
public function isIdentifier(): bool |
97
|
|
|
{ |
98
|
2 |
|
return true; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @inheritdoc |
103
|
|
|
*/ |
104
|
1 |
|
public function getIdentifier(): ParserIdentifierInterface |
105
|
|
|
{ |
106
|
1 |
|
if ($this->parsedIdentifier === null) { |
107
|
1 |
|
$this->parsedIdentifier = $this->createParsedIdentifier($this->identifier); |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
return $this->parsedIdentifier; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @inheritdoc |
115
|
|
|
*/ |
116
|
1 |
|
public function getIdentifiers(): iterable |
117
|
|
|
{ |
118
|
1 |
|
throw new LogicException(_(static::MSG_INVALID_OPERATION)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @inheritdoc |
123
|
|
|
*/ |
124
|
1 |
|
public function getResource(): ResourceInterface |
125
|
|
|
{ |
126
|
1 |
|
throw new LogicException(_(static::MSG_INVALID_OPERATION)); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @inheritdoc |
131
|
|
|
*/ |
132
|
1 |
|
public function getResources(): iterable |
133
|
|
|
{ |
134
|
1 |
|
throw new LogicException(_(static::MSG_INVALID_OPERATION)); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|