Skip to content

Commit

Permalink
#1 Added gender validation
Browse files Browse the repository at this point in the history
  • Loading branch information
iisisrael committed Jun 16, 2020
1 parent b926256 commit 3aa24c4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Entity/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ class Client implements RecipientInterface
{
use RecipientTrait;

/**
* Client genders
*/
const GENDER_FEMALE = 'F';
const GENDER_MALE = 'M';

/**
* Client relationships - expectation is that one client is designated the
* "primary" and all others are designated in relation to the primary
Expand Down Expand Up @@ -150,6 +156,17 @@ public function __construct(?Household $household = null, string $relationship =
$this->diabetic = false;
}

/**
* Get all valid genders.
*/
public static function getGenders(): array
{
return [
self::GENDER_FEMALE,
self::GENDER_MALE,
];
}

/**
* Get all valid relations.
*/
Expand Down Expand Up @@ -227,6 +244,11 @@ public function update(ClientDto $dto): self
*/
public function validate(): void
{
// check gender value
if ($this->gender && !in_array($this->gender, self::getGenders())) {
throw new ConstraintDefinitionException(sprintf('Invalid gender "%s" in %s; must be null or one of: %s', $this->gender, get_class(), implode(', ', self::getGenders())));
}

// check relationship value
if (!in_array($this->relationship, self::getRelations())) {
throw new ConstraintDefinitionException(sprintf('Invalid relationship "%s" in %s; must be one of: %s', $this->relationship, get_class(), implode(', ', self::getRelations())));
Expand Down
14 changes: 14 additions & 0 deletions tests/Entity/ClientUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ public function testConstructWithRelationship(): void
$this->assertSame('child', $client->getRelationship());
}

/**
* Verify the valid genders.
*/
public function testGetGenders(): void
{
$this->assertSame(
[
'F',
'M',
],
Client::getGenders()
);
}

/**
* Verify the valid relations.
*/
Expand Down

0 comments on commit 3aa24c4

Please sign in to comment.