Link::getArrayRepresentation()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 11
ccs 5
cts 5
cp 1
crap 1
rs 9.9
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Neomerx\JsonApi\Schema;
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\Schema\DocumentInterface;
22
use Neomerx\JsonApi\Contracts\Schema\LinkInterface;
23
24
/**
25
 * @package Neomerx\JsonApi
26
 */
27
class Link implements LinkInterface
28
{
29
    /**
30
     * If link contains sub-URL value and URL prefix should be added.
31
     *
32
     * @var bool
33
     */
34
    private $isSubUrl;
35
36
    /**
37
     * Get link’s URL value (full URL or sub-URL).
38
     *
39
     * @var string
40
     */
41
    private $value;
42
43
    /**
44
     * If link has meta information.
45
     *
46
     * @var bool
47
     */
48
    private $hasMeta;
49
50
    /**
51
     * @var mixed
52
     */
53
    private $meta;
54
55
    /**
56
     * @param bool   $isSubUrl
57
     * @param string $value
58
     * @param bool   $hasMeta
59
     * @param mixed  $meta
60
     */
61 71
    public function __construct(bool $isSubUrl, string $value, bool $hasMeta, $meta = null)
62
    {
63 71
        $this->isSubUrl = $isSubUrl;
64 71
        $this->value    = $value;
65 71
        $this->hasMeta  = $hasMeta;
66 71
        $this->meta     = $meta;
67 71
    }
68
69
    /**
70
     * @inheritdoc
71
     */
72 67
    public function canBeShownAsString(): bool
73
    {
74 67
        return $this->hasMeta === false;
75
    }
76
77
    /**
78
     * @inheritdoc
79
     */
80 67
    public function getStringRepresentation(string $prefix): string
81
    {
82 67
        \assert($this->canBeShownAsString() === true);
83
84 67
        return $this->buildUrl($prefix);
85
    }
86
87
    /**
88
     * @inheritdoc
89
     */
90 1
    public function getArrayRepresentation(string $prefix): array
91
    {
92 1
        \assert($this->canBeShownAsString() === false);
93
94 1
        \assert($this->hasMeta);
95
96
        return [
97 1
            DocumentInterface::KEYWORD_HREF => $this->buildUrl($prefix),
98 1
            DocumentInterface::KEYWORD_META => $this->meta,
99
        ];
100
    }
101
102
    /**
103
     * @param string $prefix
104
     *
105
     * @return string
106
     */
107 67
    protected function buildUrl(string $prefix): string
108
    {
109 67
        return $this->isSubUrl ? $prefix . $this->value : $this->value;
110
    }
111
}
112