From 79bcf49f0bacb986e9b5fef77c614b078b6a339f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Mon, 18 Oct 2021 16:05:03 +0200 Subject: [PATCH] Correct fill methods on models to handle all enums + arrays --- src/Models/ArcadiaMailDefault.php | 16 ++++------------ src/Models/ArcadiaMailGame.php | 25 +++++++++++++++++++++++++ src/Models/CrmClientModel.php | 17 ++++++++++++++++- src/Models/Customer.php | 15 ++++----------- src/Models/Question.php | 26 +++++++------------------- 5 files changed, 56 insertions(+), 43 deletions(-) diff --git a/src/Models/ArcadiaMailDefault.php b/src/Models/ArcadiaMailDefault.php index 6c12b41..1992239 100644 --- a/src/Models/ArcadiaMailDefault.php +++ b/src/Models/ArcadiaMailDefault.php @@ -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']; diff --git a/src/Models/ArcadiaMailGame.php b/src/Models/ArcadiaMailGame.php index 5edd564..a2fbd0f 100644 --- a/src/Models/ArcadiaMailGame.php +++ b/src/Models/ArcadiaMailGame.php @@ -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 /** diff --git a/src/Models/CrmClientModel.php b/src/Models/CrmClientModel.php index ad34b73..7235a01 100644 --- a/src/Models/CrmClientModel.php +++ b/src/Models/CrmClientModel.php @@ -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]; } diff --git a/src/Models/Customer.php b/src/Models/Customer.php index 4c47293..037976f 100644 --- a/src/Models/Customer.php +++ b/src/Models/Customer.php @@ -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']; diff --git a/src/Models/Question.php b/src/Models/Question.php index 094c389..cb9566f 100644 --- a/src/Models/Question.php +++ b/src/Models/Question.php @@ -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'];