File tree Expand file tree Collapse file tree 4 files changed +47
-2
lines changed
Expand file tree Collapse file tree 4 files changed +47
-2
lines changed Original file line number Diff line number Diff line change @@ -79,6 +79,20 @@ public function validate($value): Resolution
7979 ]);
8080 }
8181
82+ foreach ($ this ->config ->getAIConstraints () as $ code => $ constraint ) {
83+ if ($ barcode ->hasAI ((string ) $ code )
84+ && !$ constraint ($ ai = $ barcode ->ai ((string ) $ code ))
85+ ) {
86+ return Resolution::createInvalid ([
87+ ErrorCodes::INVALID_VALUE => sprintf (
88+ 'AI is invalid: code=%s, value=%s ' ,
89+ $ code ,
90+ $ ai
91+ ),
92+ ]);
93+ }
94+ }
95+
8296 return Resolution::createValid ();
8397 }
8498
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ final class ValidatorConfig
88{
99 private $ requiredAIs = [];
1010 private $ forbiddenAIs = [];
11+ private $ aiConstraints = [];
1112 private $ allowEmpty = false ;
1213
1314 public function getRequiredAIs (): array
@@ -42,4 +43,15 @@ public function setAllowEmpty(bool $allowEmpty): self
4243 $ this ->allowEmpty = $ allowEmpty ;
4344 return $ this ;
4445 }
45- }
46+
47+ public function setAIConstraints (array $ aiConstraints ): self
48+ {
49+ $ this ->aiConstraints = $ aiConstraints ;
50+ return $ this ;
51+ }
52+
53+ public function getAIConstraints (): array
54+ {
55+ return $ this ->aiConstraints ;
56+ }
57+ }
Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ public function testConfigDefaults(): void
1818
1919 self ::assertEmpty ($ config ->getRequiredAIs ());
2020 self ::assertEmpty ($ config ->getForbiddenAIs ());
21+ self ::assertEmpty ($ config ->getAIConstraints ());
2122 self ::assertFalse ($ config ->isAllowEmpty ());
2223 }
2324
@@ -26,10 +27,12 @@ public function testGettersSetters(): void
2627 $ config = (new ValidatorConfig ())
2728 ->setAllowEmpty (true )
2829 ->setRequiredAIs (['10 ' ])
29- ->setForbiddenAIs (['01 ' ]);
30+ ->setForbiddenAIs (['01 ' ])
31+ ->setAIConstraints (['01 ' => fn (string $ ai ) => true ]);
3032
3133 self ::assertTrue ($ config ->isAllowEmpty ());
3234 self ::assertEquals (['10 ' ], $ config ->getRequiredAIs ());
3335 self ::assertEquals (['01 ' ], $ config ->getForbiddenAIs ());
36+ self ::assertEquals (['01 ' => fn (string $ ai ) => true ], $ config ->getAIConstraints ());
3437 }
3538}
Original file line number Diff line number Diff line change @@ -62,6 +62,10 @@ public function dataValidate(): array
6262 ->setRequiredAIs (['10 ' ]);
6363 $ forbiddenAI = (new ValidatorConfig ())
6464 ->setForbiddenAIs (['01 ' ]);
65+ $ aiConstraintReturnTrue = (new ValidatorConfig ())
66+ ->setAIConstraints (['01 ' => fn (string $ ai ) => true ]);
67+ $ aiConstraintReturnFalse = (new ValidatorConfig ())
68+ ->setAIConstraints (['01 ' => fn (string $ ai ) => false ]);
6569
6670 return [
6771 'valid ' => [
@@ -114,6 +118,18 @@ public function dataValidate(): array
114118 ErrorCodes::FORBIDDEN_AIS => '' ,
115119 ]),
116120 ],
121+ 'ai constraint return true ' => [
122+ $ aiConstraintReturnTrue ,
123+ ']d201034531200000111719112511ABCD1234 ' ,
124+ Resolution::createValid (),
125+ ],
126+ 'ai constraint return false ' => [
127+ $ aiConstraintReturnFalse ,
128+ ']d201034531200000111719112511ABCD1234 ' ,
129+ Resolution::createInvalid ([
130+ ErrorCodes::INVALID_VALUE => 'AI is invalid: code=01, value=03453120000011 ' ,
131+ ]),
132+ ],
117133 ];
118134 }
119135
You can’t perform that action at this time.
0 commit comments