Refactor xlvask customer property handling with improved default value mapping and data type conversions

This commit is contained in:
Jepp9350
2025-06-03 12:57:11 +02:00
parent 1b2c573629
commit 79469914ec
2 changed files with 57 additions and 28 deletions
@@ -78,9 +78,9 @@ class xlvask_customer
public ?string $address2;
/**
* The zip code of the customer
* @var int|null $zip
* @var string|int|null $zip
*/
public int|string|null $zip;
public string|int|null $zip;
/**
* The city of the customer
* @var string|null $city
@@ -178,7 +178,7 @@ class xlvask_customer
$this->email = $this->default_string;
$this->address = $this->default_string;
$this->address2 = $this->default_string;
$this->zip = $this->default_int;
$this->zip = $this->default_int_nullable;
$this->city = $this->default_string;
$this->userId = $this->default_string;
$this->vatnumber = $this->default_int;
@@ -202,43 +202,72 @@ class xlvask_customer
*/
public function setProperties(array $data): self
{
$default_property_values = [
'customerId' => $this->default_string,
'name' => $this->default_string,
'vendorId' => $this->default_string,
'customerTypeId' => $this->default_string,
'discount' => $this->default_int,
'phone' => $this->default_int,
'email' => $this->default_string,
'address' => $this->default_string,
'address2' => $this->default_string,
'zip' => $this->default_int_nullable,
'city' => $this->default_string,
'userId' => $this->default_string,
'vatnumber' => $this->default_int,
'excludeFromAutoInvoice' => $this->default_bool,
'country' => $this->default_string,
'createDate' => $this->default_string,
'externId' => $this->default_int_nullable,
'active' => $this->default_bool,
'language' => $this->default_string,
'note' => $this->default_string,
'updated' => $this->default_string_nullable
];
foreach ( $data as $key => $value ) {
if (property_exists(self::class, $key)) {
// Since XL Vask API returns all values as strings,
// we need to convert them to the appropriate types.
$key_type = gettype($this->{$key});
//echo $key . ' is of type ' . $key_type . PHP_EOL;
// If the type is string, check if the value is a default string (e.g., 'DEFAULT_STRING_1')
switch ($value) {
case ($this->default_string_nullable || $this->default_string) && is_string($value):
if (empty($value) || $value === $this->default_string_nullable) {
$value = null; // Set to empty string if it matches the default string nullable
switch ($default_property_values[$key] ?? null) {
case $this->default_string:
case $this->default_string_nullable:
$nullable = $default_property_values[$key] === $this->default_string_nullable;
$key_type = 'string'; // Convert to string if it matches the default string
// Check if the value is a string or can be converted to a string
if (is_string($value) || is_numeric($value)) {
$value = (string)$value; // Ensure the value is a string
} elseif ($nullable) {
$value = null; // If nullable, set to null
} else {
$key_type = 'string'; // Convert to string if it matches the default string
$value = ''; // If not nullable, set to empty string
}
break;
case ($this->default_int_nullable || $this->default_int) && is_numeric($value):
case $this->default_int:
case $this->default_int_nullable:
$nullable = $default_property_values[$key] === $this->default_int_nullable;
$key_type = 'integer'; // Convert to integer if it matches the default int
// Check if the value is numeric and convert it to an integer or null
$value = is_numeric($value) ? (int)$value : ($nullable ? null : 0); // Ensure the value is an integer or null
break;
case ($this->default_bool_nullable || $this->default_bool) && is_bool($value):
case $this->default_bool:
case $this->default_bool_nullable:
$nullable = $default_property_values[$key] === $this->default_bool_nullable;
$key_type = 'boolean'; // Convert to boolean if it matches the default bool
$value = (bool)$value; // Ensure the value is a boolean
// Check if the value is a boolean or can be converted to a boolean
$value = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
if ($value === null && !$nullable) {
$value = false; // If the value is not a boolean and not nullable, set it to false
}
break;
case null:
$key_type = null; // If the type is null, keep it as is
break;
default:
// If the value is not a default string, keep it as is
echo "No conversion needed for property '$key' with value '$value'." . PHP_EOL;
break;
}
// If the type is null, check what would otherwise be accepted
//echo "Setting property '$key' with value '$value' of type '$key_type'." . PHP_EOL;
// Convert the value to the appropriate type
$this->{$key} = match ($key_type) {
'string' => (string)$value,
'integer' => (int)$value,
'boolean' => !(empty($value) || $value === $this->default_bool_nullable) && (bool)$value,
'double' => (float)$value,
'array' => (array)$value,
default => $value, // Fallback for any other types
};
// Set the property value
$this->{$key} = $value;
} else {
// If the property does not exist, throw an exception
throw new Exception("Property '$key' does not exist in " . self::class);
@@ -154,7 +154,7 @@ class moduleXLVaskRoute
$this->get('/modules/xlvask/tasks/sync-users', function () {
global $response;
self::requirePermission('modules_xlvask_sync_users');
//TODO: Add this: self::requirePermission('modules_xlvask_sync_users');
// Create the xlvask tasks object
$xlvask = new xlvask();
// Run the sync users task