Skip to content

Commit 3e31d97

Browse files
authored
Merge pull request #6 from Invertus/BRD-130/price-data
BRD-130: price data added v2
2 parents b175c2c + 7bc01e3 commit 3e31d97

File tree

5 files changed

+107
-2
lines changed

5 files changed

+107
-2
lines changed

IMPLEMENTATION.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ SDK/php/
6363
### 3. Data Validation
6464

6565
- **Strict validation** against field configuration
66-
- **Type checking** for all field types (text, keyword, hierarchy, variants, URLs, etc.)
66+
- **Type checking** for all field types (text, keyword, hierarchy, variants, URLs, name-value lists, floats, etc.)
6767
- **Comprehensive error reporting** with detailed validation messages
6868
- **Variant validation** with attribute checking
6969

@@ -98,8 +98,12 @@ All field types from the Go implementation:
9898
- `KEYWORD` - Exact keyword matching
9999
- `HIERARCHY` - Hierarchical categories
100100
- `VARIANTS` - Product variants with attributes
101+
- `NAME_VALUE_LIST` - List of name-value pairs (e.g., features, specifications)
101102
- `IMAGE_URL` - Image URLs object with size keys (`small`, `medium`)
102103
- `URL` - Regular URLs with validation
104+
- `FLOAT` - Numeric floating-point values
105+
- `INTEGER` - Integer numeric values
106+
- `DOUBLE` - Double precision floating-point values
103107

104108
## Usage Examples
105109

src/Adapters/PrestaShopAdapter.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ private function transformProduct(array $product): array
4141
$result = [
4242
'id' => $this->getRequiredField($product, 'remoteId'),
4343
'sku' => $this->getRequiredField($product, 'sku'),
44+
'price' => $this->getRequiredField($product, 'price'),
45+
'formattedPrice' => $this->getRequiredField($product, 'formattedPrice'),
4446
'variants' => [],
4547
];
4648

src/Enums/FieldType.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@ enum FieldType: string
1414
case NAME_VALUE_LIST = 'name_value_list';
1515
case IMAGE_URL = 'image_url';
1616
case URL = 'url';
17-
}
17+
case FLOAT = 'float';
18+
case INTEGER = 'integer';
19+
case DOUBLE = 'double';
20+
}

src/Models/FieldConfigBuilder.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,30 @@ public static function imageUrl(): FieldConfig
5656
return new FieldConfig(FieldType::IMAGE_URL);
5757
}
5858

59+
/**
60+
* Create a float field
61+
*/
62+
public static function float(): FieldConfig
63+
{
64+
return new FieldConfig(FieldType::FLOAT);
65+
}
66+
67+
/**
68+
* Create an integer field
69+
*/
70+
public static function integer(): FieldConfig
71+
{
72+
return new FieldConfig(FieldType::INTEGER);
73+
}
74+
75+
/**
76+
* Create a double field
77+
*/
78+
public static function double(): FieldConfig
79+
{
80+
return new FieldConfig(FieldType::DOUBLE);
81+
}
82+
5983
/**
6084
* Create a variants field with attributes
6185
*
@@ -104,6 +128,12 @@ public static function ecommerceFields(array $locales): array
104128

105129
$defaultFields = [
106130
'id' => self::keyword(),
131+
'name' => self::textKeyword(),
132+
'brand' => self::textKeyword(),
133+
'price' => self::double(),
134+
'formattedPrice' => self::keyword(),
135+
'categoryDefault' => self::textKeyword(),
136+
'categories' => self::hierarchy(),
107137
'sku' => self::keyword(),
108138
'imageUrl' => self::imageUrl(),
109139
'productUrl' => self::url(),

src/Validators/DataValidator.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,32 @@ private function validateField(string $fieldName, mixed $value, FieldConfig $fie
8181
}
8282
break;
8383

84+
case FieldType::FLOAT:
85+
if (!is_numeric($value)) {
86+
$errors[] = "Field '{$fieldName}' must be a numeric value";
87+
} elseif (is_infinite((float)$value) || is_nan((float)$value)) {
88+
$errors[] = "Field '{$fieldName}' must be a finite numeric value";
89+
}
90+
break;
91+
92+
case FieldType::INTEGER:
93+
if (!is_numeric($value)) {
94+
$errors[] = "Field '{$fieldName}' must be a numeric value";
95+
} elseif (!is_int($value) && !ctype_digit((string)$value)) {
96+
$errors[] = "Field '{$fieldName}' must be an integer value";
97+
} elseif (is_string($value) && (int)$value != $value) {
98+
$errors[] = "Field '{$fieldName}' must be an integer value";
99+
}
100+
break;
101+
102+
case FieldType::DOUBLE:
103+
if (!is_numeric($value)) {
104+
$errors[] = "Field '{$fieldName}' must be a numeric value";
105+
} elseif (is_infinite((float)$value) || is_nan((float)$value)) {
106+
$errors[] = "Field '{$fieldName}' must be a finite numeric value";
107+
}
108+
break;
109+
84110
case FieldType::URL:
85111
if (!is_string($value) || !filter_var($value, FILTER_VALIDATE_URL)) {
86112
$errors[] = "Field '{$fieldName}' must be a valid URL";
@@ -115,6 +141,14 @@ private function validateField(string $fieldName, mixed $value, FieldConfig $fie
115141
$errors = array_merge($errors, $this->validateVariants($fieldName, $value, $fieldConfig));
116142
}
117143
break;
144+
145+
case FieldType::NAME_VALUE_LIST:
146+
if (!is_array($value)) {
147+
$errors[] = "Field '{$fieldName}' must be an array for name-value list type";
148+
} else {
149+
$errors = array_merge($errors, $this->validateNameValueList($fieldName, $value));
150+
}
151+
break;
118152
}
119153

120154
return $errors;
@@ -245,4 +279,36 @@ private function isValidImageUrl(string $url): bool
245279

246280
return in_array($extension, $imageExtensions, true);
247281
}
282+
283+
/**
284+
* Validate name-value list structure
285+
*
286+
* @return array<string>
287+
*/
288+
private function validateNameValueList(string $fieldName, array $nameValueList): array
289+
{
290+
$errors = [];
291+
292+
foreach ($nameValueList as $index => $item) {
293+
if (!is_array($item)) {
294+
$errors[] = "Field '{$fieldName}' item at index {$index} must be an object with 'name' and 'value' fields";
295+
continue;
296+
}
297+
298+
// Validate required fields
299+
if (!isset($item['name'])) {
300+
$errors[] = "Field '{$fieldName}' item at index {$index} must have a 'name' field";
301+
} elseif (!is_string($item['name']) || empty(trim($item['name']))) {
302+
$errors[] = "Field '{$fieldName}' item at index {$index} 'name' must be a non-empty string";
303+
}
304+
305+
if (!isset($item['value'])) {
306+
$errors[] = "Field '{$fieldName}' item at index {$index} must have a 'value' field";
307+
} elseif (!is_string($item['value'])) {
308+
$errors[] = "Field '{$fieldName}' item at index {$index} 'value' must be a string";
309+
}
310+
}
311+
312+
return $errors;
313+
}
248314
}

0 commit comments

Comments
 (0)