1616 */
1717class ProductAttributesExtender
1818{
19- /**
20- * Validation pattern for attribute code
21- */
22- private const VALIDATION_RULE_PATTERN = '/^[a-zA-Z]+[a-zA-Z0-9_]*$/u ' ;
23-
24- private const ATTRIBUTE_CODE_MAX_LENGTH = 60 ;
25-
26- private const ATTRIBUTE_CODE_MIN_LENGTH = 1 ;
27-
2819 /**
2920 * @var Fields
3021 */
@@ -57,65 +48,6 @@ public function __construct(
5748 $ this ->attributeCollectionFactory = $ attributeCollectionFactory ;
5849 }
5950
60- /**
61- * Get only attribute codes that pass validation
62- *
63- * @return array
64- */
65- private function getValidAttributeCodes (): array
66- {
67- return array_filter ($ this ->fields ->getFieldsUsedInQuery (), [$ this ,'validateAttributeCode ' ]);
68- }
69-
70- /**
71- * Validate attribute code
72- *
73- * @param string|int $attributeCode
74- * @return bool
75- * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
76- */
77- private function validateAttributeCode (string |int $ attributeCode ): bool
78- {
79- $ attributeCode = trim ((string )$ attributeCode );
80- if (strlen ($ attributeCode ) > 0
81- && !preg_match (self ::VALIDATION_RULE_PATTERN , $ attributeCode )
82- ) {
83- return false ;
84- }
85-
86- $ minLength = self ::ATTRIBUTE_CODE_MIN_LENGTH ;
87- $ maxLength = self ::ATTRIBUTE_CODE_MAX_LENGTH ;
88-
89- $ isAllowedLength = filter_var (
90- strlen ($ attributeCode ),
91- FILTER_VALIDATE_INT ,
92- ['options ' => [
93- 'min_range ' => $ minLength , 'max_range ' => $ maxLength ]
94- ]
95- );
96-
97- if (!$ isAllowedLength ) {
98- return false ;
99- }
100-
101- return true ;
102- }
103-
104- /**
105- * Get attributes based on validated codes
106- *
107- * @return array
108- */
109- private function getAttributes (): array
110- {
111- $ attributeCollection = $ this ->attributeCollectionFactory ->create ()
112- ->removeAllFieldsFromSelect ()
113- ->addFieldToSelect ('attribute_code ' )
114- ->setCodeFilter ($ this ->getValidAttributeCodes ())
115- ->load ();
116- return $ attributeCollection ->getColumnValues ('attribute_code ' );
117- }
118-
11951 /**
12052 * Add requested product attributes.
12153 *
@@ -126,10 +58,17 @@ private function getAttributes(): array
12658 */
12759 public function afterGetProductAttributes (QuoteConfig $ subject , array $ result ): array
12860 {
129- $ hash = hash ('sha256 ' , json_encode ($ this ->fields ->getFieldsUsedInQuery ()));
130- if (!$ this ->fieldsHash || $ this ->fieldsHash !== $ hash ) {
131- $ this ->fieldsHash = hash ('sha256 ' , json_encode ($ this ->fields ->getFieldsUsedInQuery ()));
132- $ this ->attributes = $ this ->getAttributes ();
61+
62+ $ fieldsUsedInQuery = $ this ->fields ->getFieldsUsedInQuery ();
63+ $ fieldsHash = hash ('sha256 ' , json_encode ($ fieldsUsedInQuery ));
64+ if (!$ this ->fieldsHash || $ this ->fieldsHash !== $ fieldsHash ) {
65+ $ this ->fieldsHash = hash ('sha256 ' , json_encode ($ fieldsUsedInQuery ));
66+ $ attributeCollection = $ this ->attributeCollectionFactory ->create ()
67+ ->removeAllFieldsFromSelect ()
68+ ->addFieldToSelect ('attribute_code ' )
69+ ->setCodeFilter ($ fieldsUsedInQuery )
70+ ->load ();
71+ $ this ->attributes = $ attributeCollection ->getColumnValues ('attribute_code ' );
13372 }
13473 $ attributes = $ this ->attributes ;
13574
0 commit comments