From 19108c833ed51f4133bc0b90ecd0e3ec43a40b0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Sun, 12 Sep 2021 00:05:38 +0200 Subject: [PATCH] add some comments on exceptions --- src/Exceptions/DuplicateLabelsException.php | 6 +++++ src/Exceptions/DuplicateValuesException.php | 7 +++++- src/Rules/EnumIsValidRule.php | 25 ++++++--------------- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/Exceptions/DuplicateLabelsException.php b/src/Exceptions/DuplicateLabelsException.php index 9ed5df4..07e2978 100644 --- a/src/Exceptions/DuplicateLabelsException.php +++ b/src/Exceptions/DuplicateLabelsException.php @@ -4,6 +4,12 @@ namespace Kwaadpepper\Enum\Exceptions; use Exception; +/** + * Is thrown when your enum definition + * contain multiple times the same label value + * + * This should only happen if you override the labels method + */ class DuplicateLabelsException extends Exception { // diff --git a/src/Exceptions/DuplicateValuesException.php b/src/Exceptions/DuplicateValuesException.php index ff4d818..f5cd5ca 100644 --- a/src/Exceptions/DuplicateValuesException.php +++ b/src/Exceptions/DuplicateValuesException.php @@ -4,7 +4,12 @@ namespace Kwaadpepper\Enum\Exceptions; use Exception; -class DuplicateValuesException extends Exception +/** + * Is thrown when your enum definition + * contain multiple times the same value + * + * This should only happen if you override the values method + */ class DuplicateValuesException extends Exception { // } diff --git a/src/Rules/EnumIsValidRule.php b/src/Rules/EnumIsValidRule.php index 77d9ad7..462eb0f 100644 --- a/src/Rules/EnumIsValidRule.php +++ b/src/Rules/EnumIsValidRule.php @@ -5,11 +5,12 @@ namespace Kwaadpepper\Enum\Rules; use BadMethodCallException; use TypeError; +/** + * Custom Rule to validate an enum + */ class EnumIsValidRule extends BaseEnumRule { - private $messageType = 0; - /** * Determine if the validation rule passes. * @@ -25,11 +26,7 @@ class EnumIsValidRule extends BaseEnumRule $value = (is_numeric($value) and floatval(intval($value)) === floatval($value)) ? (int)$value : $value; forward_static_call([$this->enumClass, 'make'], $value); - } catch (TypeError $e) { - $this->messageType = 1; - return false; - } catch (BadMethodCallException $e) { - $this->messageType = 2; + } catch (TypeError | BadMethodCallException $e) { return false; } return true; @@ -41,16 +38,8 @@ class EnumIsValidRule extends BaseEnumRule */ public function message(): string { - $message = null; - switch ($this->messageType) { - case 1: - $message = trans('enum::enum.isNotValid'); - break; - case 2: - $message = trans('enum::enum.notInList', [ - 'values' => implode(',', forward_static_call([$this->enumClass, 'toValues'])) - ]); - } - return \is_string($message) ? $message : 'Invalid Enum'; + return trans('enum::enum.notInList', [ + 'values' => implode(',', forward_static_call([$this->enumClass, 'toValues'])) + ]); } }