Skip to content

Commit

Permalink
Merge pull request #5 from zendex/feature/ai-constraint-validation
Browse files Browse the repository at this point in the history
Add ai constraint validation
  • Loading branch information
denpli authored Feb 14, 2022
2 parents 7431458 + 6fc6ec9 commit 0d015e8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/Validator/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@ public function validate($value): Resolution
]);
}

foreach ($this->config->getAIConstraints() as $code => $constraint) {
if ($barcode->hasAI((string) $code)
&& !$constraint($ai = $barcode->ai((string) $code))
) {
return Resolution::createInvalid([
ErrorCodes::INVALID_VALUE => sprintf(
'AI is invalid: code=%s, value=%s',
$code,
$ai
),
]);
}
}

return Resolution::createValid();
}

Expand Down
14 changes: 13 additions & 1 deletion src/Validator/ValidatorConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ final class ValidatorConfig
{
private $requiredAIs = [];
private $forbiddenAIs = [];
private $aiConstraints = [];
private $allowEmpty = false;

public function getRequiredAIs(): array
Expand Down Expand Up @@ -42,4 +43,15 @@ public function setAllowEmpty(bool $allowEmpty): self
$this->allowEmpty = $allowEmpty;
return $this;
}
}

public function setAIConstraints(array $aiConstraints): self
{
$this->aiConstraints = $aiConstraints;
return $this;
}

public function getAIConstraints(): array
{
return $this->aiConstraints;
}
}
5 changes: 4 additions & 1 deletion tests/Validator/ValidatorConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function testConfigDefaults(): void

self::assertEmpty($config->getRequiredAIs());
self::assertEmpty($config->getForbiddenAIs());
self::assertEmpty($config->getAIConstraints());
self::assertFalse($config->isAllowEmpty());
}

Expand All @@ -26,10 +27,12 @@ public function testGettersSetters(): void
$config = (new ValidatorConfig())
->setAllowEmpty(true)
->setRequiredAIs(['10'])
->setForbiddenAIs(['01']);
->setForbiddenAIs(['01'])
->setAIConstraints(['01' => fn(string $ai) => true]);

self::assertTrue($config->isAllowEmpty());
self::assertEquals(['10'], $config->getRequiredAIs());
self::assertEquals(['01'], $config->getForbiddenAIs());
self::assertEquals(['01' => fn(string $ai) => true], $config->getAIConstraints());
}
}
16 changes: 16 additions & 0 deletions tests/Validator/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public function dataValidate(): array
->setRequiredAIs(['10']);
$forbiddenAI = (new ValidatorConfig())
->setForbiddenAIs(['01']);
$aiConstraintReturnTrue = (new ValidatorConfig())
->setAIConstraints(['01' => fn (string $ai) => true]);
$aiConstraintReturnFalse = (new ValidatorConfig())
->setAIConstraints(['01' => fn (string $ai) => false]);

return [
'valid' => [
Expand Down Expand Up @@ -114,6 +118,18 @@ public function dataValidate(): array
ErrorCodes::FORBIDDEN_AIS => '',
]),
],
'ai constraint return true' => [
$aiConstraintReturnTrue,
']d201034531200000111719112511ABCD1234',
Resolution::createValid(),
],
'ai constraint return false' => [
$aiConstraintReturnFalse,
']d201034531200000111719112511ABCD1234',
Resolution::createInvalid([
ErrorCodes::INVALID_VALUE => 'AI is invalid: code=01, value=03453120000011',
]),
],
];
}

Expand Down

0 comments on commit 0d015e8

Please sign in to comment.