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 b21077e
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 342 deletions.
17 changes: 10 additions & 7 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 [php-isocodes-db-only][] to be installed.**

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

Expand All @@ -15,13 +17,13 @@ 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`).
- ISO 3166-1 alpha-2: `alpha-2`
- ISO 3166-1 alpha-3: `alpha-3`
- ISO 3166-1 numeric: `numeric`

When no set is defined, the rule uses `'alpha-2'` (`CountryCode::ALPHA2`).

When no set is defined the rule uses `'alpha-2'` (`CountryCode::ALPHA2`).

This rules uses data from [iso-codes][].

## Categorization

Expand All @@ -46,4 +48,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
[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 b21077e

Please sign in to comment.