|
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
|
|
|
public function __construct(Linkable $model, array $options = []) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->model = $model; |
|
56
|
|
|
|
|
57
|
|
|
if (key_exists('class', $options)) { |
|
58
|
|
|
$options['attributes'] = @$options['attributes'] ?: []; |
|
59
|
|
|
$options['attributes']['class'] = $options['class']; |
|
60
|
|
|
unset($options['class']); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$attributes = []; |
|
64
|
|
|
if (key_exists('attributes', $options)) { |
|
65
|
|
|
$attributes = $options['attributes']; |
|
66
|
|
|
unset($options['attributes']); |
|
67
|
|
|
} |
|
68
|
|
|
$this->setAttributes($attributes); |
|
69
|
|
|
|
|
70
|
|
|
$this->options = $options; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get the link href for a given model |
|
75
|
|
|
* |
|
76
|
|
|
* @param $model |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
|
|
public function href() : string |
|
80
|
|
|
{ |
|
81
|
|
|
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
|
|
|
public function label() : string |
|
92
|
|
|
{ |
|
93
|
|
|
return (string) $this->model->{$this->labelAttribute}; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Get the HtmlAttributes object |
|
98
|
|
|
* |
|
99
|
|
|
* @return \Oddvalue\LinkBuilder\HtmlAttributes |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getAttributes() : HtmlAttributes |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->attributes; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Set the link attributes |
|
108
|
|
|
* |
|
109
|
|
|
* @param array $attributes |
|
110
|
|
|
* @return self |
|
111
|
|
|
*/ |
|
112
|
|
|
public function setAttributes(array $attributes) |
|
113
|
|
|
{ |
|
114
|
|
|
$this->attributes = HtmlAttributes::make($attributes); |
|
115
|
|
|
return $this; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Generate an HTML link for the model |
|
120
|
|
|
* |
|
121
|
|
|
* @return string |
|
122
|
|
|
*/ |
|
123
|
|
|
public function toHtml() |
|
124
|
|
|
{ |
|
125
|
|
|
return <<<HTML |
|
126
|
|
|
<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
|
|
|
public function __toString() : string |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->toHtml(); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|