Skip to content

Commit 0dc14db

Browse files
committed
Context validaction proposal
1 parent 2f76173 commit 0dc14db

13 files changed

Lines changed: 87 additions & 40 deletions

src/Address.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ private function __construct(
2727
) {
2828
$this->streetAndNumber = PrimitiveTypes::extractString($data, 'street_and_number');
2929
$this->town = PrimitiveTypes::extractString($data, 'town');
30-
$this->zipCode = ZipCode::extract($data, 'zip_code');
3130
$this->country = CountryCode::extract($data, 'country');
31+
$this->zipCode = ZipCode::extract($data, 'zip_code', countryCode: $this->country);
3232
}
3333

3434
/**

src/Duration.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ private function __construct(
3434
}
3535

3636
public static function from(
37-
mixed $data
37+
mixed $data,
38+
mixed ...$params
3839
): Duration {
3940
if ($data instanceof self) {
4041
return $data;

src/ExtractableTraits/ArrayExtractableTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ abstract public function __construct(
1919
);
2020

2121
final public static function from(
22-
mixed $data
22+
mixed $data,
23+
mixed ...$params
2324
): self {
2425
if ($data instanceof self) {
2526
return $data;

src/ExtractableTraits/EnumExtractableTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ abstract public static function get(
2323
* @return static
2424
*/
2525
final public static function from(
26-
mixed $data
26+
mixed $data,
27+
mixed ...$params
2728
): static {
2829
if ($data instanceof self) {
2930
return $data;

src/ExtractableTraits/ExtractableTrait.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ trait ExtractableTrait
1313
{
1414

1515
abstract public static function from(
16-
mixed $data
16+
mixed $data,
17+
mixed ...$params
1718
): self;
1819

1920
/**
@@ -23,7 +24,8 @@ abstract public static function from(
2324
*/
2425
public static function extract(
2526
array | \ArrayAccess $data,
26-
string | int $key
27+
string | int $key,
28+
mixed ...$params
2729
): static {
2830
$value = ExtractableHelpers::extractValue($data, $key);
2931

@@ -32,7 +34,7 @@ public static function extract(
3234
}
3335

3436
try {
35-
return self::from($value);
37+
return self::from($value, ...$params);
3638
} catch (InvalidTypeException $e) {
3739
throw $e->wrap($key);
3840
}

src/ExtractableTraits/FloatExtractableTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ abstract public function __construct(
1616
);
1717

1818
final public static function from(
19-
mixed $data
19+
mixed $data,
20+
mixed ...$params
2021
): self {
2122
if ($data instanceof self) {
2223
return $data;

src/ExtractableTraits/IntExtractableTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ abstract public function __construct(
1616
);
1717

1818
final public static function from(
19-
mixed $data
19+
mixed $data,
20+
mixed ...$params
2021
): self {
2122
if ($data instanceof self) {
2223
return $data;

src/ExtractableTraits/StringExtractableTrait.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@ abstract public function __construct(
1616
);
1717

1818
final public static function from(
19-
mixed $data
19+
mixed $data,
20+
mixed ...$params
2021
): self {
2122
if ($data instanceof self) {
2223
return $data;
2324
}
2425

2526
$data = PrimitiveTypes::getString($data);
2627

27-
return new static($data);
28+
return new static($data, ...$params);
2829
}
2930

3031
}

src/JsonString.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ private function __construct(
3232
* @throws \SmartEmailing\Types\InvalidTypeException
3333
*/
3434
public static function from(
35-
mixed $data
35+
mixed $data,
36+
mixed ...$params
3637
): JsonString
3738
{
3839
if ($data instanceof self) {

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
}

0 commit comments

Comments
 (0)