Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions src/Rules/LanguageCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Intervention\Validation\Rules;

use Intervention\Validation\AbstractRule;

class LanguageCode extends AbstractRule
{
private const VALUES = [
'aa', 'ab', 'ae', 'af', 'ak', 'am', 'an', 'ar', 'as', 'av', 'ay', 'az', 'ba', 'be', 'bg', 'bh', 'bi', 'bm',
'bn', 'bo', 'br', 'bs', 'ca', 'ce', 'ch', 'co', 'cr', 'cs', 'cu', 'cv', 'cy', 'da', 'de', 'dv', 'dz', 'ee',
'el', 'en', 'eo', 'es', 'et', 'eu', 'fa', 'ff', 'fi', 'fj', 'fo', 'fr', 'fy', 'ga', 'gd', 'gl', 'gn', 'gu',
'gv', 'ha', 'he', 'hi', 'ho', 'hr', 'ht', 'hu', 'hy', 'hz', 'ia', 'id', 'ie', 'ig', 'ii', 'ik', 'io', 'is',
'it', 'iu', 'ja', 'jv', 'ka', 'kg', 'ki', 'kj', 'kk', 'kl', 'km', 'kn', 'ko', 'kr', 'ks', 'ku', 'kv', 'kw',
'ky', 'la', 'lb', 'lg', 'li', 'ln', 'lo', 'lt', 'lu', 'lv', 'mg', 'mh', 'mi', 'mk', 'ml', 'mn', 'mr', 'ms',
'mt', 'my', 'na', 'nb', 'nd', 'ne', 'ng', 'nl', 'nn', 'no', 'nr', 'nv', 'ny', 'oc', 'oj', 'om', 'or', 'os',
'pa', 'pi', 'pl', 'ps', 'pt', 'qu', 'rm', 'rn', 'ro', 'ru', 'rw', 'sa', 'sc', 'sd', 'se', 'sg', 'si', 'sk',
'sl', 'sm', 'sn', 'so', 'sq', 'sr', 'ss', 'st', 'su', 'sv', 'sw', 'ta', 'te', 'tg', 'th', 'ti', 'tk', 'tl',
'tn', 'to', 'tr', 'ts', 'tt', 'tw', 'ty', 'ug', 'uk', 'ur', 'uz', 've', 'vi', 'vo', 'wa', 'wo', 'xh', 'yi',
'yo', 'za', 'zh', 'zu',
];

public function __construct(protected bool $strict = true)
{
//
}

/**
* {@inheritdoc}
*
* @see Rule::isValid()
*/
public function isValid(mixed $value): bool
{
$value = strval($value);

if ($this->strict === false) {
$value = strtolower($value);
}

return in_array($value, self::VALUES, strict: true);
}
}
33 changes: 33 additions & 0 deletions tests/Rules/LanguageCodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Intervention\Validation\Tests\Rules;

use Generator;
use Intervention\Validation\Rules\LanguageCode;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

final class LanguageCodeTest extends TestCase
{
#[DataProvider('dataProvider')]
public function testValidationAlpha2(bool $result, string $value, bool $strict): void
{
$valid = (new LanguageCode($strict))->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];
}
}