1
|
|
|
<?php |
2
|
|
|
namespace Malezha\Menu\Entity; |
3
|
|
|
|
4
|
|
|
use Malezha\Menu\Contracts\Attributes as AttributesContract; |
5
|
|
|
use Malezha\Menu\Support\MergeAttributes; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Attributes |
9
|
|
|
* @package Malezha\Menu\Entity |
10
|
|
|
*/ |
11
|
|
|
class Attributes implements AttributesContract |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
protected $attributes; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param array $attributes |
20
|
|
|
*/ |
21
|
24 |
|
public function __construct(array $attributes) |
22
|
|
|
{ |
23
|
24 |
|
$this->attributes = $attributes; |
24
|
24 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get attribute by name |
28
|
|
|
* |
29
|
|
|
* @param string $name |
30
|
|
|
* @param string|null $default |
31
|
|
|
* @return string|null |
32
|
|
|
*/ |
33
|
4 |
|
public function get($name, $default = null) |
34
|
|
|
{ |
35
|
4 |
|
if (array_key_exists($name, $this->attributes)) { |
36
|
3 |
|
return $this->attributes[$name]; |
37
|
|
|
} |
38
|
1 |
|
return $default; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Set array attributes |
43
|
|
|
* |
44
|
|
|
* @param array $attributes |
45
|
|
|
* @return AttributesContract |
46
|
|
|
*/ |
47
|
2 |
|
public function set(array $attributes) |
48
|
|
|
{ |
49
|
2 |
|
$this->attributes = $attributes; |
50
|
|
|
|
51
|
2 |
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get all attributes |
56
|
|
|
* |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
9 |
|
public function all() |
60
|
|
|
{ |
61
|
9 |
|
return $this->attributes; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Check exits attribute by name |
66
|
|
|
* |
67
|
|
|
* @param string $name |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
2 |
|
public function has($name) |
71
|
|
|
{ |
72
|
2 |
|
return array_key_exists($name, $this->attributes); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Delete attribute by name |
77
|
|
|
* |
78
|
|
|
* @param string $name |
79
|
|
|
*/ |
80
|
1 |
|
public function forget($name) |
81
|
|
|
{ |
82
|
1 |
|
if ($this->has($name)) { |
83
|
1 |
|
unset($this->attributes[$name]); |
84
|
1 |
|
} |
85
|
1 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Set attribute or attributes. |
89
|
|
|
* No merge attributes value. |
90
|
|
|
* |
91
|
|
|
* @param array $attributes |
92
|
|
|
* @return AttributesContract |
93
|
|
|
*/ |
94
|
2 |
|
public function push(array $attributes) |
95
|
|
|
{ |
96
|
2 |
|
$this->attributes = array_merge($this->attributes, $attributes); |
97
|
|
|
|
98
|
2 |
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Set attribute value |
103
|
|
|
* |
104
|
|
|
* @param string $name |
105
|
|
|
* @param string $value |
106
|
|
|
* @return AttributesContract |
107
|
|
|
*/ |
108
|
1 |
|
public function put($name, $value) |
109
|
|
|
{ |
110
|
1 |
|
$this->attributes[$name] = $value; |
111
|
|
|
|
112
|
1 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Merge attributes and merge their values |
117
|
|
|
* |
118
|
|
|
* @param array $attributes |
119
|
|
|
* @return AttributesContract |
120
|
|
|
*/ |
121
|
1 |
|
public function merge(array $attributes) |
122
|
|
|
{ |
123
|
1 |
|
$this->set((new MergeAttributes($this->all(), $attributes))->merge()); |
124
|
|
|
|
125
|
1 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Build attributes html valid string |
130
|
|
|
* |
131
|
|
|
* @param array $attributes |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
5 |
|
public function build($attributes = []) |
135
|
|
|
{ |
136
|
5 |
|
$attributes = (new MergeAttributes($this->all(), $attributes))->merge(); |
137
|
|
|
|
138
|
5 |
|
$html = (count($attributes) > 0) ? ' ' : ''; |
139
|
|
|
|
140
|
5 |
|
foreach ($attributes as $key => $value) { |
141
|
5 |
|
$html .= $key . '="' . $value . '" '; |
142
|
5 |
|
} |
143
|
|
|
|
144
|
5 |
|
return rtrim($html); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
1 |
|
public function __toString() |
151
|
|
|
{ |
152
|
1 |
|
return $this->build(); |
153
|
|
|
} |
154
|
|
|
} |