Skip to content

Commit

Permalink
feat(Languages): Add support for locales
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoya-de committed Oct 15, 2024
1 parent c7e7b82 commit 9727e87
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/AbstractQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
use Kore\DataObject\DataObject;
use Makaira\Exceptions\DomainException;

use function preg_replace;
use function sprintf;
use function strlen;
use function strtolower;
use function strtoupper;
use function substr;
use function trim;

abstract class AbstractQuery extends DataObject
{
/**
Expand Down Expand Up @@ -74,9 +82,28 @@ public function verify()
}
}

if (0 === preg_match('(^[a-z]{2}$)', $this->constraints[Constraints::LANGUAGE])) {
throw new DomainException('Language constraint must be two letters in lowercase.');
$trimmed = trim($this->constraints[Constraints::LANGUAGE]);
$lowered = strtolower($trimmed);
$replaced = preg_replace('/\W/', '-', $lowered);

switch (strtolower($replaced)) {
case 2:
$language = $replaced;
break;
case 4:
$language = sprintf('%s-%s', substr($replaced, 0, 2), strtoupper(substr($replaced, 2, 2)));
break;
case 5:
$language = sprintf('%s-%s', substr($replaced, 0, 2), strtoupper(substr($replaced, 3, 2)));
break;
default:
throw new DomainException(
sprintf("The language constraint must be a language or a locale, but it contains '%s'.", $trimmed)
);
}

$this->constraints[Constraints::LANGUAGE] = $language;

return $this;
}

Expand Down

0 comments on commit 9727e87

Please sign in to comment.