1
|
|
|
<?php |
2
|
|
|
namespace Malezha\Menu\Entity; |
3
|
|
|
|
4
|
|
|
use Malezha\Menu\Support\MergeAttributes; |
5
|
|
|
|
6
|
|
|
class Attributes |
7
|
|
|
{ |
8
|
|
|
protected $list; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @param array $attributes |
12
|
|
|
*/ |
13
|
|
|
public function __construct(array $attributes) |
14
|
24 |
|
{ |
15
|
|
|
$this->list = $attributes; |
16
|
24 |
|
} |
17
|
24 |
|
|
18
|
|
|
/** |
19
|
|
|
* @param string $name |
20
|
|
|
* @param mixed $default |
21
|
|
|
* @return mixed |
22
|
|
|
*/ |
23
|
|
|
public function get($name, $default = null) |
24
|
4 |
|
{ |
25
|
|
|
if (array_key_exists($name, $this->list)) { |
26
|
4 |
|
return $this->list[$name]; |
27
|
3 |
|
} |
28
|
|
|
return $default; |
29
|
1 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param array $attributes |
33
|
|
|
* @return $this |
34
|
|
|
*/ |
35
|
|
|
public function set(array $attributes) |
36
|
2 |
|
{ |
37
|
|
|
$this->list = $attributes; |
38
|
2 |
|
|
39
|
|
|
return $this; |
40
|
2 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
public function all() |
46
|
8 |
|
{ |
47
|
|
|
return $this->list; |
48
|
8 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $name |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
|
|
public function has($name) |
55
|
2 |
|
{ |
56
|
|
|
return array_key_exists($name, $this->list); |
57
|
2 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $name |
61
|
|
|
*/ |
62
|
|
|
public function forget($name) |
63
|
1 |
|
{ |
64
|
|
|
if ($this->has($name)) { |
65
|
1 |
|
unset($this->list[$name]); |
66
|
1 |
|
} |
67
|
1 |
|
} |
68
|
1 |
|
|
69
|
|
|
/** |
70
|
|
|
* @param array $attributes |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
|
|
public function push(array $attributes) |
74
|
2 |
|
{ |
75
|
|
|
$this->list = array_merge($this->list, $attributes); |
76
|
2 |
|
|
77
|
|
|
return $this; |
78
|
2 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $name |
82
|
|
|
* @param mixed $value |
83
|
|
|
* @return $this |
84
|
|
|
*/ |
85
|
|
|
public function put($name, $value) |
86
|
1 |
|
{ |
87
|
|
|
$this->list[$name] = $value; |
88
|
1 |
|
|
89
|
|
|
return $this; |
90
|
1 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param array $attributes |
94
|
|
|
* @return $this |
95
|
|
|
*/ |
96
|
|
|
public function merge(array $attributes) |
97
|
1 |
|
{ |
98
|
|
|
$this->set((new MergeAttributes($this->all(), $attributes))->merge()); |
99
|
1 |
|
|
100
|
|
|
return $this; |
101
|
1 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param array $attributes |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
6 |
|
public function build($attributes = []) |
108
|
|
|
{ |
109
|
6 |
|
$attributes = (new MergeAttributes($this->all(), $attributes))->merge(); |
110
|
1 |
|
|
111
|
|
|
$html = (count($attributes) > 0) ? ' ' : ''; |
112
|
|
|
|
113
|
5 |
|
foreach ($attributes as $key => $value) { |
114
|
5 |
|
$html .= $key . '="' . $value . '" '; |
115
|
|
|
} |
116
|
5 |
|
|
117
|
5 |
|
return rtrim($html); |
118
|
5 |
|
} |
119
|
|
|
|
120
|
5 |
|
/** |
121
|
|
|
* @return string |
122
|
5 |
|
*/ |
123
|
|
|
public function __toString() |
124
|
5 |
|
{ |
125
|
5 |
|
return $this->build(); |
126
|
|
|
} |
127
|
|
|
} |