Completed
Push — master ( 68a553...e9f254 )
by Milroy
01:21
created

Link::name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
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_DELETE = 'delete';
12
13
    private $name;
14
15
    private $url;
16
17
    private $meta = [];
18
19
    public function __construct(string $name, string $url)
20
    {
21
        $this->name = $name;
22
        $this->url = $url;
23
    }
24
25
    public static function make(string $name, string $url): self
26
    {
27
        return new self($name, $url);
28
    }
29
30
    public function name(): string
31
    {
32
        return $this->name;
33
    }
34
35
    public function post()
36
    {
37
        return $this->setMethod(self::METHOD_POST);
38
    }
39
40
    public function put()
41
    {
42
        return $this->setMethod(self::METHOD_PUT);
43
    }
44
45
    public function delete()
46
    {
47
        return $this->setMethod(self::METHOD_DELETE);
48
    }
49
50
    private function setMethod(string $method)
51
    {
52
        return $this->meta('method', $method);
53
    }
54
55
    public function meta(string $key, $value)
56
    {
57
        $this->meta[$key] = $value;
58
59
        return $this;
60
    }
61
62
    public function setData(array $data)
63
    {
64
        return $this->meta('data', [
65
            'data' => $data
66
        ]);
67
    }
68
69
    public function data()
70
    {
71
        if (empty($this->meta)) {
72
            return $this->url;
73
        }
74
75
        return [
76
            'href' => $this->url,
77
            'meta' => $this->meta,
78
        ];
79
    }
80
}
81