Link::label()   A
last analyzed

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 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
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
abstract class Link implements LinkGenerator
9
{
10
    /**
11
     * The model we're generating a link for
12
     *
13
     * @var object
14
     */
15
    protected $model;
16
17
    /**
18
     * Options array
19
     * There's no concrete implementation or use case for this
20
     * do with it as you wish
21
     *
22
     * @var array
23
     */
24
    protected $options;
25
26
    /**
27
     * Holds the HTML attribute object
28
     *
29
     * @var \Oddvalue\LinkBuilder\HtmlAttributes
30
     */
31
    protected $attributes;
32
33
    /**
34
     * The attribute on the model from which the link href is derived
35
     *
36
     * @var string
37
     */
38
    protected $hrefAttribute = 'slug';
39
40
    /**
41
     * The attribute on the model to use as the link text
42
     *
43
     * @var string
44
     */
45
    protected $labelAttribute = 'name';
46
47
    /**
48
     * Instantiate the generator with the linkable model
49
     *
50
     * @param Linkable $model
51
     * @param array $options
52
     */
53 24
    public function __construct(Linkable $model, array $options = [])
54
    {
55 24
        $this->model = $model;
56
57 24
        if (key_exists('class', $options)) {
58 3
            $options['attributes'] = @$options['attributes'] ?: [];
59 3
            $options['attributes']['class'] = $options['class'];
60 3
            unset($options['class']);
61
        }
62
63 24
        $attributes = [];
64 24
        if (key_exists('attributes', $options)) {
65 6
            $attributes = $options['attributes'];
66 6
            unset($options['attributes']);
67
        }
68 24
        $this->setAttributes($attributes);
69
70 24
        $this->options = $options;
71 24
    }
72
73
    /**
74
     * Get the link href for a given model
75
     *
76
     * @param $model
77
     * @return string
78
     */
79 15
    public function href() : string
80
    {
81 15
        return '/' . $this->model->{$this->hrefAttribute};
82
    }
83
84
    /**
85
     * Get the link text for a given model
86
     *
87
     * @param $model
88
     * @param array $options
89
     * @return string
90
     */
91 15
    public function label() : string
92
    {
93 15
        return (string) $this->model->{$this->labelAttribute};
94
    }
95
96
    /**
97
     * Get the HtmlAttributes object
98
     *
99
     * @return \Oddvalue\LinkBuilder\HtmlAttributes
100
     */
101 18
    public function getAttributes() : HtmlAttributes
102
    {
103 18
        return $this->attributes;
104
    }
105
106
    /**
107
     * Set the link attributes
108
     *
109
     * @param array $attributes
110
     * @return self
111
     */
112 24
    public function setAttributes(array $attributes)
113
    {
114 24
        $this->attributes = HtmlAttributes::make($attributes);
115 24
        return $this;
116
    }
117
118
    /**
119
     * Generate an HTML link for the model
120
     *
121
     * @return string
122
     */
123 12
    public function toHtml()
124
    {
125
        return <<<HTML
126 12
<a href="{$this->href()}"{$this->getAttributes()}>{$this->label()}</a>
127
HTML;
128
    }
129
130
    /**
131
     * Cast the generator to a string
132
     *
133
     * @return string
134
     */
135 6
    public function __toString() : string
136
    {
137 6
        return $this->toHtml();
138
    }
139
}
140