11
11
use Magento \Catalog \Api \Data \ProductCustomOptionValuesInterfaceFactory ;
12
12
use Magento \Catalog \Api \Data \ProductInterface ;
13
13
use Magento \Catalog \Model \Product ;
14
+ use Magento \Catalog \Model \Product \Option \Type \DefaultType ;
14
15
use Magento \Catalog \Model \ResourceModel \Product \Option \Value \Collection ;
15
16
use Magento \Catalog \Pricing \Price \BasePrice ;
16
17
use Magento \Framework \EntityManager \MetadataPool ;
@@ -98,6 +99,16 @@ class Option extends AbstractExtensibleModel implements ProductCustomOptionInter
98
99
*/
99
100
protected $ validatorPool ;
100
101
102
+ /**
103
+ * @var DefaultType[]
104
+ */
105
+ private $ groupsPool ;
106
+
107
+ /**
108
+ * @var string[]
109
+ */
110
+ private $ typesPool ;
111
+
101
112
/**
102
113
* @var MetadataPool
103
114
*/
@@ -120,6 +131,8 @@ class Option extends AbstractExtensibleModel implements ProductCustomOptionInter
120
131
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
121
132
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
122
133
* @param array $data
134
+ * @param array $groupsPool
135
+ * @param array $typesPool
123
136
* @param ProductCustomOptionValuesInterfaceFactory|null $customOptionValuesFactory
124
137
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
125
138
*/
@@ -135,12 +148,16 @@ public function __construct(
135
148
\Magento \Framework \Model \ResourceModel \AbstractResource $ resource = null ,
136
149
\Magento \Framework \Data \Collection \AbstractDb $ resourceCollection = null ,
137
150
array $ data = [],
151
+ array $ groupsPool = [],
152
+ array $ typesPool = [],
138
153
ProductCustomOptionValuesInterfaceFactory $ customOptionValuesFactory = null
139
154
) {
140
155
$ this ->productOptionValue = $ productOptionValue ;
141
156
$ this ->optionTypeFactory = $ optionFactory ;
142
- $ this ->validatorPool = $ validatorPool ;
143
157
$ this ->string = $ string ;
158
+ $ this ->validatorPool = $ validatorPool ;
159
+ $ this ->groupsPool = $ groupsPool ;
160
+ $ this ->typesPool = $ typesPool ;
144
161
$ this ->customOptionValuesFactory = $ customOptionValuesFactory ?:
145
162
\Magento \Framework \App \ObjectManager::getInstance ()->get (ProductCustomOptionValuesInterfaceFactory::class);
146
163
@@ -306,44 +323,30 @@ public function setProduct(Product $product = null)
306
323
/**
307
324
* Get group name of option by given option type
308
325
*
309
- * @param string $type
326
+ * @param string|null $type
310
327
* @return string
311
328
*/
312
- public function getGroupByType ($ type = null )
329
+ public function getGroupByType ($ type = null ): string
313
330
{
314
331
if ($ type === null ) {
315
332
$ type = $ this ->getType ();
316
333
}
317
- $ optionGroupsToTypes = [
318
- self ::OPTION_TYPE_FIELD => self ::OPTION_GROUP_TEXT ,
319
- self ::OPTION_TYPE_AREA => self ::OPTION_GROUP_TEXT ,
320
- self ::OPTION_TYPE_FILE => self ::OPTION_GROUP_FILE ,
321
- self ::OPTION_TYPE_DROP_DOWN => self ::OPTION_GROUP_SELECT ,
322
- self ::OPTION_TYPE_RADIO => self ::OPTION_GROUP_SELECT ,
323
- self ::OPTION_TYPE_CHECKBOX => self ::OPTION_GROUP_SELECT ,
324
- self ::OPTION_TYPE_MULTIPLE => self ::OPTION_GROUP_SELECT ,
325
- self ::OPTION_TYPE_DATE => self ::OPTION_GROUP_DATE ,
326
- self ::OPTION_TYPE_DATE_TIME => self ::OPTION_GROUP_DATE ,
327
- self ::OPTION_TYPE_TIME => self ::OPTION_GROUP_DATE ,
328
- ];
329
334
330
- return $ optionGroupsToTypes [$ type ] ?? '' ;
335
+ return $ this -> typesPool [$ type ] ?? '' ;
331
336
}
332
337
333
338
/**
334
339
* Group model factory
335
340
*
336
341
* @param string $type Option type
337
- * @return \Magento\Catalog\Model\Product\Option\Type\ DefaultType
342
+ * @return DefaultType
338
343
* @throws LocalizedException
339
344
*/
340
- public function groupFactory ($ type )
345
+ public function groupFactory ($ type ): DefaultType
341
346
{
342
347
$ group = $ this ->getGroupByType ($ type );
343
- if (!empty ($ group )) {
344
- return $ this ->optionTypeFactory ->create (
345
- 'Magento\Catalog\Model\Product\Option\Type \\' . $ this ->string ->upperCaseWords ($ group )
346
- );
348
+ if (!empty ($ group ) && isset ($ this ->groupsPool [$ group ])) {
349
+ return $ this ->optionTypeFactory ->create ($ this ->groupsPool [$ group ]);
347
350
}
348
351
throw new LocalizedException (__ ('The option type to get group instance is incorrect. ' ));
349
352
}
0 commit comments