|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Malezha\Menu\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use Malezha\Menu\Traits\DisplayRule; |
|
6
|
|
|
use Malezha\Menu\Traits\HasAttributes; |
|
7
|
|
|
use Malezha\Menu\Traits\IsUrlEqual; |
|
8
|
|
|
|
|
9
|
|
|
class Item |
|
10
|
|
|
{ |
|
11
|
|
|
use HasAttributes, DisplayRule, IsUrlEqual; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var Link |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $link; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var Builder |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $builder; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param Builder $builder |
|
25
|
|
|
* @param string $name |
|
26
|
|
|
* @param array $attributes |
|
27
|
|
|
* @param string $title |
|
28
|
|
|
* @param string $url |
|
29
|
|
|
* @param array $linkAttributes |
|
30
|
|
|
*/ |
|
31
|
10 |
|
function __construct(Builder $builder, $name, $attributes = [], $title = '', $url = '#', $linkAttributes = []) |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
10 |
|
$title = empty($title) ? $name : $title; |
|
34
|
10 |
|
$this->builder = $builder; |
|
35
|
10 |
|
$this->attributes = new Attributes($attributes); |
|
36
|
10 |
|
$this->link = new Link($title, $url, $linkAttributes); |
|
37
|
10 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return Link |
|
41
|
|
|
*/ |
|
42
|
5 |
|
public function getLink() |
|
43
|
|
|
{ |
|
44
|
5 |
|
return $this->link; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param array $attributes |
|
49
|
|
|
* @return string |
|
50
|
|
|
*/ |
|
51
|
3 |
|
public function buildAttributes($attributes = []) |
|
52
|
|
|
{ |
|
53
|
3 |
|
$attributes = $this->isActive() ? |
|
54
|
3 |
|
Attributes::mergeArrayValues($this->builder->activeAttributes()->all(), $attributes) : |
|
55
|
3 |
|
$attributes; |
|
56
|
|
|
|
|
57
|
3 |
|
return $this->attributes->build($attributes); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return bool |
|
62
|
|
|
*/ |
|
63
|
3 |
|
public function isActive() |
|
64
|
|
|
{ |
|
65
|
3 |
|
$currentUrl = app('request')->url(); |
|
66
|
3 |
|
$url = url($this->getLink()->getUrl()); |
|
67
|
|
|
|
|
68
|
3 |
|
return $this->isUrlEqual($url, $currentUrl); |
|
69
|
|
|
} |
|
70
|
|
|
} |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.