Completed
Push — master ( 76e900...bb0f4d )
by Jim
02:02
created

Link::getAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Oddvalue\LinkBuilder;
4
5
use Oddvalue\LinkBuilder\Contracts\Linkable;
6
use Oddvalue\LinkBuilder\Contracts\LinkGenerator;
7
8
class Link implements LinkGenerator
9
{
10
    protected $model;
11
12
    protected $attributes;
13
14
    /**
15
     * Instantiate the generator with the linkable model
16
     *
17
     * @param Linkable $model
18
     * @param array $attributes
19
     */
20 18
    public function __construct(Linkable $model, array $attributes = [])
21
    {
22 18
        $this->model = $model;
23 18
        $this->setAttributes($attributes);
24 18
    }
25
26
    /**
27
     * Get the link href for a given model
28
     *
29
     * @param $model
30
     * @return string
31
     */
32 15
    public function href() : string
33
    {
34 15
        return (string) url($this->model->slug ?? '/');
0 ignored issues
show
Bug introduced by
Accessing slug on the interface Oddvalue\LinkBuilder\Contracts\Linkable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
35
    }
36
37
    /**
38
     * Get the link text for a given model
39
     *
40
     * @param $model
41
     * @param array $options
42
     * @return string
43
     */
44 15
    public function label() : string
45
    {
46 15
        return (string) $this->model->name;
0 ignored issues
show
Bug introduced by
Accessing name on the interface Oddvalue\LinkBuilder\Contracts\Linkable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
47
    }
48
49 3
    public function getAttributes() : HtmlAttributes
50
    {
51 3
        return $this->attributes;
52
    }
53
54 18
    public function setAttributes(array $attributes)
55
    {
56 18
        $this->attributes = HtmlAttributes::make($attributes);
57 18
        return $this;
58
    }
59
60
    /**
61
     * Generate an HTML link for the model
62
     *
63
     * @return string
64
     */
65 12
    public function toHtml()
66
    {
67
        return <<<HTML
68 12
<a href="{$this->href()}"{$this->attributes}>{$this->label()}</a>
69
HTML;
70
    }
71
72
    /**
73
     * Cast the generator to a string
74
     *
75
     * @return string
76
     */
77 6
    public function __toString() : string
78
    {
79 6
        return $this->toHtml();
80
    }
81
}
82