File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 /**
Original file line number Diff line number Diff 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 ;
Original file line number Diff line number Diff 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 ;
Original file line number Diff line number Diff 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 ;
Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff 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 ;
Original file line number Diff line number Diff 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 ;
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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) {
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments