3
3
* Copyright © 2015 Magento. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
-
7
- // @codingStandardsIgnoreFile
8
-
9
6
namespace Magento \Widget \Model ;
10
7
11
8
/**
@@ -16,39 +13,37 @@ class Widget
16
13
/**
17
14
* @var \Magento\Widget\Model\Config\Data
18
15
*/
19
- protected $ _dataStorage ;
16
+ protected $ dataStorage ;
20
17
21
18
/**
22
19
* @var \Magento\Framework\App\Cache\Type\Config
23
20
*/
24
- protected $ _configCacheType ;
21
+ protected $ configCacheType ;
25
22
26
23
/**
27
24
* @var \Magento\Framework\View\Asset\Repository
28
25
*/
29
- protected $ _assetRepo ;
26
+ protected $ assetRepo ;
30
27
31
28
/**
32
29
* @var \Magento\Framework\View\Asset\Source
33
30
*/
34
- protected $ _assetSource ;
31
+ protected $ assetSource ;
35
32
36
33
/**
37
34
* @var \Magento\Framework\View\FileSystem
38
35
*/
39
- protected $ _viewFileSystem ;
36
+ protected $ viewFileSystem ;
40
37
41
38
/**
42
- * Core data
43
- *
44
39
* @var \Magento\Framework\Escaper
45
40
*/
46
- protected $ _escaper ;
41
+ protected $ escaper ;
47
42
48
43
/**
49
44
* @var array
50
45
*/
51
- protected $ _widgetsArray = [];
46
+ protected $ widgetsArray = [];
52
47
53
48
/**
54
49
* @var \Magento\Widget\Helper\Conditions
@@ -71,11 +66,11 @@ public function __construct(
71
66
\Magento \Framework \View \FileSystem $ viewFileSystem ,
72
67
\Magento \Widget \Helper \Conditions $ conditionsHelper
73
68
) {
74
- $ this ->_escaper = $ escaper ;
75
- $ this ->_dataStorage = $ dataStorage ;
76
- $ this ->_assetRepo = $ assetRepo ;
77
- $ this ->_assetSource = $ assetSource ;
78
- $ this ->_viewFileSystem = $ viewFileSystem ;
69
+ $ this ->escaper = $ escaper ;
70
+ $ this ->dataStorage = $ dataStorage ;
71
+ $ this ->assetRepo = $ assetRepo ;
72
+ $ this ->assetSource = $ assetSource ;
73
+ $ this ->viewFileSystem = $ viewFileSystem ;
79
74
$ this ->conditionsHelper = $ conditionsHelper ;
80
75
}
81
76
@@ -118,8 +113,6 @@ public function getConfigAsXml($type)
118
113
*
119
114
* @param string $type Widget type
120
115
* @return \Magento\Framework\Object
121
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
122
- * @SuppressWarnings(PHPMD.NPathComplexity)
123
116
*/
124
117
public function getConfigAsObject ($ type )
125
118
{
@@ -129,54 +122,92 @@ public function getConfigAsObject($type)
129
122
if ($ widget === null ) {
130
123
return $ object ;
131
124
}
132
- $ widget = $ this ->_getAsCanonicalArray ($ widget );
125
+ $ widget = $ this ->getAsCanonicalArray ($ widget );
133
126
134
127
// Save all nodes to object data
135
128
$ object ->setType ($ type );
136
129
$ object ->setData ($ widget );
137
130
138
131
// Correct widget parameters and convert its data to objects
132
+ $ newParams = $ this ->prepareWidgetParameters ($ object );
133
+ $ object ->setData ('parameters ' , $ newParams );
134
+
135
+ return $ object ;
136
+ }
137
+
138
+ /**
139
+ * Prepare widget parameters
140
+ *
141
+ * @param \Magento\Framework\Object $object
142
+ * @return array
143
+ */
144
+ protected function prepareWidgetParameters (\Magento \Framework \Object $ object )
145
+ {
139
146
$ params = $ object ->getData ('parameters ' );
140
147
$ newParams = [];
141
148
if (is_array ($ params )) {
142
149
$ sortOrder = 0 ;
143
150
foreach ($ params as $ key => $ data ) {
144
151
if (is_array ($ data )) {
145
- $ data ['key ' ] = $ key ;
146
- $ data ['sort_order ' ] = isset ($ data ['sort_order ' ]) ? (int )$ data ['sort_order ' ] : $ sortOrder ;
147
-
148
- // prepare values (for drop-dawns) specified directly in configuration
149
- $ values = [];
150
- if (isset ($ data ['values ' ]) && is_array ($ data ['values ' ])) {
151
- foreach ($ data ['values ' ] as $ value ) {
152
- if (isset ($ value ['label ' ]) && isset ($ value ['value ' ])) {
153
- $ values [] = $ value ;
154
- }
155
- }
156
- }
157
- $ data ['values ' ] = $ values ;
158
-
159
- // prepare helper block object
160
- if (isset ($ data ['helper_block ' ])) {
161
- $ helper = new \Magento \Framework \Object ();
162
- if (isset ($ data ['helper_block ' ]['data ' ]) && is_array ($ data ['helper_block ' ]['data ' ])) {
163
- $ helper ->addData ($ data ['helper_block ' ]['data ' ]);
164
- }
165
- if (isset ($ data ['helper_block ' ]['type ' ])) {
166
- $ helper ->setType ($ data ['helper_block ' ]['type ' ]);
167
- }
168
- $ data ['helper_block ' ] = $ helper ;
169
- }
152
+ $ data = $ this ->prepareDropDownValues ($ data , $ key , $ sortOrder );
153
+ $ data = $ this ->prepareHelperBlock ($ data );
170
154
171
155
$ newParams [$ key ] = new \Magento \Framework \Object ($ data );
172
156
$ sortOrder ++;
173
157
}
174
158
}
175
159
}
176
- uasort ($ newParams , [$ this , '_sortParameters ' ]);
177
- $ object ->setData ('parameters ' , $ newParams );
160
+ uasort ($ newParams , [$ this , 'sortParameters ' ]);
178
161
179
- return $ object ;
162
+ return $ newParams ;
163
+ }
164
+
165
+ /**
166
+ * Prepare drop-down values
167
+ *
168
+ * @param array $data
169
+ * @param string $key
170
+ * @param int $sortOrder
171
+ * @return array
172
+ */
173
+ protected function prepareDropDownValues (array $ data , $ key , $ sortOrder )
174
+ {
175
+ $ data ['key ' ] = $ key ;
176
+ $ data ['sort_order ' ] = isset ($ data ['sort_order ' ]) ? (int )$ data ['sort_order ' ] : $ sortOrder ;
177
+
178
+ $ values = [];
179
+ if (isset ($ data ['values ' ]) && is_array ($ data ['values ' ])) {
180
+ foreach ($ data ['values ' ] as $ value ) {
181
+ if (isset ($ value ['label ' ]) && isset ($ value ['value ' ])) {
182
+ $ values [] = $ value ;
183
+ }
184
+ }
185
+ }
186
+ $ data ['values ' ] = $ values ;
187
+
188
+ return $ data ;
189
+ }
190
+
191
+ /**
192
+ * Prepare helper block
193
+ *
194
+ * @param array $data
195
+ * @return array
196
+ */
197
+ protected function prepareHelperBlock (array $ data )
198
+ {
199
+ if (isset ($ data ['helper_block ' ])) {
200
+ $ helper = new \Magento \Framework \Object ();
201
+ if (isset ($ data ['helper_block ' ]['data ' ]) && is_array ($ data ['helper_block ' ]['data ' ])) {
202
+ $ helper ->addData ($ data ['helper_block ' ]['data ' ]);
203
+ }
204
+ if (isset ($ data ['helper_block ' ]['type ' ])) {
205
+ $ helper ->setType ($ data ['helper_block ' ]['type ' ]);
206
+ }
207
+ $ data ['helper_block ' ] = $ helper ;
208
+ }
209
+
210
+ return $ data ;
180
211
}
181
212
182
213
/**
@@ -188,7 +219,7 @@ public function getConfigAsObject($type)
188
219
*/
189
220
public function getWidgets ($ filters = [])
190
221
{
191
- $ widgets = $ this ->_dataStorage ->get ();
222
+ $ widgets = $ this ->dataStorage ->get ();
192
223
$ result = $ widgets ;
193
224
194
225
// filter widgets by params
@@ -219,7 +250,7 @@ public function getWidgets($filters = [])
219
250
*/
220
251
public function getWidgetsArray ($ filters = [])
221
252
{
222
- if (empty ($ this ->_widgetsArray )) {
253
+ if (empty ($ this ->widgetsArray )) {
223
254
$ result = [];
224
255
foreach ($ this ->getWidgets ($ filters ) as $ code => $ widget ) {
225
256
$ result [$ widget ['name ' ]] = [
@@ -229,10 +260,10 @@ public function getWidgetsArray($filters = [])
229
260
'description ' => __ ((string )$ widget ['description ' ]),
230
261
];
231
262
}
232
- usort ($ result , [$ this , "_sortWidgets " ]);
233
- $ this ->_widgetsArray = $ result ;
263
+ usort ($ result , [$ this , "sortWidgets " ]);
264
+ $ this ->widgetsArray = $ result ;
234
265
}
235
- return $ this ->_widgetsArray ;
266
+ return $ this ->widgetsArray ;
236
267
}
237
268
238
269
/**
@@ -274,9 +305,9 @@ public function getWidgetDeclaration($type, $params = [], $asIs = true)
274
305
275
306
$ html = sprintf (
276
307
'<img id="%s" src="%s" title="%s"> ' ,
277
- $ this ->_idEncode ($ directive ),
308
+ $ this ->idEncode ($ directive ),
278
309
$ this ->getPlaceholderImageUrl ($ type ),
279
- $ this ->_escaper ->escapeUrl ($ directive )
310
+ $ this ->escaper ->escapeUrl ($ directive )
280
311
);
281
312
return $ html ;
282
313
}
@@ -295,13 +326,13 @@ public function getPlaceholderImageUrl($type)
295
326
$ placeholder = (string )$ widget ['placeholder_image ' ];
296
327
}
297
328
if ($ placeholder ) {
298
- $ asset = $ this ->_assetRepo ->createAsset ($ placeholder );
299
- $ placeholder = $ this ->_assetSource ->getFile ($ asset );
329
+ $ asset = $ this ->assetRepo ->createAsset ($ placeholder );
330
+ $ placeholder = $ this ->assetSource ->getFile ($ asset );
300
331
if ($ placeholder ) {
301
332
return $ asset ->getUrl ();
302
333
}
303
334
}
304
- return $ this ->_assetRepo ->getUrl ('Magento_Widget::placeholder.gif ' );
335
+ return $ this ->assetRepo ->getUrl ('Magento_Widget::placeholder.gif ' );
305
336
}
306
337
307
338
/**
@@ -333,7 +364,7 @@ public function getPlaceholderImageUrls()
333
364
* @param array $inputArray
334
365
* @return array
335
366
*/
336
- protected function _getAsCanonicalArray ($ inputArray )
367
+ protected function getAsCanonicalArray ($ inputArray )
337
368
{
338
369
if (array_key_exists ('@ ' , $ inputArray )) {
339
370
unset($ inputArray ['@ ' ]);
@@ -342,7 +373,7 @@ protected function _getAsCanonicalArray($inputArray)
342
373
if (!is_array ($ value )) {
343
374
continue ;
344
375
}
345
- $ inputArray [$ key ] = $ this ->_getAsCanonicalArray ($ value );
376
+ $ inputArray [$ key ] = $ this ->getAsCanonicalArray ($ value );
346
377
}
347
378
return $ inputArray ;
348
379
}
@@ -353,7 +384,7 @@ protected function _getAsCanonicalArray($inputArray)
353
384
* @param string $string
354
385
* @return string
355
386
*/
356
- protected function _idEncode ($ string )
387
+ protected function idEncode ($ string )
357
388
{
358
389
return strtr (base64_encode ($ string ), '+/= ' , ':_- ' );
359
390
}
@@ -365,7 +396,7 @@ protected function _idEncode($string)
365
396
* @param array $secondElement
366
397
* @return bool
367
398
*/
368
- protected function _sortWidgets ($ firstElement , $ secondElement )
399
+ protected function sortWidgets ($ firstElement , $ secondElement )
369
400
{
370
401
return strcmp ($ firstElement ["name " ], $ secondElement ["name " ]);
371
402
}
@@ -377,7 +408,7 @@ protected function _sortWidgets($firstElement, $secondElement)
377
408
* @param \Magento\Framework\Object $secondElement
378
409
* @return int
379
410
*/
380
- protected function _sortParameters ($ firstElement , $ secondElement )
411
+ protected function sortParameters ($ firstElement , $ secondElement )
381
412
{
382
413
$ aOrder = (int )$ firstElement ->getData ('sort_order ' );
383
414
$ bOrder = (int )$ secondElement ->getData ('sort_order ' );
0 commit comments