From bee4081aebda6b32a39d05ba3c2b3f0255934fcf Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Tue, 7 Jul 2026 16:57:15 +0200 Subject: [PATCH] Add LanguageCode rule --- README.md | 12 +++++++++ src/Rules/LanguageCode.php | 45 ++++++++++++++++++++++++++++++++ tests/Rules/LanguageCodeTest.php | 33 +++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 src/Rules/LanguageCode.php create mode 100644 tests/Rules/LanguageCodeTest.php diff --git a/README.md b/README.md index bf0c45c..c7a32fc 100644 --- a/README.md +++ b/README.md @@ -351,6 +351,18 @@ The field under validation must be a valid country code according to ISO 3166-1 The country code has three different format `ALPHA2`, `ALPHA3` or `NUMERIC`. Select the format you want to check. Default `Iso3166::ALPHA2`. +### Language Code (ISO 639-1) + +The field under validation must be a valid language code according to ISO ISO 639-1. + + public Intervention\Validation\Rules\Iso3166::__construct(bool $strict = true) + +#### Parameters + +**strict** + +If `strict` is `true`, the language code must be passed in lowercase. Otherwise, the case doesn't matter. + ## Development & Testing With this package comes a Docker image to build a test suite container. To diff --git a/src/Rules/LanguageCode.php b/src/Rules/LanguageCode.php new file mode 100644 index 0000000..0ea1015 --- /dev/null +++ b/src/Rules/LanguageCode.php @@ -0,0 +1,45 @@ +strict === false) { + $value = strtolower($value); + } + + return in_array($value, self::VALUES, strict: true); + } +} diff --git a/tests/Rules/LanguageCodeTest.php b/tests/Rules/LanguageCodeTest.php new file mode 100644 index 0000000..7598c8d --- /dev/null +++ b/tests/Rules/LanguageCodeTest.php @@ -0,0 +1,33 @@ +isValid($value); + $this->assertEquals($result, $valid); + } + + public static function dataProvider(): Generator + { + yield [true, 'de', true]; + yield [true, 'en', true]; + yield [true, 'se', true]; + yield [true, 'cs', true]; + yield [true, 'EN', false]; + yield [false, 'cz', true]; + yield [false, 'us', true]; + yield [false, 'EN', true]; + yield [false, 'xx', true]; + } +}