3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
6
7
7
8
namespace Magento \Catalog \Helper \Product ;
8
9
10
+ use Magento \Catalog \Model \Config ;
11
+ use Magento \Framework \App \Config \ScopeConfigInterface ;
12
+ use Magento \Framework \App \ObjectManager ;
13
+ use Magento \Framework \Registry ;
14
+ use Magento \Store \Model \ScopeInterface ;
15
+
9
16
/**
10
- * Class ProductList
11
- *
12
17
* @api
13
18
* @since 100.0.2
14
19
*/
@@ -18,18 +23,15 @@ class ProductList
18
23
* List mode configuration path
19
24
*/
20
25
const XML_PATH_LIST_MODE = 'catalog/frontend/list_mode ' ;
21
-
22
- const VIEW_MODE_LIST = 'list ' ;
23
- const VIEW_MODE_GRID = 'grid ' ;
24
-
25
26
const DEFAULT_SORT_DIRECTION = 'asc ' ;
27
+
26
28
/**
27
- * @var \Magento\Framework\App\Config\ ScopeConfigInterface
29
+ * @var ScopeConfigInterface
28
30
*/
29
31
protected $ scopeConfig ;
30
32
31
33
/**
32
- * @var \Magento\Framework\ Registry
34
+ * @var Registry
33
35
*/
34
36
private $ coreRegistry ;
35
37
@@ -38,20 +40,18 @@ class ProductList
38
40
*
39
41
* @var array
40
42
*/
41
- protected $ _defaultAvailableLimit = [10 => 10 ,20 => 20 ,50 => 50 ];
43
+ protected $ _defaultAvailableLimit = [10 => 10 , 20 => 20 , 50 => 50 ];
42
44
43
45
/**
44
- * @param \Magento\Framework\App\Config\ ScopeConfigInterface $scopeConfig
45
- * @param \Magento\Framework\ Registry $coreRegistry
46
+ * @param ScopeConfigInterface $scopeConfig
47
+ * @param Registry $coreRegistry
46
48
*/
47
49
public function __construct (
48
- \ Magento \ Framework \ App \ Config \ ScopeConfigInterface $ scopeConfig ,
49
- \ Magento \ Framework \ Registry $ coreRegistry = null
50
+ ScopeConfigInterface $ scopeConfig ,
51
+ Registry $ coreRegistry = null
50
52
) {
51
53
$ this ->scopeConfig = $ scopeConfig ;
52
- $ this ->coreRegistry = $ coreRegistry ?: \Magento \Framework \App \ObjectManager::getInstance ()->get (
53
- \Magento \Framework \Registry::class
54
- );
54
+ $ this ->coreRegistry = $ coreRegistry ?? ObjectManager::getInstance ()->get (Registry::class);
55
55
}
56
56
57
57
/**
@@ -61,31 +61,23 @@ public function __construct(
61
61
*/
62
62
public function getAvailableViewMode ()
63
63
{
64
- $ value = $ this ->scopeConfig ->getValue (
65
- self ::XML_PATH_LIST_MODE ,
66
- \Magento \Store \Model \ScopeInterface::SCOPE_STORE
67
- );
64
+ $ value = $ this ->scopeConfig ->getValue (self ::XML_PATH_LIST_MODE , ScopeInterface::SCOPE_STORE );
65
+
68
66
switch ($ value ) {
69
67
case 'grid ' :
70
- $ availableMode = ['grid ' => __ ('Grid ' )];
71
- break ;
68
+ return ['grid ' => __ ('Grid ' )];
72
69
73
70
case 'list ' :
74
- $ availableMode = ['list ' => __ ('List ' )];
75
- break ;
71
+ return ['list ' => __ ('List ' )];
76
72
77
73
case 'grid-list ' :
78
- $ availableMode = ['grid ' => __ ('Grid ' ), 'list ' => __ ('List ' )];
79
- break ;
74
+ return ['grid ' => __ ('Grid ' ), 'list ' => __ ('List ' )];
80
75
81
76
case 'list-grid ' :
82
- $ availableMode = ['list ' => __ ('List ' ), 'grid ' => __ ('Grid ' )];
83
- break ;
84
- default :
85
- $ availableMode = null ;
86
- break ;
77
+ return ['list ' => __ ('List ' ), 'grid ' => __ ('Grid ' )];
87
78
}
88
- return $ availableMode ;
79
+
80
+ return null ;
89
81
}
90
82
91
83
/**
@@ -99,12 +91,14 @@ public function getDefaultViewMode($options = [])
99
91
if (empty ($ options )) {
100
92
$ options = $ this ->getAvailableViewMode ();
101
93
}
94
+
102
95
return current (array_keys ($ options ));
103
96
}
104
97
105
98
/**
106
99
* Get default sort field
107
100
*
101
+ * @FIXME Helper should be context-independent
108
102
* @return null|string
109
103
*/
110
104
public function getDefaultSortField ()
@@ -114,59 +108,46 @@ public function getDefaultSortField()
114
108
return $ currentCategory ->getDefaultSortBy ();
115
109
}
116
110
117
- return $ this ->scopeConfig ->getValue (
118
- \Magento \Catalog \Model \Config::XML_PATH_LIST_DEFAULT_SORT_BY ,
119
- \Magento \Store \Model \ScopeInterface::SCOPE_STORE
120
- );
111
+ return $ this ->scopeConfig ->getValue (Config::XML_PATH_LIST_DEFAULT_SORT_BY , ScopeInterface::SCOPE_STORE );
121
112
}
122
113
123
114
/**
124
115
* Retrieve available limits for specified view mode
125
116
*
126
- * @param string $mode
117
+ * @param string $viewMode
127
118
* @return array
128
119
*/
129
- public function getAvailableLimit ($ mode )
120
+ public function getAvailableLimit ($ viewMode ): array
130
121
{
131
- if (!in_array ($ mode , [self ::VIEW_MODE_GRID , self ::VIEW_MODE_LIST ])) {
122
+ $ availableViewModes = $ this ->getAvailableViewMode ();
123
+
124
+ if (!isset ($ availableViewModes [$ viewMode ])) {
132
125
return $ this ->_defaultAvailableLimit ;
133
126
}
134
- $ perPageConfigKey = 'catalog/frontend/ ' . $ mode . '_per_page_values ' ;
135
- $ perPageValues = (string )$ this ->scopeConfig ->getValue (
136
- $ perPageConfigKey ,
137
- \Magento \Store \Model \ScopeInterface::SCOPE_STORE
138
- );
127
+
128
+ $ perPageConfigPath = 'catalog/frontend/ ' . $ viewMode . '_per_page_values ' ;
129
+ $ perPageValues = (string )$ this ->scopeConfig ->getValue ($ perPageConfigPath , ScopeInterface::SCOPE_STORE );
139
130
$ perPageValues = explode (', ' , $ perPageValues );
140
131
$ perPageValues = array_combine ($ perPageValues , $ perPageValues );
141
- if ($ this ->scopeConfig ->isSetFlag (
142
- 'catalog/frontend/list_allow_all ' ,
143
- \Magento \Store \Model \ScopeInterface::SCOPE_STORE
144
- )) {
132
+ if ($ this ->scopeConfig ->isSetFlag ('catalog/frontend/list_allow_all ' , ScopeInterface::SCOPE_STORE )) {
145
133
return ($ perPageValues + ['all ' => __ ('All ' )]);
146
134
} else {
147
135
return $ perPageValues ;
148
136
}
149
137
}
150
138
151
139
/**
152
- * Retrieve default per page values
140
+ * Returns default value of `per_page` for view mode provided
153
141
*
154
142
* @param string $viewMode
155
- * @return string (comma separated)
143
+ * @return int
156
144
*/
157
- public function getDefaultLimitPerPageValue ($ viewMode )
145
+ public function getDefaultLimitPerPageValue ($ viewMode ): int
158
146
{
159
- if ($ viewMode == self ::VIEW_MODE_LIST ) {
160
- return $ this ->scopeConfig ->getValue (
161
- 'catalog/frontend/list_per_page ' ,
162
- \Magento \Store \Model \ScopeInterface::SCOPE_STORE
163
- );
164
- } elseif ($ viewMode == self ::VIEW_MODE_GRID ) {
165
- return $ this ->scopeConfig ->getValue (
166
- 'catalog/frontend/grid_per_page ' ,
167
- \Magento \Store \Model \ScopeInterface::SCOPE_STORE
168
- );
169
- }
170
- return 0 ;
147
+ $ xmlConfigPath = sprintf ('catalog/frontend/%s_per_page ' , $ viewMode );
148
+ $ defaultLimit = $ this ->scopeConfig ->getValue ($ xmlConfigPath , ScopeInterface::SCOPE_STORE );
149
+
150
+ $ availableLimits = $ this ->getAvailableLimit ($ viewMode );
151
+ return (int )($ availableLimits [$ defaultLimit ] ?? current ($ availableLimits ));
171
152
}
172
153
}
0 commit comments