From bd0b1f83b839d233530f5db01f95bfa5b6ef2467 Mon Sep 17 00:00:00 2001 From: Jepp9350 <2jepp9350@gmail.com> Date: Wed, 18 Jun 2025 12:47:09 +0200 Subject: [PATCH] Add validation methods to `xlvask_customer`, enhance default property values, and update `createCustomer` with new references to ensure data consistency. --- .../xlvask/classes/xlvask_endpoints.php | 2 + .../xlvask/helpers/xlvask_customer.php | 64 +++++++++++++++++-- 2 files changed, 62 insertions(+), 4 deletions(-) diff --git a/services/nginx/app/modules/xlvask/classes/xlvask_endpoints.php b/services/nginx/app/modules/xlvask/classes/xlvask_endpoints.php index 784d6191..86e8fdc1 100644 --- a/services/nginx/app/modules/xlvask/classes/xlvask_endpoints.php +++ b/services/nginx/app/modules/xlvask/classes/xlvask_endpoints.php @@ -213,6 +213,8 @@ abstract class xlvask_endpoints extends xlvask_request implements xlvask_endpoin * @throws Exception If the response is not valid JSON * @throws Exception If the response is not successful * @throws Exception If there is an error with the request + * @see xlvask_customer + * @see xlvask_customer::isValid() */ public function createCustomer(xlvask_customer $xlvaskCustomer): array { diff --git a/services/nginx/app/modules/xlvask/helpers/xlvask_customer.php b/services/nginx/app/modules/xlvask/helpers/xlvask_customer.php index c0d13503..b9d4540c 100644 --- a/services/nginx/app/modules/xlvask/helpers/xlvask_customer.php +++ b/services/nginx/app/modules/xlvask/helpers/xlvask_customer.php @@ -7,6 +7,14 @@ use objects\users_o; class xlvask_customer { + protected static array $requiredProperties = [ + 'customerId', + 'name', + 'vendorId', + 'customerTypeId', + 'excludeFromAutoInvoice', + 'language', + ]; /** * The unique identifier for the customer. (UUID format) * @var string $customerId @@ -21,13 +29,13 @@ class xlvask_customer * The unique identifier for the vendor associated with the customer. (UUID format) * @var string $vendorId */ - public string $vendorId; + public string $vendorId = "9dde3a0a-197d-4172-8d0d-b4652d867593"; // For some reason, this is hardcoded in the XLVask system. It has to be this value. /** * The unique identifier for the customer type. (UUID format) * @var string|null $customerTypeId * @see xlvask_guid::generate() */ - public ?string $customerTypeId; + public ?string $customerTypeId = "COMPANY"; // Default to "COMPANY" if not set /** * The discount percentage for the customer. * @var null|integer $discount @@ -100,10 +108,10 @@ class xlvask_customer */ public ?bool $active; /** - * The language preference of the customer. (Can be null) + * The language preference of the customer. (Cannot be null) * @var string|null $language */ - public ?string $language; + public ?string $language = 'da'; // Default to 'da' (Danish) if not set. /** * Additional notes or comments about the customer. (Can be null) * @var string|null $note @@ -145,4 +153,52 @@ class xlvask_customer return null; } } + + /** + * Validate the customer object. + * This method checks if the customer object has all required properties set. + * @return bool + */ + public function isValid(): bool + { + // Check if the required properties are set + foreach ( self::$requiredProperties as $property ) { + if (!$this->isPropertyValid($property)) { + // If any required property is not valid, return false + return false; + } + } + // If all required properties are set, return true + return true; + } + + /** + * Check if a property is valid. + * This method checks if a given property exists in the customer object and is not empty. + * @param string $property The name of the property to check. + * @return bool True if the property exists and is not empty, false otherwise. + */ + public function isPropertyValid(string $property): bool + { + if (!property_exists($this, $property)) { + return false; // Property does not exist + } + return !empty($this->{$property}); // Check if the property is not empty + } + + /** + * This method returns an array of required properties for the customer object. + * Each property is checked for its existence and validity. + * @return array + */ + public function getMissingRequiredProperties(): array + { + $tmp = []; + foreach ( self::$requiredProperties as $property ) { + if (!$this->isPropertyValid($property)) { + $tmp[] = $property; // Add the property to the array if it is not valid + } + } + return $tmp; + } } \ No newline at end of file