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 BEAR\Resource\DataLoader\DataLoaderInterface;
9
use JsonSerializable;
10
use Override;
11
12
#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
13
final class Link implements JsonSerializable
14
{
15
    /**
16
     * Relation to the target resource of the link
17
     *
18
     * @var string
19
     */
20
    public $rel;
21
22
    /**
23
     * A URI template, as defined by RFC 6570
24
     *
25
     * @var string
26
     */
27
    public $href;
28
29
    /**
30
     * A method for the Link
31
     *
32
     * @var string
33
     */
34
    public $method;
35
36
    /**
37
     * A title for the link
38
     *
39
     * @var string
40
     */
41
    public $title;
42
43
    /**
44
     * Crawl tag ID for crawl request
45
     *
46
     * @var string
47
     */
48
    public $crawl;
49
50
    /**
51
     * DataLoader class for bulk processing
52
     *
53
     * @var class-string<DataLoaderInterface>|null
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<DataLoaderInterface>|null at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<DataLoaderInterface>|null.
Loading history...
54
     */
55
    public $dataLoader;
56
57
    /**
58
     * @return string[]
59
     * @psalm-return array{rel: string, href: string, method: string, title?: string}
60
     */
61
    #[Override]
62
    public function jsonSerialize(): array
63
    {
64
        $json = [
65
            'rel' => $this->rel,
66
            'href' => $this->href,
67
            'method' => $this->method,
68
        ];
69
        if ($this->title) {
70
            $json += ['title' => $this->title];
71
        }
72
73
        return $json;
74
    }
75
76
    /** @param array{rel?: string, href?: string, method?: string, title?: string, crawl?: string, dataLoader?: class-string<DataLoaderInterface>|null} $values */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{rel?: string, href...aLoaderInterface>|null} at position 24 could not be parsed: Unknown type name 'class-string' at position 24 in array{rel?: string, href?: string, method?: string, title?: string, crawl?: string, dataLoader?: class-string<DataLoaderInterface>|null}.
Loading history...
77
    public function __construct(
78
        array $values = [],
79
        string $rel = '',
80
        string $href = '',
81
        string $method = 'get',
82
        string $title = '',
83
        string $crawl = '',
84
        string|null $dataLoader = null,
85
    ) {
86
        $this->rel = $values['rel'] ?? $rel;
87
        $this->href = $values['href'] ?? $href;
88
        $this->method = $values['method'] ?? $method;
89
        $this->title = $values['title'] ?? $title;
90
        $this->crawl = $values['crawl'] ?? $crawl;
91
        /** @var class-string<DataLoaderInterface>|null $resolvedDataLoader */
92
        $resolvedDataLoader = $values['dataLoader'] ?? $dataLoader;
93
        $this->dataLoader = $resolvedDataLoader;
94
    }
95
}
96