Link::jsonSerialize()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 0
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Resource\Annotation;
6
7
use Attribute;
8
use JsonSerializable;
9
use Override;
10
11
#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
12
final class Link implements JsonSerializable
13
{
14
    /**
15
     * Relation to the target resource of the link
16
     *
17
     * @var string
18
     */
19
    public $rel;
20
21
    /**
22
     * A URI template, as defined by RFC 6570
23
     *
24
     * @var string
25
     */
26
    public $href;
27
28
    /**
29
     * A method for the Link
30
     *
31
     * @var string
32
     */
33
    public $method;
34
35
    /**
36
     * A title for the link
37
     *
38
     * @var string
39
     */
40
    public $title;
41
42
    /**
43
     * Crawl tag ID for crawl request
44
     *
45
     * @var string
46
     */
47
    public $crawl;
48
49
    /**
50
     * @return string[]
51
     * @psalm-return array{rel: string, href: string, method: string, title?: string}
52
     */
53
    #[Override]
54
    public function jsonSerialize(): array
55
    {
56
        $json = [
57
            'rel' => $this->rel,
58
            'href' => $this->href,
59
            'method' => $this->method,
60
        ];
61
        if ($this->title) {
62
            $json += ['title' => $this->title];
63
        }
64
65
        return $json;
66
    }
67
68
    /** @param array{rel?: string, href?: string, method?: string, title?: string, crawl?:string} $values */
69
    public function __construct(
70
        array $values = [],
71
        string $rel = '',
72
        string $href = '',
73
        string $method = 'get',
74
        string $title = '',
75
        string $crawl = '',
76
    ) {
77
        $this->rel = $values['rel'] ?? $rel;
78
        $this->href = $values['href'] ?? $href;
79
        $this->method = $values['method'] ?? $method;
80
        $this->title = $values['title'] ?? $title;
81
        $this->crawl = $values['crawl'] ?? $crawl;
82
    }
83
}
84