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