Correct fill methods on models to handle all enums + arrays

This commit is contained in:
Jérémy Munsch 2021-10-18 16:05:03 +02:00
parent c9572314aa
commit 79bcf49f0b
No known key found for this signature in database
GPG key ID: 55196BEE445F6049
5 changed files with 56 additions and 43 deletions

View file

@ -257,15 +257,6 @@ class ArcadiaMailDefault extends CrmClientModel
public function __construct(array $properties = [])
{
parent::__construct();
if (count(self::$validation['state_order']) < 2) {
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
self::$validation['state_order'][] = function ($attribute, $value, $fail) {
if (!is_object($value) or !\in_array($value, ArcadiaOrderType::toArray())) {
$type = \gettype($value);
$fail("State order ($type)$value is not in ArcadiaOrderType enum list");
}
};
}
$this->fill($properties);
return $this;
}
@ -315,9 +306,10 @@ class ArcadiaMailDefault extends CrmClientModel
{
if (isset($properties['state_order'])) {
try {
$properties['state_order'] = (\is_int($properties['state_order']) or
\ctype_digit($properties['state_order'])) ?
ArcadiaOrderType::make((int)$properties['state_order']) : $properties['state_order'];
$properties['state_order'] = (\is_object($properties['state_order']) and
get_class($properties['country']) === ArcadiaOrderType::class) ?
$properties['state_order']->value : (ctype_digit($properties['state_order']) ?
(int)$properties['state_order'] : $properties['state_order']);
} catch (TypeError $e) {
// cast anyway
$properties['state_order'] = (int)$properties['state_order'];

View file

@ -5,6 +5,7 @@ namespace LDC\LDCCRM\Models;
use LDC\LDCCRM\Enums\ApiStatus;
use LDC\LDCCRM\Enums\ArcadiaOrderType;
use LDC\LDCCRM\Utils;
use TypeError;
/**
* This class represents the customer info with
@ -263,6 +264,30 @@ class ArcadiaMailGame extends CrmClientModel
return $out;
}
/**
* Fill data into this model
* using validation rules
*
* @param array $properties
* @return void
* @throws TypeError if any valid is not validated
*/
public function fill(array $properties)
{
if (isset($properties['state_order'])) {
try {
$properties['state_order'] = (\is_object($properties['state_order']) and
get_class($properties['country']) === ArcadiaOrderType::class) ?
$properties['state_order']->value : (ctype_digit($properties['state_order']) ?
(int)$properties['state_order'] : $properties['state_order']);
} catch (TypeError $e) {
// cast anyway
$properties['state_order'] = (int)$properties['state_order'];
}
}
parent::fill($properties);
}
// PUBLIC METHODS
/**

View file

@ -7,6 +7,7 @@ use JsonSerializable;
use Kwaadpepper\Enum\BaseEnum;
use LDC\LDCCRM\Enums\ArcadiaOrderType;
use LDC\LDCCRM\Enums\Country;
use LDC\LDCCRM\Enums\QuestionType;
use LDC\LDCCRM\Exceptions\ValidationException;
use LDC\LDCCRM\Transport\ApiInterface;
use LDC\LDCCRM\Utils;
@ -122,6 +123,10 @@ abstract class CrmClientModel implements JsonSerializable
{
if (count($properties)) {
foreach ($properties as $field => $value) {
$type = $this->getPropType($field);
if (!$this->checkFieldType($type, $value)) {
throw new TypeError("Wrong type for $field");
}
$this->validateValue($field, $value);
$this->updatedValues[$field] = $value;
self::setRawProperty($this, $field, $value);
@ -204,6 +209,13 @@ abstract class CrmClientModel implements JsonSerializable
return \is_object($value) and (get_class($value) === Country::class or
\in_array(Country::class, \class_parents($value)));
}
case '\\' . QuestionType::class:
try {
$value = \is_int($value) ? QuestionType::make($value) : $value;
} finally {
return \is_object($value) and (get_class($value) === QuestionType::class or
\in_array(QuestionType::class, \class_parents($value)));
}
case '\\' . ArcadiaOrderType::class:
try {
$value = \is_int($value) ? ArcadiaOrderType::make($value) : $value;
@ -214,6 +226,8 @@ abstract class CrmClientModel implements JsonSerializable
case 'bool':
$value = \is_int($value) ? (bool)$value : $value;
return \is_bool($value);
case 'array':
return \is_array($value);
case 'string':
return \is_string($value);
case 'int':
@ -237,7 +251,8 @@ abstract class CrmClientModel implements JsonSerializable
$docComment = $propReflection->getDocComment();
preg_match_all('/@var ([\w_\x5c]+)/', $docComment, $matches);
if (isset($matches[1][0]) and in_array($matches[1][0], [
'string', 'int', 'bool', '\\' . Country::class, '\\' . ArcadiaOrderType::class
'string', 'int', 'bool', 'array', '\\' . Country::class, '\\' . ArcadiaOrderType::class,
'\\' . QuestionType::class
], true)) {
return $matches[1][0];
}

View file

@ -285,15 +285,6 @@ class Customer extends CrmClientModel
public function __construct(array $properties = [])
{
parent::__construct();
if (!is_array(self::$validation['password'])) {
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
self::$validation['country'][] = function ($attribute, $value, $fail) {
if (!is_object($value) or !\in_array($value, Country::toArray())) {
$type = \gettype($value);
$fail("Country ($type)$value is not in Country enum list");
}
};
}
$this->fill($properties);
return $this;
}
@ -343,8 +334,10 @@ class Customer extends CrmClientModel
{
if (isset($properties['country'])) {
try {
$properties['country'] = (\is_int($properties['country']) or \ctype_digit($properties['country'])) ?
Country::make((int)$properties['country']) : $properties['country'];
$properties['country'] = (\is_object($properties['country']) and
get_class($properties['country']) === Country::class) ?
$properties['country']->value : (ctype_digit($properties['country']) ?
(int)$properties['country'] : $properties['country']);
} catch (TypeError $e) {
// cast anyway
$properties['country'] = (int)$properties['country'];

View file

@ -25,13 +25,6 @@ use TypeError;
class Question extends CrmClientModel
{
/**
* Is true if this model was not filled from crm
*
* @var boolean
*/
private $isNew = true;
/**
* The question id
*
@ -68,7 +61,9 @@ class Question extends CrmClientModel
/**
* The question answers
*
* @var \LDC\LDCCRM\Models\Answer[]
* \LDC\LDCCRM\Models\Answer
*
* @var array
*/
private $answers = [];
@ -96,15 +91,6 @@ class Question extends CrmClientModel
public function __construct(array $properties = [])
{
parent::__construct();
if (count(self::$validation['type']) < 2) {
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
self::$validation['type'][] = function ($attribute, $value, $fail) {
if (!is_object($value) or !\in_array($value, QuestionType::toArray())) {
$type = \gettype($value);
$fail("Question type ($type)$value is not in QuestionType enum list");
}
};
}
$this->fill($properties);
return $this;
}
@ -154,8 +140,10 @@ class Question extends CrmClientModel
{
if (isset($properties['type'])) {
try {
$properties['type'] = (\is_int($properties['type']) or \ctype_digit($properties['type'])) ?
QuestionType::make((int)$properties['type']) : $properties['type'];
$properties['type'] = (\is_object($properties['type']) and
get_class($properties['type']) === QuestionType::class) ?
$properties['type']->value : (ctype_digit($properties['type']) ?
(int)$properties['type'] : $properties['type']);
} catch (TypeError $e) {
// cast anyway
$properties['type'] = (int)$properties['type'];