5
5
*/
6
6
namespace Magento \Backend \Model ;
7
7
8
+ use Magento \Backend \Model \Menu \Item ;
9
+ use Magento \Backend \Model \Menu \Item \Factory ;
10
+ use Magento \Framework \App \ObjectManager ;
11
+ use Magento \Framework \Serialize \SerializerInterface ;
12
+ use Psr \Log \LoggerInterface ;
13
+
8
14
/**
9
15
* Backend menu model
10
16
*/
@@ -18,33 +24,52 @@ class Menu extends \ArrayObject
18
24
protected $ _path = '' ;
19
25
20
26
/**
21
- * @var \Psr\Log\ LoggerInterface
27
+ * @var LoggerInterface
22
28
*/
23
29
protected $ _logger ;
24
30
25
31
/**
26
- * @param \Psr\Log\LoggerInterface $logger
32
+ * @var Factory
33
+ */
34
+ private $ menuItemFactory ;
35
+
36
+ /**
37
+ * @var SerializerInterface
38
+ */
39
+ private $ serializer ;
40
+
41
+ /**
42
+ * @param LoggerInterface $logger
27
43
* @param string $pathInMenuStructure
44
+ * @param Factory $menuItemFactory
45
+ * @param SerializerInterface $serializer
28
46
*/
29
- public function __construct (\Psr \Log \LoggerInterface $ logger , $ pathInMenuStructure = '' )
30
- {
47
+ public function __construct (
48
+ LoggerInterface $ logger ,
49
+ $ pathInMenuStructure = '' ,
50
+ Factory $ menuItemFactory = null ,
51
+ SerializerInterface $ serializer = null
52
+ ) {
31
53
if ($ pathInMenuStructure ) {
32
54
$ this ->_path = $ pathInMenuStructure . '/ ' ;
33
55
}
34
56
$ this ->_logger = $ logger ;
35
57
$ this ->setIteratorClass (\Magento \Backend \Model \Menu \Iterator::class);
58
+ $ this ->menuItemFactory = $ menuItemFactory ?: ObjectManager::getInstance ()
59
+ ->create (Factory::class);
60
+ $ this ->serializer = $ serializer ?: ObjectManager::getInstance ()->create (SerializerInterface::class);
36
61
}
37
62
38
63
/**
39
64
* Add child to menu item
40
65
*
41
- * @param \Magento\Backend\Model\Menu\ Item $item
66
+ * @param Item $item
42
67
* @param string $parentId
43
68
* @param int $index
44
69
* @return void
45
70
* @throws \InvalidArgumentException
46
71
*/
47
- public function add (\ Magento \ Backend \ Model \ Menu \ Item $ item , $ parentId = null , $ index = null )
72
+ public function add (Item $ item , $ parentId = null , $ index = null )
48
73
{
49
74
if ($ parentId !== null ) {
50
75
$ parentItem = $ this ->get ($ parentId );
@@ -69,13 +94,13 @@ public function add(\Magento\Backend\Model\Menu\Item $item, $parentId = null, $i
69
94
* Retrieve menu item by id
70
95
*
71
96
* @param string $itemId
72
- * @return \Magento\Backend\Model\Menu\ Item|null
97
+ * @return Item|null
73
98
*/
74
99
public function get ($ itemId )
75
100
{
76
101
$ result = null ;
102
+ /** @var Item $item */
77
103
foreach ($ this as $ item ) {
78
- /** @var $item \Magento\Backend\Model\Menu\Item */
79
104
if ($ item ->getId () == $ itemId ) {
80
105
$ result = $ item ;
81
106
break ;
@@ -116,8 +141,8 @@ public function move($itemId, $toItemId, $sortIndex = null)
116
141
public function remove ($ itemId )
117
142
{
118
143
$ result = false ;
144
+ /** @var Item $item */
119
145
foreach ($ this as $ key => $ item ) {
120
- /** @var $item \Magento\Backend\Model\Menu\Item */
121
146
if ($ item ->getId () == $ itemId ) {
122
147
unset($ this [$ key ]);
123
148
$ result = true ;
@@ -144,8 +169,8 @@ public function remove($itemId)
144
169
public function reorder ($ itemId , $ position )
145
170
{
146
171
$ result = false ;
172
+ /** @var Item $item */
147
173
foreach ($ this as $ key => $ item ) {
148
- /** @var $item \Magento\Backend\Model\Menu\Item */
149
174
if ($ item ->getId () == $ itemId ) {
150
175
unset($ this [$ key ]);
151
176
$ this ->add ($ item , null , $ position );
@@ -161,23 +186,23 @@ public function reorder($itemId, $position)
161
186
/**
162
187
* Check whether provided item is last in list
163
188
*
164
- * @param \Magento\Backend\Model\Menu\ Item $item
189
+ * @param Item $item
165
190
* @return bool
166
191
*/
167
- public function isLast (\ Magento \ Backend \ Model \ Menu \ Item $ item )
192
+ public function isLast (Item $ item )
168
193
{
169
194
return $ this ->offsetGet (max (array_keys ($ this ->getArrayCopy ())))->getId () == $ item ->getId ();
170
195
}
171
196
172
197
/**
173
198
* Find first menu item that user is able to access
174
199
*
175
- * @return \Magento\Backend\Model\Menu\ Item|null
200
+ * @return Item|null
176
201
*/
177
202
public function getFirstAvailable ()
178
203
{
179
204
$ result = null ;
180
- /** @var $item \Magento\Backend\Model\Menu\Item */
205
+ /** @var Item $item */
181
206
foreach ($ this as $ item ) {
182
207
if ($ item ->isAllowed () && !$ item ->isDisabled ()) {
183
208
if ($ item ->hasChildren ()) {
@@ -198,7 +223,7 @@ public function getFirstAvailable()
198
223
* Get parent items by item id
199
224
*
200
225
* @param string $itemId
201
- * @return \Magento\Backend\Model\Menu\ Item[]
226
+ * @return Item[]
202
227
*/
203
228
public function getParentItems ($ itemId )
204
229
{
@@ -217,8 +242,8 @@ public function getParentItems($itemId)
217
242
*/
218
243
protected function _findParentItems ($ menu , $ itemId , &$ parents )
219
244
{
245
+ /** @var Item $item */
220
246
foreach ($ menu as $ item ) {
221
- /** @var $item \Magento\Backend\Model\Menu\Item */
222
247
if ($ item ->getId () == $ itemId ) {
223
248
return true ;
224
249
}
@@ -241,8 +266,51 @@ public function serialize()
241
266
{
242
267
$ logger = $ this ->_logger ;
243
268
unset($ this ->_logger );
244
- $ result = parent :: serialize ();
269
+ $ result = $ this -> serializer -> serialize ($ this -> toArray () );
245
270
$ this ->_logger = $ logger ;
246
271
return $ result ;
247
272
}
273
+
274
+ /**
275
+ * Get menu data represented as an array
276
+ *
277
+ * @return array
278
+ */
279
+ public function toArray ()
280
+ {
281
+ $ data = [];
282
+ foreach ($ this as $ item ) {
283
+ $ data [] = $ item ->toArray ();
284
+ }
285
+ return $ data ;
286
+ }
287
+
288
+ /**
289
+ * Unserialize menu
290
+ *
291
+ * @param string $serialized
292
+ * @return void
293
+ */
294
+ public function unserialize ($ serialized )
295
+ {
296
+ $ data = $ this ->serializer ->unserialize ($ serialized );
297
+ $ this ->populateFromArray ($ data );
298
+ }
299
+
300
+ /**
301
+ * Populate the menu with data from array
302
+ *
303
+ * @param array $data
304
+ * @return void
305
+ */
306
+ public function populateFromArray (array $ data )
307
+ {
308
+ $ items = [];
309
+ foreach ($ data as $ itemData ) {
310
+ $ item = $ this ->menuItemFactory ->create ();
311
+ $ item ->populateFromArray ($ itemData );
312
+ $ items [] = $ item ;
313
+ }
314
+ $ this ->exchangeArray ($ items );
315
+ }
248
316
}
0 commit comments