Completed
Branch next (92e3a0)
by Neomerx
01:44
created

RelationshipDataIsCollection::isCollection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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