Skip to content

Commit fc4671a

Browse files
committed
Context validation proposal
1 parent 172f084 commit fc4671a

5 files changed

Lines changed: 44 additions & 8 deletions

File tree

src/Address.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ final class Address
2525
private function __construct(
2626
array $data
2727
) {
28-
$this->streetAndNumber = PrimitiveTypes::extractString($data, 'street_and_number');
29-
$this->town = PrimitiveTypes::extractString($data, 'town');
30-
$this->zipCode = ZipCode::extract($data, 'zip_code');
28+
$this->streetAndNumber = (string) NonEmptyString::extract($data, 'street_and_number');
29+
$this->town = (string) NonEmptyString::extract($data, 'town');
3130
$this->country = CountryCode::extract($data, 'country');
31+
$this->zipCode = ZipCode::extract($data, 'zip_code', countryCode: $this->country);
3232
}
3333

3434
/**

src/ExtractableTraits/StringExtractableTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ final public static function from(
2525

2626
$data = PrimitiveTypes::getString($data);
2727

28-
return new static($data);
28+
return new static($data, ...$params);
2929
}
3030

3131
}

src/ZipCode.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,13 @@ final class ZipCode implements ToStringInterface
4545
];
4646

4747
private function __construct(
48-
string $value
48+
string $value,
49+
?CountryCode $countryCode = null
4950
) {
5051
$value = StringHelpers::removeWhitespace($value);
5152
$value = Strings::upper($value);
5253

53-
if (!$this->isValid($value)) {
54+
if (!$this->isValid($value, $countryCode)) {
5455
throw new InvalidTypeException('Invalid ZIP code: ' . $value);
5556
}
5657

@@ -63,15 +64,32 @@ public function getValue(): string
6364
}
6465

6566
private function isValid(
66-
string $value
67+
string $value,
68+
?CountryCode $countryCode
6769
): bool {
70+
if ($countryCode !== null) {
71+
$pattern = self::$patternsByCountry[(string) $countryCode] ?? null;
72+
73+
if ($pattern !== null) {
74+
return $this->validate($value, $pattern);
75+
}
76+
}
77+
6878
foreach (self::$patternsByCountry as $pattern) {
69-
if (Strings::match($value, $pattern)) {
79+
if ($this->validate($value, $pattern)) {
7080
return true;
7181
}
7282
}
7383

7484
return false;
7585
}
7686

87+
private function validate(
88+
string $value,
89+
string $pattern
90+
): bool
91+
{
92+
return Strings::match($value, $pattern) !== null;
93+
}
94+
7795
}

tests/ZipCodeTest.phpt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,23 @@ final class ZipCodeTest extends TestCase
4343
Assert::equal($validValue, $zip->getValue());
4444
}
4545

46+
Assert::equal('39174', ZipCode::extract($validValues, 0, countryCode: CountryCode::from(CountryCode::CZ))->getValue());
47+
48+
Assert::exception(
49+
static fn () => ZipCode::extract($validValues, 0, countryCode: CountryCode::from(CountryCode::GB)),
50+
InvalidTypeException::class
51+
);
52+
4653
Assert::equal('WC2N5DU', ZipCode::from('WC2N 5DU')->getValue());
4754

4855
Assert::equal('W22LW', ZipCode::from('w2 2lw')->getValue());
56+
57+
Assert::equal('WC2N5DU', ZipCode::from('WC2N 5DU', countryCode: CountryCode::from(CountryCode::GB))->getValue());
58+
59+
Assert::exception(
60+
static fn () => ZipCode::from('WC2N 5DU', countryCode: CountryCode::from(CountryCode::SK)),
61+
InvalidTypeException::class
62+
);
4963
}
5064

5165
}

tools/cs/ruleset.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
<exclude name="SlevomatCodingStandard.Functions.RequireTrailingCommaInDeclaration.MissingTrailingComma"/>
3838

3939
<exclude name="SlevomatCodingStandard.Numbers.RequireNumericLiteralSeparator.RequiredNumericLiteralSeparator"/>
40+
41+
<!-- todo -->
42+
<exclude name="SlevomatCodingStandard.Functions.DisallowNamedArguments.DisallowedNamedArgument"/>
43+
<exclude name="SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter"/>
4044
</rule>
4145
<rule ref="SlevomatCodingStandard.Namespaces.UnusedUses">
4246
<properties>

0 commit comments

Comments
 (0)