From 00fe11f87ae5f46ceed1f987ffc6d6de2e5582a3 Mon Sep 17 00:00:00 2001 From: Daniel Kurowski Date: Thu, 22 Apr 2021 09:46:09 +0200 Subject: [PATCH] Use enums instead of constants --- composer.json | 1 + src/Response/Event/Event.php | 23 ++++-- src/Response/Event/Invitation/Food.php | 71 ++++--------------- src/Response/Event/Program.php | 45 +++--------- src/Response/Event/ProgramType.php | 29 ++++++++ .../Event/Registration/RegistrationType.php | 34 +++------ .../Registration/RegistrationTypeEnum.php | 25 +++++++ .../OrganizationalUnit/OrganizationalUnit.php | 47 +++++------- .../OrganizationalUnitType.php | 23 ++++++ 9 files changed, 148 insertions(+), 150 deletions(-) create mode 100644 src/Response/Event/ProgramType.php create mode 100644 src/Response/Event/Registration/RegistrationTypeEnum.php create mode 100644 src/Response/OrganizationalUnit/OrganizationalUnitType.php diff --git a/composer.json b/composer.json index 4eddc7b..becbd96 100644 --- a/composer.json +++ b/composer.json @@ -12,6 +12,7 @@ "require": { "php": ">=8.0", "ext-dom": "*", + "grifart/enum": "^0.2.1", "guzzlehttp/guzzle": "^6.3" }, "require-dev": { diff --git a/src/Response/Event/Event.php b/src/Response/Event/Event.php index 511b203..3438f48 100644 --- a/src/Response/Event/Event.php +++ b/src/Response/Event/Event.php @@ -2,6 +2,7 @@ namespace HnutiBrontosaurus\BisApiClient\Response\Event; +use Grifart\Enum\MissingValueDeclarationException; use HnutiBrontosaurus\BisApiClient\BadUsageException; use HnutiBrontosaurus\BisApiClient\Response\Event\Invitation\Food; use HnutiBrontosaurus\BisApiClient\Response\Event\Invitation\Invitation; @@ -9,6 +10,7 @@ use HnutiBrontosaurus\BisApiClient\Response\Event\Invitation\Presentation; use HnutiBrontosaurus\BisApiClient\Response\Event\Registration\RegistrationQuestion; use HnutiBrontosaurus\BisApiClient\Response\Event\Registration\RegistrationType; +use HnutiBrontosaurus\BisApiClient\Response\Event\Registration\RegistrationTypeEnum; use HnutiBrontosaurus\BisApiClient\Response\RegistrationTypeException; @@ -46,7 +48,13 @@ private function __construct( public static function fromResponseData(array $data): static { // program - $programSlug = (isset($data['program_id']) && $data['program_id'] !== '') ? $data['program_id'] : null; + try { + $programSlug = (isset($data['program_id']) && $data['program_id'] !== '') + ? ProgramType::fromScalar($data['program_id']) + : ProgramType::NONE(); + } catch (MissingValueDeclarationException) { + $programSlug = ProgramType::NONE(); + } $programName = (isset($data['program']) && $data['program'] !== '') ? $data['program'] : null; $program = Program::from($programSlug, $programName); @@ -64,7 +72,12 @@ public static function fromResponseData(array $data): static ); // registration - $registrationType = (int) $data['prihlaska']; + try { + $registrationType = RegistrationTypeEnum::fromScalar((int) $data['prihlaska']); + } catch (MissingValueDeclarationException) { + $registrationType = RegistrationTypeEnum::DISABLED(); // silent fallback + } + $webRegistrationQuestion1 = (isset($data['add_info_title']) && $data['add_info_title'] !== '') ? $data['add_info_title'] : null; $webRegistrationQuestion2 = (isset($data['add_info_title_2']) && $data['add_info_title_2'] !== '') ? $data['add_info_title_2'] : null; $webRegistrationQuestion3 = (isset($data['add_info_title_3']) && $data['add_info_title_3'] !== '') ? $data['add_info_title_3'] : null; @@ -114,7 +127,9 @@ public static function fromResponseData(array $data): static // invitation // BIS API returns "0", "1", "2" etc. for real options and "" when nothing is set - $food = (isset($data['strava']) && $data['strava'] !== '') ? (int) $data['strava'] : null; + $food = (isset($data['strava']) && $data['strava'] !== '') + ? Food::fromScalar((int) $data['strava']) + : Food::NOT_LISTED(); /** @var Photo[] $invitationPresentationPhotos */ $invitationPresentationPhotos = []; @@ -134,7 +149,7 @@ public static function fromResponseData(array $data): static $invitationIntroduction, $invitationOrganizationalInformation, $accommodation, - Food::from($food), + $food, $invitationWorkDescription, $workHoursPerDay, ($invitationPresentationText !== null || \count($invitationPresentationPhotos) > 0) diff --git a/src/Response/Event/Invitation/Food.php b/src/Response/Event/Invitation/Food.php index 7d51900..49a2c3b 100644 --- a/src/Response/Event/Invitation/Food.php +++ b/src/Response/Event/Invitation/Food.php @@ -2,63 +2,22 @@ namespace HnutiBrontosaurus\BisApiClient\Response\Event\Invitation; +use Grifart\Enum\AutoInstances; +use Grifart\Enum\Enum; -final class Food -{ - - private const CHOOSEABLE = 0; - private const VEGETARIAN = 1; - private const NON_VEGETARIAN = 2; - - private bool $isListed = false; - private bool $isOfTypeChooseable = false; - private bool $isOfTypeVegetarian = false; - private bool $isOfTypeNonVegetarian = false; - - - private function __construct(?int $food) - { - if (\in_array($food, [ - self::CHOOSEABLE, - self::VEGETARIAN, - self::NON_VEGETARIAN, - ], true)) { - $this->isListed = true; - - $this->isOfTypeChooseable = $food === self::CHOOSEABLE; - $this->isOfTypeVegetarian = $food === self::VEGETARIAN; - $this->isOfTypeNonVegetarian = $food === self::NON_VEGETARIAN; - } - } - - - public static function from(?int $food): self - { - return new self($food); - } +/** + * @method static Food NOT_LISTED() + * @method static Food CHOOSEABLE() + * @method static Food VEGETARIAN() + * @method static Food NON_VEGETARIAN() + */ +final class Food extends Enum +{ + use AutoInstances; - public function isListed(): bool - { - return $this->isListed; - } - - - public function isOfTypeChooseable(): bool - { - return $this->isOfTypeChooseable; - } - - - public function isOfTypeVegetarian(): bool - { - return $this->isOfTypeVegetarian; - } - - - public function isOfTypeNonVegetarian(): bool - { - return $this->isOfTypeNonVegetarian; - } - + protected const NOT_LISTED = -1; + protected const CHOOSEABLE = 0; + protected const VEGETARIAN = 1; + protected const NON_VEGETARIAN = 2; } diff --git a/src/Response/Event/Program.php b/src/Response/Event/Program.php index 476445f..cbf4746 100644 --- a/src/Response/Event/Program.php +++ b/src/Response/Event/Program.php @@ -6,49 +6,24 @@ final class Program { - const PROGRAM_NONE = 'none'; - const PROGRAM_NATURE = 'ap'; - const PROGRAM_SIGHTS = 'pamatky'; - const PROGRAM_BRDO = 'brdo'; - const PROGRAM_EKOSTAN = 'ekostan'; - const PROGRAM_PSB = 'psb'; - const PROGRAM_EDUCATION = 'vzdelavani'; - - private function __construct( - private string $slug, + private ProgramType $slug, private ?string $name, ) {} public static function from( - ?string $slug, + ProgramType $slug, ?string $name, ): self { - if ($slug === null) { - $slug = self::PROGRAM_NONE; - } - - if ( ! \in_array($slug, [ - self::PROGRAM_NONE, - self::PROGRAM_NATURE, - self::PROGRAM_SIGHTS, - self::PROGRAM_BRDO, - self::PROGRAM_EKOSTAN, - self::PROGRAM_PSB, - self::PROGRAM_EDUCATION, - ], true)) { - $slug = self::PROGRAM_NONE; - } - return new self($slug, $name); } public function getSlug(): string { - return $this->slug; + return $this->slug->toScalar(); } @@ -60,43 +35,43 @@ public function getName(): ?string public function isNotSelected(): bool { - return $this->slug === self::PROGRAM_NONE; + return $this->slug->equals(ProgramType::NONE()); } public function isOfTypeNature(): bool { - return $this->slug === self::PROGRAM_NATURE; + return $this->slug->equals(ProgramType::NATURE()); } public function isOfTypeSights(): bool { - return $this->slug === self::PROGRAM_SIGHTS; + return $this->slug->equals(ProgramType::SIGHTS()); } public function isOfTypeBrdo(): bool { - return $this->slug === self::PROGRAM_BRDO; + return $this->slug->equals(ProgramType::BRDO()); } public function isOfTypeEkostan(): bool { - return $this->slug === self::PROGRAM_EKOSTAN; + return $this->slug->equals(ProgramType::EKOSTAN()); } public function isOfTypePsb(): bool { - return $this->slug === self::PROGRAM_PSB; + return $this->slug->equals(ProgramType::PSB()); } public function isOfTypeEducation(): bool { - return $this->slug === self::PROGRAM_EDUCATION; + return $this->slug->equals(ProgramType::EDUCATION()); } } diff --git a/src/Response/Event/ProgramType.php b/src/Response/Event/ProgramType.php new file mode 100644 index 0000000..df29adf --- /dev/null +++ b/src/Response/Event/ProgramType.php @@ -0,0 +1,29 @@ +hasValidData = match (true) { - $type === self::TYPE_VIA_EMAIL && $email === null, - $type === self::TYPE_VIA_CUSTOM_WEBPAGE && $url === null, + $type->equals(RegistrationTypeEnum::EMAIL()) && $email === null, + $type->equals(RegistrationTypeEnum::EXTERNAL_WEBPAGE()) && $url === null, => false, default => true }; @@ -40,21 +33,12 @@ private function __construct( * @param RegistrationQuestion[] $questions */ public static function from( - int $type, + RegistrationTypeEnum $type, array $questions, ?string $email, ?string $url, ): self { - if ( ! \in_array($type, [ - self::TYPE_VIA_BRONTOWEB, - self::TYPE_VIA_EMAIL, - self::TYPE_VIA_CUSTOM_WEBPAGE, - self::TYPE_NONE, - ], true)) { - $type = self::TYPE_DISABLED; // silent fallback - } - return new self($type, $questions, $email, $url); } @@ -63,7 +47,7 @@ public static function from( public function isOfTypeBrontoWeb(): bool { - return $this->type === self::TYPE_VIA_BRONTOWEB; + return $this->type->equals(RegistrationTypeEnum::BRONTOWEB()); } @@ -85,7 +69,7 @@ public function getQuestions(): array public function isOfTypeEmail(): bool { - return $this->type === self::TYPE_VIA_EMAIL; + return $this->type->equals(RegistrationTypeEnum::EMAIL()); } /** @@ -110,7 +94,7 @@ public function getEmail(): ?string public function isOfTypeCustomWebpage(): bool { - return $this->type === self::TYPE_VIA_CUSTOM_WEBPAGE; + return $this->type->equals(RegistrationTypeEnum::EXTERNAL_WEBPAGE()); } /** @@ -135,7 +119,7 @@ public function getUrl(): ?string public function isOfTypeNone(): bool { - return $this->type === self::TYPE_NONE; + return $this->type->equals(RegistrationTypeEnum::NONE()); } @@ -143,7 +127,7 @@ public function isOfTypeNone(): bool public function isOfTypeDisabled(): bool { - return $this->type === self::TYPE_DISABLED; + return $this->type->equals(RegistrationTypeEnum::DISABLED()); } diff --git a/src/Response/Event/Registration/RegistrationTypeEnum.php b/src/Response/Event/Registration/RegistrationTypeEnum.php new file mode 100644 index 0000000..a974aa5 --- /dev/null +++ b/src/Response/Event/Registration/RegistrationTypeEnum.php @@ -0,0 +1,25 @@ +type = $type; - } + ) {} /** @@ -49,6 +28,14 @@ private function __construct( */ public static function fromResponseData(array $data): self { + try { + $organizationalUnitTypeScalar = (int) $data['uroven']; + $organizationalUnitType = OrganizationalUnitType::fromScalar($organizationalUnitTypeScalar); + + } catch (MissingValueDeclarationException) { + throw new UnknownOrganizationUnitTypeException('Type `' . $organizationalUnitTypeScalar . '` is not of valid types.'); + } + return new self( (int) $data['id'], $data['nazev'], @@ -58,7 +45,7 @@ public static function fromResponseData(array $data): self (isset($data['telefon']) && $data['telefon'] !== '') ? $data['telefon'] : null, (isset($data['email']) && $data['email'] !== '') ? $data['email'] : null, (isset($data['www']) && $data['www'] !== '') ? $data['www'] : null, - (int) $data['uroven'], + $organizationalUnitType, (isset($data['predseda']) && $data['predseda'] !== '') ? $data['predseda'] : null, (isset($data['hospodar']) && $data['hospodar'] !== '') ? $data['hospodar'] : null, ); @@ -127,25 +114,25 @@ public function getManager(): ?string public function isClub(): bool { - return $this->type === self::TYPE_CLUB; + return $this->type->equals(OrganizationalUnitType::CLUB()); } public function isBaseUnit(): bool { - return $this->type === self::TYPE_BASE; + return $this->type->equals(OrganizationalUnitType::BASE()); } public function isRegionalUnit(): bool { - return $this->type === self::TYPE_REGIONAL; + return $this->type->equals(OrganizationalUnitType::REGIONAL()); } public function isOffice(): bool { - return $this->type === self::TYPE_OFFICE; + return $this->type->equals(OrganizationalUnitType::OFFICE()); } } diff --git a/src/Response/OrganizationalUnit/OrganizationalUnitType.php b/src/Response/OrganizationalUnit/OrganizationalUnitType.php new file mode 100644 index 0000000..e294b73 --- /dev/null +++ b/src/Response/OrganizationalUnit/OrganizationalUnitType.php @@ -0,0 +1,23 @@ +