Add min and max fields to product options

This commit introduces support for `min` and `max` fields in product options. It includes backend changes to handle validation, setting, and retrieval of these new fields, ensuring compatibility. Additionally, a new `requireTypeIn` utility and `TYPE_NULL` constant were added for improved type validation.
This commit is contained in:
Jepp9350
2025-03-07 12:29:45 +01:00
parent 370760fbd3
commit d160ecbb63
3 changed files with 103 additions and 3 deletions
@@ -14,6 +14,8 @@ class product_options_o extends db
public object_property $product_id;
public object_property $option_id;
public object_property $name;
public object_property $min;
public object_property $max;
public function structure(): void
{
@@ -43,6 +45,8 @@ class product_options_o extends db
$this->product_id = new object_property($this->table, $this->id, 'product_id', 'int', false);
$this->option_id = new object_property($this->table, $this->id, 'option_id', 'int', false);
$this->name = new object_property($this->table, $this->id, 'name', 'string', false);
$this->min = new object_property($this->table, $this->id, 'min', 'int', false);
$this->max = new object_property($this->table, $this->id, 'max', 'int', false);
}
public function objectChanged(): void
@@ -80,6 +84,44 @@ class product_options_o extends db
self::objectChanged();
}
/**
* Set the min value of the product option
* @param int|null $min
* @notation If the int is less than 1 or empty, set it to null
* @throws Exception If the object was not selected
*/
public function set_min(int|null $min): void
{
self::requireSelected();
// If the int is empty, set it to null
if (empty($min) || $min < 1) {
$this->min->nullify();
self::objectChanged();
return;
}
$this->min->set($min);
self::objectChanged();
}
/**
* Set the max value of the product option
* @param int|null $max
* @notation If the int is less than 1, or empty, it will be set to null
* @throws Exception If the object was not selected
*/
public function set_max(int|null $max): void
{
self::requireSelected();
// If the int is empty, set it to null
if (empty($max) || $max < 1) {
$this->max->nullify();
self::objectChanged();
return;
}
$this->max->set($max);
self::objectChanged();
}
/**
* Get all options for a product
* @param int $id The id of the product
@@ -94,6 +136,8 @@ class product_options_o extends db
'product_id',
'option_id',
'name',
'min',
'max',
'created_at',
'updated_at'
]
@@ -106,6 +150,8 @@ class product_options_o extends db
'product_id' => (int)$option['product_id'],
'option_id' => (int)$option['option_id'],
'name' => (string)$option['name'] === '' ? $tmp_product['name'] : (string)$option['name'],
'min' => $option['min'] === null ? null : (int)$option['min'],
'max' => $option['max'] === null ? null : (int)$option['max'],
'created_at' => (string)$option['created_at'],
'updated_at' => (string)$option['updated_at'],
'product' => $tmp_product
@@ -33,6 +33,8 @@ class productOptionsRoute
'product_id',
'option_id',
'name',
'min',
'max',
'created_at',
'updated_at'
])
@@ -44,6 +46,8 @@ class productOptionsRoute
'product_id' => (int)$option['product_id'],
'option_id' => (int)$option['option_id'],
'name' => (string)$option['name'],
'min' => $option['min'] === null ? null : (int)$option['min'],
'max' => $option['max'] === null ? null : (int)$option['max'],
'created_at' => (string)$option['created_at'],
'updated_at' => (string)$option['updated_at'],
];
@@ -109,15 +113,37 @@ class productOptionsRoute
// Check if the request was successful
if ($user) {
// Check if the required parameters are set
self::requireParameters(['id', 'name']);
self::requireParameters(['id']);
// Check if the parameters are of the correct type
self::requireType(self::getParameter('id'), self::TYPE_INT());
// Create the object
$product_options = new product_options_o();
// Select the object
$product_options->select(self::getParameter('id'));
// Set the name
$product_options->set_name(self::getParameter('name'));
// Check what the user wants to edit
// Option name
if (self::isParametersSet(['name'])) {
// Check if the parameters are of the correct type
self::requireType(self::getParameter('name'), self::TYPE_STRING());
// Set the name
$product_options->set_name(self::getParameter('name'));
}
// Option min
if (self::isParametersSet(['min'])) {
// Check if the parameters are of the correct type (Or NULL)
self::requireTypeIn(self::getParameter('min'), [self::TYPE_INT(), self::TYPE_NULL()]);
// Set the min value
$product_options->set_min(self::getParameter('min'));
}
// Option max
if (self::isParametersSet(['max'])) {
// Check if the parameters are of the correct type
self::requireType(self::getParameter('max'), self::TYPE_INT());
// Set the max value
$product_options->set_max(self::getParameter('max'));
}
// Log the incident
(new logs_o())->add('product_options', 'global', 1, $user->id, 'EDIT_PRODUCT_OPTION', 'User edited a product option');
// Return the object
+28
View File
@@ -39,6 +39,34 @@ trait route_t
return true;
}
/**
* Require parameter to be one of the given types
* @param mixed $value The value to check
* @param array $types The types to check against
* @return bool
*/
public function requireTypeIn(mixed $value, array $types): bool
{
// Get the type of the value
$value_type = gettype($value);
// Check if the value is of the required type
if (!in_array($value_type, $types)) {
global $response;
$response->error('Invalid type. Expected: ' . implode(', ', $types) . ' Got: ' . $value_type . ' Value: ' . $value, 400);
}
return true;
}
/**
* TYPE_NULL
* @note This is used to check if the value is null, this is used in routes to validate input
* @return string
*/
public function TYPE_NULL(): string
{
return 'NULL';
}
/**
* The default date format (Y-m-d) Example: 2023-01-01
* @note This is used to format dates in the API