Skip to content

Commit

Permalink
Merge pull request #5 from jaytaph/validator
Browse files Browse the repository at this point in the history
Added validator class
  • Loading branch information
annejan authored Mar 9, 2021
2 parents 1d24102 + 9037c60 commit 15afbfc
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 9 deletions.
8 changes: 8 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,13 @@
"suggest": {
"ext-openssl": "Install the OpenSSL extension in order to speed up a wide variety of cryptographic operations.",
"ext-gmp": "Install the GMP (GNU Multiple Precision) extension in order to speed up arbitrary precision integer arithmetic operations."
},
"scripts": {
"test": [
"vendor/bin/phpunit",
"vendor/bin/phpstan analyse",
"vendor/bin/psalm",
"vendor/bin/phpcs"
]
}
}
18 changes: 9 additions & 9 deletions src/UziUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@
class UziUser implements \JsonSerializable
{
/** @var string */
protected $agb_code;
protected $agb_code = "";
/** @var string */
protected $card_type;
protected $card_type = "";
/** @var string */
protected $given_name;
protected $given_name = "";
/** @var string */
protected $oid_ca;
protected $oid_ca = "";
/** @var string */
protected $role;
protected $role = "";
/** @var string */
protected $subscriber_number;
protected $subscriber_number = "";
/** @var string */
protected $sur_name;
protected $sur_name = "";
/** @var string */
protected $uzi_number;
protected $uzi_number = "";
/** @var string */
protected $uzi_version;
protected $uzi_version = "";

/**
* @return string
Expand Down
71 changes: 71 additions & 0 deletions src/UziValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace MinVWS\PUZI;

use MinVWS\PUZI\Exceptions\UziException;

/**
* Class UziValidator
* @package MinVWS\Laravel\Puzi
*/
class UziValidator
{
/** @var bool */
protected $strictCAcheck;
/** @var array */
protected $allowedTypes;
/** @var array */
protected $allowedRoles;

/**
* UziValidator constructor.
*
* @param bool $strictCaCheck
* @param array $allowedTypes
* @param array $allowedRoles
*/
public function __construct(bool $strictCaCheck, array $allowedTypes, array $allowedRoles)
{
$this->strictCAcheck = $strictCaCheck;
$this->allowedTypes = $allowedTypes;
$this->allowedRoles = $allowedRoles;
}

/**
* @param UziUser $user
* @return bool
*/
public function isValid(UziUser $user): bool
{
try {
$this->validate($user);
} catch (UziException $e) {
return false;
}
return true;
}

/**
* @param UziUser $user
* @throws UziException
*/
public function validate(UziUser $user): void
{
if (
$this->strictCAcheck == true &&
$user->getOidCa() !== UziConstants::OID_CA_CARE_PROVIDER &&
$user->getOidCa() !== UziConstants::OID_CA_NAMED_EMPLOYEE
) {
throw new UziException('CA OID not UZI register Care Provider or named employee');
}
if ($user->getUziVersion() !== '1') {
throw new UziException('UZI version not 1');
}
if (!in_array($user->getCardType(), $this->allowedTypes)) {
throw new UziException('UZI card type not allowed');
}
if (!in_array(substr($user->getRole(), 0, 3), $this->allowedRoles)) {
throw new UziException("UZI card role not allowed");
}
}
}
86 changes: 86 additions & 0 deletions tests/UziValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace MinVWS\PUZI\Tests;

use MinVWS\PUZI\UziConstants;
use MinVWS\PUZI\UziValidator;
use MinVWS\PUZI\Exceptions\UziException;
use MinVWS\PUZI\UziUser;
use PHPUnit\Framework\TestCase;

/**
* Class UziValidatorTest
* SPDX-License-Identifier: EUPL-1.2
* @package MinVWS\PUZI\Tests
*/
final class UziValidatorTest extends TestCase
{
public function testValidateIncorectOID(): void
{
$user = new UziUser();
$user->setOidCa("1.2.3.4");

$this->expectException(UziException::class);
$this->expectExceptionMessage("CA OID not UZI register");

$validator = new UziValidator(true, [], []);
$validator->validate($user);
}

public function testIncorrectVersion(): void
{
$user = new UziUser();
$user->setOidCa(UziConstants::OID_CA_CARE_PROVIDER);
$user->setUziVersion("123");

$this->expectException(UziException::class);
$this->expectExceptionMessage("UZI version not 1");

$validator = new UziValidator(true, [], []);
$validator->validate($user);
}

public function testNotAllowedType(): void
{
$user = new UziUser();
$user->setOidCa(UziConstants::OID_CA_CARE_PROVIDER);
$user->setUziVersion("1");
$user->setCardType(UziConstants::UZI_TYPE_SERVER);

$this->expectException(UziException::class);
$this->expectExceptionMessage("UZI card type not allowed");

$validator = new UziValidator(true, [UziConstants::UZI_TYPE_CARE_PROVIDER], []);
$validator->validate($user);
}

public function testNotAllowedRole(): void
{
$user = new UziUser();
$user->setOidCa(UziConstants::OID_CA_CARE_PROVIDER);
$user->setUziVersion("1");
$user->setCardType(UziConstants::UZI_TYPE_CARE_PROVIDER);
$user->setRole(UziConstants::UZI_ROLE_DENTIST);

$this->expectException(UziException::class);
$this->expectExceptionMessage("UZI card role not allowed");

$validator = new UziValidator(true, [UziConstants::UZI_TYPE_CARE_PROVIDER], [UziConstants::UZI_ROLE_NURSE]);
$validator->validate($user);
}

public function testIsValid(): void
{
$user = new UziUser();
$user->setOidCa(UziConstants::OID_CA_CARE_PROVIDER);
$user->setUziVersion("1");
$user->setCardType(UziConstants::UZI_TYPE_CARE_PROVIDER);
$user->setRole(UziConstants::UZI_ROLE_DENTIST);

$validator = new UziValidator(true, [UziConstants::UZI_TYPE_CARE_PROVIDER], [UziConstants::UZI_ROLE_DENTIST]);
$this->assertTrue($validator->isValid($user));

$user->setRole(UziConstants::UZI_ROLE_PHARMACIST);
$this->assertFalse($validator->isValid($user));
}
}

0 comments on commit 15afbfc

Please sign in to comment.