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 Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
9
use JsonSerializable;
10
use Override;
11
12
/**
13
 * @Annotation
14
 * @Target("METHOD")
15
 * @NamedArgumentConstructor
16
 */
17
#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
18
final class Link implements JsonSerializable
19
{
20
    /**
21
     * Relation to the target resource of the link
22
     *
23
     * @var string
24
     */
25
    public $rel;
26
27
    /**
28
     * A URI template, as defined by RFC 6570
29
     *
30
     * @var string
31
     */
32
    public $href;
33
34
    /**
35
     * A method for the Link
36
     *
37
     * @var string
38
     * @Enum({"get", "post", "put", "patch", "delete"})
39
     */
40
    public $method;
41
42
    /**
43
     * A title for the link
44
     *
45
     * @var string
46
     */
47
    public $title;
48
49
    /**
50
     * Crawl tag ID for crawl request
51
     *
52
     * @var string
53
     */
54
    public $crawl;
55
56
    /**
57
     * @return string[]
58
     * @psalm-return array{rel: string, href: string, method: string, title?: string}
59
     */
60
    #[Override]
61
    public function jsonSerialize(): array
62
    {
63
        $json = [
64
            'rel' => $this->rel,
65
            'href' => $this->href,
66
            'method' => $this->method,
67
        ];
68
        if ($this->title) {
69
            $json += ['title' => $this->title];
70
        }
71
72
        return $json;
73
    }
74
75
    /** @param array{rel?: string, href?: string, method?: string, title?: string, crawl?:string} $values */
76
    public function __construct(
77
        array $values = [],
78
        string $rel = '',
79
        string $href = '',
80
        string $method = 'get',
81
        string $title = '',
82
        string $crawl = '',
83
    ) {
84
        $this->rel = $values['rel'] ?? $rel;
85
        $this->href = $values['href'] ?? $href;
86
        $this->method = $values['method'] ?? $method;
87
        $this->title = $values['title'] ?? $title;
88
        $this->crawl = $values['crawl'] ?? $crawl;
89
    }
90
}
91