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 RelationshipDataIsCollection extends BaseRelationshipData implements RelationshipDataInterface |
36
|
|
|
{ |
37
|
|
|
/** @var string */ |
38
|
|
|
public const MSG_INVALID_OPERATION = 'Invalid operation.'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var iterable |
42
|
|
|
*/ |
43
|
|
|
private $resources; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var iterable |
47
|
|
|
*/ |
48
|
|
|
private $parsedResources = null; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param FactoryInterface $factory |
52
|
|
|
* @param SchemaContainerInterface $schemaContainer |
53
|
|
|
* @param EditableContextInterface $context |
54
|
|
|
* @param PositionInterface $position |
55
|
|
|
* @param iterable $resources |
56
|
|
|
*/ |
57
|
36 |
|
public function __construct( |
58
|
|
|
FactoryInterface $factory, |
59
|
|
|
SchemaContainerInterface $schemaContainer, |
60
|
|
|
EditableContextInterface $context, |
61
|
|
|
PositionInterface $position, |
62
|
|
|
iterable $resources |
63
|
|
|
) { |
64
|
36 |
|
parent::__construct($factory, $schemaContainer, $context, $position); |
65
|
|
|
|
66
|
36 |
|
$this->resources = $resources; |
67
|
36 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @inheritdoc |
71
|
|
|
*/ |
72
|
35 |
|
public function isCollection(): bool |
73
|
|
|
{ |
74
|
35 |
|
return true; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @inheritdoc |
79
|
|
|
*/ |
80
|
1 |
|
public function isNull(): bool |
81
|
|
|
{ |
82
|
1 |
|
return false; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @inheritdoc |
87
|
|
|
*/ |
88
|
35 |
|
public function isResource(): bool |
89
|
|
|
{ |
90
|
35 |
|
return false; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @inheritdoc |
95
|
|
|
*/ |
96
|
32 |
|
public function isIdentifier(): bool |
97
|
|
|
{ |
98
|
32 |
|
return false; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @inheritdoc |
103
|
|
|
*/ |
104
|
1 |
|
public function getIdentifier(): ParserIdentifierInterface |
105
|
|
|
{ |
106
|
1 |
|
throw new LogicException(_(static::MSG_INVALID_OPERATION)); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @inheritdoc |
111
|
|
|
*/ |
112
|
31 |
|
public function getIdentifiers(): iterable |
113
|
|
|
{ |
114
|
31 |
|
return $this->getResources(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @inheritdoc |
119
|
|
|
*/ |
120
|
1 |
|
public function getResource(): ResourceInterface |
121
|
|
|
{ |
122
|
1 |
|
throw new LogicException(_(static::MSG_INVALID_OPERATION)); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @inheritdoc |
127
|
|
|
*/ |
128
|
34 |
|
public function getResources(): iterable |
129
|
|
|
{ |
130
|
34 |
|
if ($this->parsedResources === null) { |
131
|
34 |
|
foreach ($this->resources as $resourceOrIdentifier) { |
132
|
30 |
|
$parsedResource = $resourceOrIdentifier instanceof SchemaIdentifierInterface ? |
133
|
1 |
|
$this->createParsedIdentifier($resourceOrIdentifier) : |
134
|
30 |
|
$this->createParsedResource($resourceOrIdentifier); |
135
|
30 |
|
$this->parsedResources[] = $parsedResource; |
136
|
|
|
|
137
|
30 |
|
yield $parsedResource; |
138
|
|
|
} |
139
|
|
|
|
140
|
34 |
|
return; |
141
|
|
|
} |
142
|
|
|
|
143
|
18 |
|
yield from $this->parsedResources; |
144
|
17 |
|
} |
145
|
|
|
} |
146
|
|
|
|