|
1
|
|
|
<?php |
|
2
|
|
|
namespace Malezha\Menu\Entity; |
|
3
|
|
|
|
|
4
|
|
|
use Illuminate\Http\Request; |
|
5
|
|
|
use Malezha\Menu\Contracts\Attributes as AttributesContract; |
|
6
|
|
|
use Malezha\Menu\Contracts\Builder as BuilderContract; |
|
7
|
|
|
use Malezha\Menu\Contracts\Link as LinkContract; |
|
8
|
|
|
use Malezha\Menu\Contracts\Item as ItemContract; |
|
9
|
|
|
use Malezha\Menu\Traits\DisplayRule; |
|
10
|
|
|
use Malezha\Menu\Traits\HasAttributes; |
|
11
|
|
|
use Malezha\Menu\Support\MergeAttributes; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class Item |
|
15
|
|
|
* @package Malezha\Menu\Entity |
|
16
|
|
|
*/ |
|
17
|
|
|
class Item implements ItemContract |
|
18
|
|
|
{ |
|
19
|
|
|
use HasAttributes, DisplayRule; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var LinkContract |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $link; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var BuilderContract |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $builder; |
|
30
|
|
|
|
|
31
|
10 |
|
/** |
|
32
|
|
|
* @var Request |
|
33
|
10 |
|
*/ |
|
34
|
10 |
|
protected $request; |
|
35
|
10 |
|
|
|
36
|
10 |
|
/** |
|
37
|
10 |
|
* Item constructor. |
|
38
|
|
|
* @param BuilderContract $builder |
|
39
|
|
|
* @param AttributesContract $attributes |
|
40
|
|
|
* @param LinkContract $link |
|
41
|
|
|
* @param Request $request |
|
42
|
5 |
|
*/ |
|
43
|
|
|
public function __construct(BuilderContract $builder, AttributesContract $attributes, |
|
44
|
5 |
|
LinkContract $link, Request $request) |
|
|
|
|
|
|
45
|
|
|
{ |
|
46
|
|
|
$this->builder = $builder; |
|
47
|
|
|
$this->attributes = $attributes; |
|
48
|
|
|
$this->link = $link; |
|
49
|
|
|
$this->request = $request; |
|
50
|
|
|
} |
|
51
|
3 |
|
|
|
52
|
|
|
/** |
|
53
|
3 |
|
* @return Link |
|
54
|
3 |
|
*/ |
|
55
|
3 |
|
public function getLink() |
|
56
|
|
|
{ |
|
57
|
3 |
|
return $this->link; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param array $attributes |
|
62
|
|
|
* @return string |
|
63
|
3 |
|
*/ |
|
64
|
|
|
public function buildAttributes($attributes = []) |
|
65
|
3 |
|
{ |
|
66
|
3 |
|
$attributes = $this->isActive() ? |
|
67
|
|
|
(new MergeAttributes($this->builder->activeAttributes()->all(), $attributes))->merge() : |
|
68
|
3 |
|
$attributes; |
|
69
|
|
|
|
|
70
|
|
|
return $this->attributes->build($attributes); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return bool |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function isActive() |
|
77
|
|
|
{ |
|
78
|
|
|
$currentUrl = $this->request->url(); |
|
79
|
|
|
$url = url($this->getLink()->getUrl()); |
|
80
|
|
|
|
|
81
|
|
|
return $this->isUrlEqual($url, $currentUrl); |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Check is two url equal |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $first |
|
88
|
|
|
* @param string $second |
|
89
|
|
|
* @return bool |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function isUrlEqual($first, $second) |
|
92
|
|
|
{ |
|
93
|
|
|
$uriForTrim = [ |
|
94
|
|
|
'#', |
|
95
|
|
|
'/index', |
|
96
|
|
|
'/' |
|
97
|
|
|
]; |
|
98
|
|
|
|
|
99
|
|
|
foreach ($uriForTrim as $trim) { |
|
100
|
|
|
$first = rtrim($first, $trim); |
|
101
|
|
|
$second = rtrim($second, $trim); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $first == $second; |
|
105
|
|
|
} |
|
106
|
|
|
} |