51 lines
1.2 KiB
PHP
51 lines
1.2 KiB
PHP
<?php
|
|
|
|
class WPI_Product_Validator
|
|
{
|
|
/**
|
|
* Validate product data after mapping
|
|
*
|
|
* @return array [bool $is_valid, array $errors]
|
|
*/
|
|
public static function validate(array $data): array
|
|
{
|
|
$errors = [];
|
|
|
|
// Required fields
|
|
if (empty($data['sku'])) {
|
|
$errors[] = 'SKU is required';
|
|
}
|
|
|
|
if (empty($data['name'])) {
|
|
$errors[] = 'Product name is required';
|
|
}
|
|
|
|
// Price
|
|
if (!isset($data['price']) || !is_numeric($data['price'])) {
|
|
$errors[] = 'Price must be numeric';
|
|
} elseif ($data['price'] < 0) {
|
|
$errors[] = 'Price must be >= 0';
|
|
}
|
|
|
|
// Stock
|
|
if (!isset($data['stock']) || !is_numeric($data['stock'])) {
|
|
$errors[] = 'Stock must be numeric';
|
|
}
|
|
|
|
// Images
|
|
if (!empty($data['images']) && !is_array($data['images'])) {
|
|
$errors[] = 'Images must be an array';
|
|
}
|
|
|
|
// Categories
|
|
if (!empty($data['categories']) && !is_array($data['categories'])) {
|
|
$errors[] = 'Categories must be an array';
|
|
}
|
|
|
|
return [
|
|
'valid' => empty($errors),
|
|
'errors' => $errors
|
|
];
|
|
}
|
|
}
|