Skip to content

Commit

Permalink
Use PHP ISO Codes in the "CountryCode" rule
Browse files Browse the repository at this point in the history
Since we now have PHP ISO Codes as a dependency[1], it doesn't make
sense to keep dealing with this data ourselves.

[1]: 04b2722

Signed-off-by: Henrique Moody <[email protected]>
  • Loading branch information
henriquemoody committed Feb 22, 2024
1 parent 1fd60ed commit 4c99cbf
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 343 deletions.
18 changes: 10 additions & 8 deletions docs/rules/CountryCode.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# CountryCode

- `CountryCode()`
- `CountryCode(string $set)`
- `CountryCode("alpha-2"|"alpha-3"|"numeric" $set)`

Validates whether the input is a country code in [ISO 3166-1][] standard.

**This rule requires [sokil/php-isocodes][] and [sokil/php-isocodes-db-only][] to be installed.**

```php
v::countryCode()->validate('BR'); // true

Expand All @@ -15,13 +17,11 @@ v::countryCode('numeric')->validate('504'); // true

This rule supports the three sets of country codes:

- ISO 3166-1 alpha-2 (`'alpha-2'` or `CountryCode::ALPHA2`)
- ISO 3166-1 alpha-3 (`'alpha-3'` or `CountryCode::ALPHA3`)
- ISO 3166-1 numeric (`'numeric'` or `CountryCode::NUMERIC`).

When no set is defined the rule uses `'alpha-2'` (`CountryCode::ALPHA2`).
- ISO 3166-1 alpha-2: `alpha-2`
- ISO 3166-1 alpha-3: `alpha-3`
- ISO 3166-1 numeric: `numeric`

This rules uses data from [iso-codes][].
When no set is defined, the rule uses `'alpha-2'` (`CountryCode::ALPHA2`).

## Categorization

Expand All @@ -32,6 +32,7 @@ This rules uses data from [iso-codes][].

Version | Description
--------|-------------
3.0.0 | Require [sokil/php-isocodes][] and [sokil/php-isocodes-db-only][]
2.0.0 | Became case-sensitive
0.5.0 | Created

Expand All @@ -46,4 +47,5 @@ See also:
- [Tld](Tld.md)

[ISO 3166-1]: https://wikipedia.org/wiki/ISO_3166-1
[iso-codes]: https://salsa.debian.org/iso-codes-team/iso-codes
[sokil/php-isocodes]: https://github.com/sokil/php-isocodes
[sokil/php-isocodes-db-only]: https://github.com/sokil/php-isocodes-db-only
3 changes: 2 additions & 1 deletion library/ChainedValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ public function containsAny(array $needles, bool $strictCompareArray = false): C

public function countable(): ChainedValidator;

public function countryCode(?string $set = null): ChainedValidator;
/** @param "alpha-2"|"alpha-3"|"numeric" $set */
public function countryCode(string $set = 'alpha-2'): ChainedValidator;

public function currencyCode(): ChainedValidator;

Expand Down
Loading

0 comments on commit 4c99cbf

Please sign in to comment.