Link::setData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sarala;
6
7
class Link
8
{
9
    const METHOD_POST = 'post';
10
    const METHOD_PUT = 'put';
11
    const METHOD_PATCH = 'patch';
12
    const METHOD_DELETE = 'delete';
13
14
    private string $name;
15
16
    private string $url;
17
18
    private array $meta = [];
19
20
    public function __construct(string $name, string $url)
21
    {
22
        $this->name = $name;
23
        $this->url = $url;
24
    }
25
26
    public static function make(string $name, string $url): self
27
    {
28
        return new self($name, $url);
29
    }
30
31
    public function name(): string
32
    {
33
        return $this->name;
34
    }
35
36
    public function post(): self
37
    {
38
        return $this->setMethod(self::METHOD_POST);
39
    }
40
41
    public function put(): self
42
    {
43
        return $this->setMethod(self::METHOD_PUT);
44
    }
45
46
    public function patch(): self
47
    {
48
        return $this->setMethod(self::METHOD_PATCH);
49
    }
50
51
    public function delete(): self
52
    {
53
        return $this->setMethod(self::METHOD_DELETE);
54
    }
55
56
    private function setMethod(string $method): self
57
    {
58
        return $this->meta('method', $method);
59
    }
60
61
    public function meta(string $key, $value): self
62
    {
63
        $this->meta[$key] = $value;
64
65
        return $this;
66
    }
67
68
    public function setData(array $data): self
69
    {
70
        return $this->meta('data', $data);
71
    }
72
73
    /**
74
     * @return array|string
75
     */
76
    public function data()
77
    {
78
        if (empty($this->meta)) {
79
            return $this->url;
80
        }
81
82
        return [
83
            'href' => $this->url,
84
            'meta' => $this->meta,
85
        ];
86
    }
87
}
88