Skip to content

Commit

Permalink
Use enums instead of constants
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Kurowski committed Apr 22, 2021
1 parent 215d6b3 commit 00fe11f
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 150 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"require": {
"php": ">=8.0",
"ext-dom": "*",
"grifart/enum": "^0.2.1",
"guzzlehttp/guzzle": "^6.3"
},
"require-dev": {
Expand Down
23 changes: 19 additions & 4 deletions src/Response/Event/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

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;
use HnutiBrontosaurus\BisApiClient\Response\Event\Invitation\Photo;
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;


Expand Down Expand Up @@ -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);

Expand All @@ -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;
Expand Down Expand Up @@ -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 = [];
Expand All @@ -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)
Expand Down
71 changes: 15 additions & 56 deletions src/Response/Event/Invitation/Food.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
45 changes: 10 additions & 35 deletions src/Response/Event/Program.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}


Expand All @@ -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());
}

}
29 changes: 29 additions & 0 deletions src/Response/Event/ProgramType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types = 1);

namespace HnutiBrontosaurus\BisApiClient\Response\Event;

use Grifart\Enum\AutoInstances;
use Grifart\Enum\Enum;


/**
* @method static ProgramType NONE()
* @method static ProgramType NATURE()
* @method static ProgramType SIGHTS()
* @method static ProgramType BRDO()
* @method static ProgramType EKOSTAN()
* @method static ProgramType PSB()
* @method static ProgramType EDUCATION()
*/
final class ProgramType extends Enum
{
use AutoInstances;

protected const NONE = 'none';
protected const NATURE = 'ap';
protected const SIGHTS = 'pamatky';
protected const BRDO = 'brdo';
protected const EKOSTAN = 'ekostan';
protected const PSB = 'psb';
protected const EDUCATION = 'vzdelavani';
}
34 changes: 9 additions & 25 deletions src/Response/Event/Registration/RegistrationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,21 @@
final class RegistrationType
{

const TYPE_VIA_BRONTOWEB = 1;
const TYPE_VIA_EMAIL = 2;
const TYPE_VIA_CUSTOM_WEBPAGE = 3;
const TYPE_NONE = 4;
const TYPE_DISABLED = 5;


private bool $hasValidData;


/**
* @param RegistrationQuestion[] $questions
*/
private function __construct(
private int $type, // one of above constant values
private RegistrationTypeEnum $type,
private array $questions, // in case of registering via Brontoweb
private ?string $email, // in case of registering via e-mail
private ?string $url, // in case of registering via custom webpage
) {
$this->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
};
Expand All @@ -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);
}

Expand All @@ -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());
}


Expand All @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -135,15 +119,15 @@ public function getUrl(): ?string

public function isOfTypeNone(): bool
{
return $this->type === self::TYPE_NONE;
return $this->type->equals(RegistrationTypeEnum::NONE());
}


// registration disabled

public function isOfTypeDisabled(): bool
{
return $this->type === self::TYPE_DISABLED;
return $this->type->equals(RegistrationTypeEnum::DISABLED());
}


Expand Down
Loading

0 comments on commit 00fe11f

Please sign in to comment.