Add validation methods to xlvask_customer, enhance default property values, and update createCustomer with new references to ensure data consistency.
This commit is contained in:
@@ -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
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user