-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from jaytaph/validator
Added validator class
- Loading branch information
Showing
4 changed files
with
174 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |