1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Malezha\Menu\Entity; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
|
7
|
|
|
class Attributes |
8
|
|
|
{ |
9
|
|
|
protected $list; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @param array $attributes |
13
|
|
|
*/ |
14
|
24 |
|
function __construct(array $attributes) |
|
|
|
|
15
|
|
|
{ |
16
|
24 |
|
$this->list = new Collection($attributes); |
17
|
24 |
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param string $name |
21
|
|
|
* @param mixed $default |
22
|
|
|
* @return mixed |
23
|
|
|
*/ |
24
|
4 |
|
public function get($name, $default = null) |
25
|
|
|
{ |
26
|
4 |
|
return $this->list->get($name, $default); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param array $attributes |
31
|
|
|
* @return $this |
32
|
|
|
*/ |
33
|
2 |
|
public function set(array $attributes) |
34
|
|
|
{ |
35
|
2 |
|
$this->list = new Collection($attributes); |
36
|
|
|
|
37
|
2 |
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
8 |
|
public function all() |
44
|
|
|
{ |
45
|
8 |
|
return $this->list->all(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $name |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
1 |
|
public function has($name) |
53
|
|
|
{ |
54
|
1 |
|
return $this->list->has($name); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $name |
59
|
|
|
*/ |
60
|
1 |
|
public function forget($name) |
61
|
|
|
{ |
62
|
1 |
|
$this->list->forget($name); |
63
|
1 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param array $attributes |
67
|
|
|
* @return $this |
68
|
|
|
*/ |
69
|
2 |
|
public function push(array $attributes) |
70
|
|
|
{ |
71
|
2 |
|
$this->list = $this->list->merge($attributes); |
72
|
|
|
|
73
|
2 |
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $name |
78
|
|
|
* @param mixed $value |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
1 |
|
public function put($name, $value) |
82
|
|
|
{ |
83
|
1 |
|
$this->list->put($name, $value); |
84
|
|
|
|
85
|
1 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param array $attributes |
90
|
|
|
* @return $this |
91
|
|
|
*/ |
92
|
1 |
|
public function merge(array $attributes) |
93
|
|
|
{ |
94
|
1 |
|
$this->set(self::mergeArrayValues($this->list->toArray(), $attributes)); |
95
|
|
|
|
96
|
1 |
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return array|bool |
101
|
|
|
*/ |
102
|
6 |
|
public static function mergeArrayValues() |
103
|
|
|
{ |
104
|
6 |
|
if (func_num_args() <= 1) { |
105
|
1 |
|
throw new \RuntimeException("Must has min two parameters."); |
106
|
|
|
} |
107
|
|
|
|
108
|
5 |
|
$arrays = func_get_args(); |
109
|
5 |
|
$keys = []; |
110
|
|
|
|
111
|
5 |
|
foreach ($arrays as $array) { |
112
|
5 |
|
$keys = array_merge($keys, array_keys($array)); |
113
|
5 |
|
} |
114
|
|
|
|
115
|
5 |
|
$keys = array_unique($keys); |
116
|
|
|
|
117
|
5 |
|
$merged = array_fill_keys($keys, null); |
118
|
|
|
|
119
|
5 |
|
foreach ($arrays as $array) { |
120
|
5 |
|
foreach ($keys as $key) { |
121
|
4 |
|
if (array_key_exists($key, $array)) { |
122
|
4 |
|
$merged[$key] = self::mergeValues($merged[$key], $array[$key]); |
123
|
4 |
|
} |
124
|
5 |
|
} |
125
|
5 |
|
} |
126
|
|
|
|
127
|
5 |
|
return $merged; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $valueOne |
132
|
|
|
* @param string $valueTwo |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
4 |
|
protected static function mergeValues($valueOne, $valueTwo) |
136
|
|
|
{ |
137
|
4 |
|
if (is_null($valueOne)) { |
138
|
4 |
|
return $valueTwo; |
139
|
|
|
} |
140
|
|
|
|
141
|
2 |
|
$valueOne = explode(' ', $valueOne); |
142
|
2 |
|
$valueTwo = explode(' ', $valueTwo); |
143
|
|
|
|
144
|
2 |
|
$merged = array_merge($valueOne, $valueTwo); |
145
|
|
|
|
146
|
2 |
|
return trim(implode(' ', $merged)); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param array $attributes |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
4 |
|
public function build($attributes = []) |
154
|
|
|
{ |
155
|
4 |
|
$attributes = $this->mergeArrayValues($this->all(), $attributes); |
156
|
|
|
|
157
|
4 |
|
$html = (count($attributes) > 0) ? ' ' : ''; |
158
|
|
|
|
159
|
4 |
|
foreach ($attributes as $key => $value) { |
160
|
3 |
|
$html .= $key . '="' . $value . '" '; |
161
|
4 |
|
} |
162
|
|
|
|
163
|
4 |
|
return rtrim($html); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
1 |
|
function __toString() |
|
|
|
|
170
|
|
|
{ |
171
|
1 |
|
return $this->build(); |
172
|
|
|
} |
173
|
|
|
} |
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.