Completed
Branch next (8f844e)
by Neomerx
04:06
created

RelationshipDataIsResource::getIdentifier()   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
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 RelationshipDataIsResource extends BaseRelationshipData implements RelationshipDataInterface
34
{
35
    /** @var string */
36
    public const MSG_INVALID_OPERATION = 'Invalid operation.';
37
38
    /**
39
     * @var mixed
40
     */
41
    private $resource;
42
43
    /**
44
     * @var null|ResourceInterface
45
     */
46
    private $parsedResource = null;
47
48
    /**
49
     * @param FactoryInterface          $factory
50
     * @param SchemaContainerInterface  $schemaContainer
51
     * @param PositionInterface         $position
52
     * @param mixed                     $resource
53
     */
54 24
    public function __construct(
55
        FactoryInterface $factory,
56
        SchemaContainerInterface $schemaContainer,
57
        PositionInterface $position,
58
        $resource
59
    ) {
60 24
        parent::__construct($factory, $schemaContainer, $position);
61
62 24
        $this->resource = $resource;
63 24
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68 1
    public function isCollection(): bool
69
    {
70 1
        return false;
71
    }
72
73
    /**
74
     * @inheritdoc
75
     */
76 1
    public function isNull(): bool
77
    {
78 1
        return false;
79
    }
80
81
    /**
82
     * @inheritdoc
83
     */
84 23
    public function isResource(): bool
85
    {
86 23
        return true;
87
    }
88
89
    /**
90
     * @inheritdoc
91
     */
92 1
    public function isIdentifier(): bool
93
    {
94 1
        return false;
95
    }
96
97
    /**
98
     * @inheritdoc
99
     */
100 1
    public function getIdentifier(): IdentifierInterface
101
    {
102 1
        throw new LogicException(_(static::MSG_INVALID_OPERATION));
103
    }
104
105
    /**
106
     * @inheritdoc
107
     */
108 1
    public function getIdentifiers(): iterable
109
    {
110 1
        throw new LogicException(_(static::MSG_INVALID_OPERATION));
111
    }
112
113
    /**
114
     * @inheritdoc
115
     */
116 22
    public function getResource(): ResourceInterface
117
    {
118 22
        if ($this->parsedResource === null) {
119 22
            $this->parsedResource = $this->createParsedResource($this->resource);
120
        }
121
122 22
        return $this->parsedResource;
123
    }
124
125
    /**
126
     * @inheritdoc
127
     */
128 1
    public function getResources(): iterable
129
    {
130 1
        throw new LogicException(_(static::MSG_INVALID_OPERATION));
131
    }
132
}
133