Enhance refactor commands with controller-aware Route() updates and fix code quality violations

Add semantic token highlighting for 'that' variable and comment file references in VS Code extension
Add Phone_Text_Input and Currency_Input components with formatting utilities
Implement client widgets, form standardization, and soft delete functionality
Add modal scroll lock and update documentation
Implement comprehensive modal system with form integration and validation
Fix modal component instantiation using jQuery plugin API
Implement modal system with responsive sizing, queuing, and validation support
Implement form submission with validation, error handling, and loading states
Implement country/state selectors with dynamic data loading and Bootstrap styling
Revert Rsx::Route() highlighting in Blade/PHP files
Target specific PHP scopes for Rsx::Route() highlighting in Blade
Expand injection selector for Rsx::Route() highlighting
Add custom syntax highlighting for Rsx::Route() and Rsx.Route() calls
Update jqhtml packages to v2.2.165
Add bundle path validation for common mistakes (development mode only)
Create Ajax_Select_Input widget and Rsx_Reference_Data controller
Create Country_Select_Input widget with default country support
Initialize Tom Select on Select_Input widgets
Add Tom Select bundle for enhanced select dropdowns
Implement ISO 3166 geographic data system for country/region selection
Implement widget-based form system with disabled state support

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
root
2025-10-30 06:21:56 +00:00
parent e678b987c2
commit f6ac36c632
5683 changed files with 5854736 additions and 22329 deletions

View File

@@ -0,0 +1,215 @@
<?php
declare(strict_types=1);
namespace Sokil\IsoCodes;
use Sokil\IsoCodes\TranslationDriver\GettextExtensionDriver;
use Sokil\IsoCodes\TranslationDriver\TranslationDriverInterface;
/**
* Abstract collection of ISO entries
*/
abstract class AbstractDatabase implements \Iterator, \Countable
{
/**
* Default ath ISO databases
*/
public const DATABASE_PATH = 'databases';
/**
* Default path to gettext localised messages
*/
public const MESSAGES_PATH = 'messages';
/**
* Path to directory with databases
*
* @var string
*/
protected $baseDirectory;
/**
* @var string[][]
* @psalm-var Array<int, Array<string, string>>
*
* Cluster index used for iteration by entries
*
*/
private $clusterIndex = [];
/**
* @var TranslationDriverInterface
*/
protected $translationDriver;
/**
* @param string|null $baseDirectory
* @param TranslationDriverInterface|null $translationDriver
*
* @throws \RuntimeException when base directory not specified and directory can not be located automatically
*/
public function __construct(
?string $baseDirectory = null,
?TranslationDriverInterface $translationDriver = null
) {
if (empty($baseDirectory)) {
// Require external database in "sokil/php-isocodes-db-*" packages
$suggestedBaseDirectories = [
// production mode, find in sibling directory "php-isocodes-db-i18n" or "php-isocodes-db-only"
__DIR__ . '/../../php-isocodes-db-i18n/',
__DIR__ . '/../../php-isocodes-db-only/',
// development mode, find in current vendor packages
__DIR__ . '/../vendor/sokil/php-isocodes-db-i18n/',
__DIR__ . '/../vendor/sokil/php-isocodes-db-only/',
];
foreach ($suggestedBaseDirectories as $suggestedBaseDirectory) {
if (is_dir($suggestedBaseDirectory)) {
$this->baseDirectory = $suggestedBaseDirectory;
break;
}
}
if (empty($this->baseDirectory)) {
throw new \RuntimeException(
sprintf(
'Base directory not specified and directory can not be located automatically. Finding at %s',
implode(', ', $suggestedBaseDirectories)
)
);
}
} else {
$this->baseDirectory = rtrim($baseDirectory, '/') . '/';
}
$this->translationDriver = $translationDriver ?? new GettextExtensionDriver();
$this->translationDriver->configureDirectory(
$this->getISONumber(),
$this->getLocalMessagesDirPath()
);
}
/**
* ISO Standard Number
*
* @psalm-pure
*/
abstract public static function getISONumber(): string;
/**
* @psalm-param Array<string, string> $entry
*
* @return object
*/
abstract protected function arrayToEntry(array $entry);
/**
* Get path to directory with database files
*
* @return string
*/
protected function getDatabasesPath(): string
{
return $this->baseDirectory . self::DATABASE_PATH;
}
/**
* Get path to directory with gettext messages
*/
private function getLocalMessagesDirPath(): string
{
return $this->baseDirectory . self::MESSAGES_PATH;
}
/**
* Get list of all database rows.
* For large databases like ISO-3166-2 this may require a lot of memory.
*
* @return array
*/
protected function getClusterIndex(): array
{
// initialise cluster index
$this->loadClusterIndex();
return $this->clusterIndex;
}
/**
* Build cluster index for iteration
*/
private function loadClusterIndex(): void
{
// check if cluster index already loaded
if (!empty($this->clusterIndex)) {
return;
}
$isoNumber = $this->getISONumber();
// load database from json file
$databaseFilePath = $this->getDatabasesPath() . '/iso_' . $isoNumber . '.json';
$json = \json_decode(
file_get_contents($databaseFilePath),
true
);
// build cluster index from database
$this->clusterIndex = $json[$isoNumber];
}
/**
* Builds array of entries.
* Creates many entry objects in loop, use iterator instead.
*
* @psalm-return Array<string, object>
* @return object[]
*/
public function toArray(): array
{
/** @psalm-var Array<string, object> $array */
$array = iterator_to_array($this);
return $array;
}
/**
* @return object
*/
#[\ReturnTypeWillChange]
public function current()
{
return $this->arrayToEntry(current($this->clusterIndex));
}
public function key(): ?int
{
return key($this->clusterIndex);
}
public function next(): void
{
next($this->clusterIndex);
}
public function rewind(): void
{
// initialise cluster index
$this->loadClusterIndex();
reset($this->clusterIndex);
}
public function valid(): bool
{
return $this->key() !== null;
}
public function count(): int
{
return count($this->getClusterIndex());
}
}

View File

@@ -0,0 +1,128 @@
<?php
declare(strict_types=1);
namespace Sokil\IsoCodes;
/**
* Abstract collection of ISO entries loaded from single file
*/
abstract class AbstractNotPartitionedDatabase extends AbstractDatabase
{
/**
* Index to search by entry field's values
*
* {indexedFieldName => {indexedFieldValue => entryObject}}
*
* @psalm-var Array<string, Array<string, object>>
* @var object[][]
*/
private $index;
/**
* List of entry fields to be indexed and searched.
* May be override in child classes to search by indexed fields.
*
* @return mixed[]
*/
protected function getIndexDefinition(): array
{
return [];
}
private function buildIndex(): void
{
// init empty index
$this->index = [];
// get index definition
$indexedFields = $this->getIndexDefinition();
// build index for database
if (!empty($indexedFields)) {
// init all defined indexes
foreach ($this->getClusterIndex() as $entryArray) {
$entry = $this->arrayToEntry($entryArray);
foreach ($indexedFields as $indexName => $indexDefinition) {
if (is_array($indexDefinition)) {
// compound index
// iteratively create hierarchy of array indexes
$reference = &$this->index[$indexName];
foreach ($indexDefinition as $indexDefinitionPart) {
if (is_array($indexDefinitionPart)) {
// limited length of field
$indexDefinitionPartValue = substr(
$entryArray[$indexDefinitionPart[0]],
0,
$indexDefinitionPart[1]
);
} else {
$indexDefinitionPartValue = $entryArray[$indexDefinitionPart];
}
if (!isset($reference[$indexDefinitionPartValue])) {
$reference[$indexDefinitionPartValue] = [];
}
$reference = &$reference[$indexDefinitionPartValue];
}
// add value
$reference = $entry;
} else {
// single index
$indexName = $indexDefinition;
// skip empty field
if (empty($entryArray[$indexDefinition])) {
continue;
}
// add to indexUA
$this->index[$indexName][$entryArray[$indexDefinition]] = $entry;
}
}
}
}
}
/**
* @return mixed[]
*
* @throws \InvalidArgumentException If no index found in database
*/
private function getIndex(string $indexedFieldName): array
{
// build index
if ($this->index === null) {
$this->buildIndex();
}
// get index
if (!isset($this->index[$indexedFieldName])) {
throw new \InvalidArgumentException(
sprintf(
'Unknown index "%s" in database "%s"',
$indexedFieldName,
get_class()
)
);
}
return $this->index[$indexedFieldName];
}
/**
* @param string $indexedFieldName
* @param string $fieldValue
*
* @return null|object|object[] null when not found, object when found by single-field index,
* object[] when found by compound index
*/
protected function find(string $indexedFieldName, string $fieldValue)
{
$fieldIndex = $this->getIndex($indexedFieldName);
return $fieldIndex[$fieldValue] ?? null;
}
}

View File

@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace Sokil\IsoCodes;
/**
* Abstract collection of ISO entries loaded from separate files
*/
abstract class AbstractPartitionedDatabase extends AbstractDatabase
{
/**
* @param string $fileName File name of partition without extension
*
* @return array
*/
protected function loadFromJSONFile(string $fileName): array
{
$pathToPartitionFile = sprintf(
'%s/iso_%s/%s.json',
$this->getDatabasesPath(),
$this->getISONumber(),
$fileName
);
if (!file_exists($pathToPartitionFile)) {
return [];
}
return \json_decode(
\file_get_contents($pathToPartitionFile),
true
);
}
}

View File

@@ -0,0 +1,82 @@
<?php
declare(strict_types=1);
namespace Sokil\IsoCodes\Database;
use Sokil\IsoCodes\AbstractNotPartitionedDatabase;
use Sokil\IsoCodes\Database\Countries\Country;
/**
* @method Country|null find(string $indexedFieldName, string $fieldValue)
*/
class Countries extends AbstractNotPartitionedDatabase
{
/**
* ISO Standard Number
*
* @psalm-pure
*/
public static function getISONumber(): string
{
return '3166-1';
}
/**
* @param array<string, string> $entry
*/
protected function arrayToEntry(array $entry): Country
{
return new Country(
$this->translationDriver,
$entry['name'],
$entry['alpha_2'],
$entry['alpha_3'],
$entry['numeric'],
$entry['flag'],
!empty($entry['official_name']) ? $entry['official_name'] : null,
!empty($entry['common_name']) ? $entry['common_name'] : null
);
}
/**
* @return string[]
*/
protected function getIndexDefinition(): array
{
return [
'alpha_2',
'alpha_3',
'numeric'
];
}
public function getByAlpha2(string $alpha2): ?Country
{
return $this->find('alpha_2', $alpha2);
}
public function getByAlpha3(string $alpha3): ?Country
{
return $this->find('alpha_3', $alpha3);
}
/**
* Using int code argument is deprecated due to it can be with leading 0 (e.g. '042').
* Please, use numeric strings.
*
* @param string|int $code
*
* @return Country|null
*
* @throws \TypeError
*/
public function getByNumericCode($code): ?Country
{
if (!is_numeric($code)) {
throw new \TypeError('Argument must be int or string');
}
return $this->find('numeric', (string)$code);
}
}

View File

@@ -0,0 +1,128 @@
<?php
declare(strict_types=1);
namespace Sokil\IsoCodes\Database\Countries;
use Sokil\IsoCodes\Database\Countries;
use Sokil\IsoCodes\TranslationDriver\TranslatorInterface;
class Country
{
/**
* @var string
*/
private $name;
/**
* @var string|null
*/
private $localName;
/**
* @var string
*/
private $alpha2;
/**
* @var string
*/
private $alpha3;
/**
* @var string
*/
private $numericCode;
/**
* Emoji of country flag
*
* @var string
*/
private $flag;
/**
* @var string
*/
private $officialName;
/**
* @var string
*/
private $commonName;
/**
* @var TranslatorInterface
*/
private $translator;
public function __construct(
TranslatorInterface $translator,
string $name,
string $alpha2,
string $alpha3,
string $numericCode,
string $flag,
?string $officialName = null,
?string $commonName = null
) {
$this->translator = $translator;
$this->name = $name;
$this->alpha2 = $alpha2;
$this->alpha3 = $alpha3;
$this->numericCode = $numericCode;
$this->flag = $flag;
$this->officialName = $officialName;
$this->commonName = $commonName;
}
public function getAlpha2(): string
{
return $this->alpha2;
}
public function getAlpha3(): string
{
return $this->alpha3;
}
public function getNumericCode(): string
{
return $this->numericCode;
}
/**
* @return string
*/
public function getFlag(): string
{
return $this->flag;
}
public function getName(): string
{
return $this->name;
}
public function getLocalName(): string
{
if ($this->localName === null) {
$this->localName = $this->translator->translate(
Countries::getISONumber(),
$this->name
);
}
return $this->localName;
}
public function getOfficialName(): ?string
{
return $this->officialName;
}
public function getCommonName(): ?string
{
return $this->commonName;
}
}

View File

@@ -0,0 +1,72 @@
<?php
declare(strict_types=1);
namespace Sokil\IsoCodes\Database;
use Sokil\IsoCodes\AbstractNotPartitionedDatabase;
use Sokil\IsoCodes\Database\Currencies\Currency;
/**
* @method Currency|null find(string $indexedFieldName, string $fieldValue)
*/
class Currencies extends AbstractNotPartitionedDatabase
{
/**
* ISO Standard Number
*
* @psalm-pure
*/
public static function getISONumber(): string
{
return '4217';
}
/**
* @param array<string, string> $entry
*/
protected function arrayToEntry(array $entry): Currency
{
return new Currency(
$this->translationDriver,
$entry['name'],
$entry['alpha_3'],
$entry['numeric']
);
}
/**
* @return string[]
*/
protected function getIndexDefinition(): array
{
return [
'alpha_3',
'numeric'
];
}
public function getByLetterCode(string $code): ?Currency
{
return $this->find('alpha_3', $code);
}
/**
* Using int code argument is deprecated due to it can be with leading 0 (e.g. '042').
* Please, use numeric strings.
*
* @param string|int $code
*
* @return Currency|null
*
* @throws \TypeError
*/
public function getByNumericCode($code): ?Currency
{
if (!is_numeric($code)) {
throw new \TypeError('Argument must be int or string');
}
return $this->find('numeric', (string)$code);
}
}

View File

@@ -0,0 +1,77 @@
<?php
declare(strict_types=1);
namespace Sokil\IsoCodes\Database\Currencies;
use Sokil\IsoCodes\Database\Currencies;
use Sokil\IsoCodes\TranslationDriver\TranslatorInterface;
class Currency
{
/**
* Alpha3
*
* @var string
*/
private $letterCode;
/**
* @var string
*/
private $numericCode;
/**
* @var string
*/
private $name;
/**
* @var string|null
*/
private $localName;
/**
* @var TranslatorInterface
*/
private $translator;
public function __construct(
TranslatorInterface $translator,
string $name,
string $letterCode,
string $numericCode
) {
$this->translator = $translator;
$this->name = $name;
$this->letterCode = $letterCode;
$this->numericCode = $numericCode;
}
public function getName(): string
{
return $this->name;
}
public function getLocalName(): string
{
if ($this->localName === null) {
$this->localName = $this->translator->translate(
Currencies::getISONumber(),
$this->name
);
}
return $this->localName;
}
public function getLetterCode(): string
{
return $this->letterCode;
}
public function getNumericCode(): string
{
return $this->numericCode;
}
}

View File

@@ -0,0 +1,87 @@
<?php
declare(strict_types=1);
namespace Sokil\IsoCodes\Database;
use Sokil\IsoCodes\AbstractNotPartitionedDatabase;
use Sokil\IsoCodes\Database\HistoricCountries\Country;
/**
* @method Country|null find(string $indexedFieldName, string $fieldValue)
*/
class HistoricCountries extends AbstractNotPartitionedDatabase
{
/**
* ISO Standard Number
*
* @psalm-pure
*/
public static function getISONumber(): string
{
return '3166-3';
}
/**
* @param array<string, string> $entry
*/
protected function arrayToEntry(array $entry): Country
{
return new Country(
$this->translationDriver,
$entry['name'],
$entry['alpha_4'],
$entry['alpha_3'],
$entry['alpha_2'],
$entry['withdrawal_date'],
!empty($entry['numeric']) ? $entry['numeric'] : null
);
}
/**
* @return string[]
*/
protected function getIndexDefinition(): array
{
return [
'alpha_4',
'alpha_3',
'alpha_2',
'numeric'
];
}
public function getByAlpha4(string $code): ?Country
{
return $this->find('alpha_4', $code);
}
public function getByAlpha3(string $code): ?Country
{
return $this->find('alpha_3', $code);
}
public function getByAlpha2(string $code): ?Country
{
return $this->find('alpha_2', $code);
}
/**
* Using int code argument is deprecated due to it can be with leading 0 (e.g. '042').
* Please, use numeric strings.
*
* @param string|int $code
*
* @return Country|null
*
* @throws \TypeError
*/
public function getByNumericCode($code): ?Country
{
if (!is_numeric($code)) {
throw new \TypeError('Argument must be int or string');
}
return $this->find('numeric', (string)$code);
}
}

View File

@@ -0,0 +1,111 @@
<?php
declare(strict_types=1);
namespace Sokil\IsoCodes\Database\HistoricCountries;
use Sokil\IsoCodes\Database\HistoricCountries;
use Sokil\IsoCodes\TranslationDriver\TranslatorInterface;
class Country
{
/**
* @var string
*/
private $name;
/**
* @var string|null
*/
private $localName;
/**
* @var string
*/
private $alpha4;
/**
* @var string
*/
private $alpha3;
/**
* @var string
*/
private $alpha2;
/**
* @var string
*/
private $withdrawalDate;
/**
* @var string|null
*/
public $numericCode;
/**
* @var TranslatorInterface
*/
private $translator;
public function __construct(
TranslatorInterface $translator,
string $name,
string $alpha4,
string $alpha3,
string $alpha2,
string $withdrawalDate,
?string $numericCode = null
) {
$this->translator = $translator;
$this->name = $name;
$this->alpha4 = $alpha4;
$this->alpha3 = $alpha3;
$this->alpha2 = $alpha2;
$this->withdrawalDate = $withdrawalDate;
$this->numericCode = $numericCode;
}
public function getName(): string
{
return $this->name;
}
public function getLocalName(): string
{
if ($this->localName === null) {
$this->localName = $this->translator->translate(
HistoricCountries::getISONumber(),
$this->name
);
}
return $this->localName;
}
public function getAlpha4(): string
{
return $this->alpha4;
}
public function getAlpha3(): string
{
return $this->alpha3;
}
public function getAlpha2(): string
{
return $this->alpha2;
}
public function getWithdrawalDate(): string
{
return $this->withdrawalDate;
}
public function getNumericCode(): ?string
{
return $this->numericCode;
}
}

View File

@@ -0,0 +1,61 @@
<?php
declare(strict_types=1);
namespace Sokil\IsoCodes\Database;
use Sokil\IsoCodes\AbstractNotPartitionedDatabase;
use Sokil\IsoCodes\Database\Languages\Language;
/**
* @method Language|null find(string $indexedFieldName, string $fieldValue)
*/
class Languages extends AbstractNotPartitionedDatabase implements LanguagesInterface
{
/**
* ISO Standard Number
*
* @psalm-pure
*/
public static function getISONumber(): string
{
return '639-3';
}
/**
* @param array<string, string> $entry
*/
protected function arrayToEntry(array $entry): Language
{
return new Language(
$this->translationDriver,
$entry['name'],
$entry['alpha_3'],
$entry['scope'],
$entry['type'],
!empty($entry['inverted_name']) ? $entry['inverted_name'] : null,
!empty($entry['alpha_2']) ? $entry['alpha_2'] : null
);
}
/**
* @return string[]
*/
protected function getIndexDefinition(): array
{
return [
'alpha_2',
'alpha_3',
];
}
public function getByAlpha2(string $alpha2): ?Language
{
return $this->find('alpha_2', $alpha2);
}
public function getByAlpha3(string $alpha3): ?Language
{
return $this->find('alpha_3', $alpha3);
}
}

View File

@@ -0,0 +1,146 @@
<?php
declare(strict_types=1);
namespace Sokil\IsoCodes\Database\Languages;
use Sokil\IsoCodes\Database\Languages;
use Sokil\IsoCodes\TranslationDriver\TranslatorInterface;
class Language
{
/**
* @see https://iso639-3.sil.org/about/scope
*/
public const SCOPE_COLLECTIVE = 'C';
public const SCOPE_INDIVIDUAL = 'I';
public const SCOPE_LOCAL = 'L';
public const SCOPE_MACROLANGUAGE = 'M';
public const SCOPE_SPECIAL = 'S';
/**
* @see https://iso639-3.sil.org/about/types
*/
public const TYPE_ANCIENT = 'A';
public const TYPE_CONSTRUCTED = 'C';
public const TYPE_EXTINCT = 'E';
public const TYPE_GENETIC = 'GENETIC'; // not supported
public const TYPE_GENETIC_ANCIENT = 'GENETIC_ANCIENT'; // not supported
public const TYPE_GENETIC_LIKE = 'GENETIC_LIKE'; // not supported
public const TYPE_GEOGRAPHIC = 'GEOGRAPHIC'; // not supported
public const TYPE_HISTORICAL = 'H';
public const TYPE_LIVING = 'L';
public const TYPE_SPECIAL = 'S';
/**
* @var string
*/
private $name;
/**
* @var string|null
*/
private $localName;
/**
* @var string
*/
private $alpha3;
/**
* @var string
*
* Scope of denotation
*
* One of self::SCOPE_*
*
* @see https://iso639-3.sil.org/about/scope
*/
private $scope;
/**
* @var string
*
* Type of language
*
* One of TYPE_*
*
* @see https://iso639-3.sil.org/about/types
*/
private $type;
/**
* @var string
*/
private $invertedName;
/**
* @var string
*/
private $alpha2;
/**
* @var TranslatorInterface
*/
private $translator;
public function __construct(
TranslatorInterface $translator,
string $name,
string $alpha3,
string $scope,
string $type,
?string $invertedName = null,
?string $alpha2 = null
) {
$this->translator = $translator;
$this->name = $name;
$this->alpha3 = $alpha3;
$this->scope = $scope;
$this->type = $type;
$this->invertedName = $invertedName;
$this->alpha2 = $alpha2;
}
public function getName(): string
{
return $this->name;
}
public function getLocalName(): string
{
if ($this->localName === null) {
$this->localName = $this->translator->translate(
Languages::getISONumber(),
$this->name
);
}
return $this->localName;
}
public function getAlpha3(): string
{
return $this->alpha3;
}
public function getScope(): string
{
return $this->scope;
}
public function getType(): string
{
return $this->type;
}
public function getInvertedName(): ?string
{
return $this->invertedName;
}
public function getAlpha2(): ?string
{
return $this->alpha2;
}
}

View File

@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace Sokil\IsoCodes\Database;
use Sokil\IsoCodes\Database\Languages\Language;
interface LanguagesInterface extends \Iterator, \Countable
{
public function getByAlpha2(string $alpha2): ?Language;
public function getByAlpha3(string $alpha3): ?Language;
}

View File

@@ -0,0 +1,63 @@
<?php
declare(strict_types=1);
namespace Sokil\IsoCodes\Database;
use Sokil\IsoCodes\AbstractPartitionedDatabase;
use Sokil\IsoCodes\Database\Languages\Language;
class LanguagesPartitioned extends AbstractPartitionedDatabase implements LanguagesInterface
{
/**
* ISO Standard Number
*
* @psalm-pure
*/
public static function getISONumber(): string
{
return '639-3';
}
/**
* @param array<string, string> $entry
*/
protected function arrayToEntry(array $entry): Language
{
return new Language(
$this->translationDriver,
$entry['name'],
$entry['alpha_3'],
$entry['scope'],
$entry['type'],
!empty($entry['inverted_name']) ? $entry['inverted_name'] : null,
!empty($entry['alpha_2']) ? $entry['alpha_2'] : null
);
}
public function getByAlpha2(string $alpha2): ?Language
{
$language = null;
foreach ($this->loadFromJSONFile('/alpha2/' . $alpha2[0]) as $languageRaw) {
if ($languageRaw['alpha_2'] === $alpha2) {
$language = $this->arrayToEntry($languageRaw);
}
}
return $language;
}
public function getByAlpha3(string $alpha3): ?Language
{
$language = null;
foreach ($this->loadFromJSONFile('/alpha3/' . substr($alpha3, 0, 2)) as $languageRaw) {
if ($languageRaw['alpha_3'] === $alpha3) {
$language = $this->arrayToEntry($languageRaw);
}
}
return $language;
}
}

View File

@@ -0,0 +1,73 @@
<?php
declare(strict_types=1);
namespace Sokil\IsoCodes\Database;
use Sokil\IsoCodes\AbstractNotPartitionedDatabase;
use Sokil\IsoCodes\Database\Scripts\Script;
/**
* @method Script|null find(string $indexedFieldName, string $fieldValue)
*/
class Scripts extends AbstractNotPartitionedDatabase
{
/**
* ISO Standard Number
*
* @psalm-pure
*/
public static function getISONumber(): string
{
return '15924';
}
/**
* @param array<string, string> $entry
*
*/
protected function arrayToEntry(array $entry): Script
{
return new Script(
$this->translationDriver,
$entry['name'],
$entry['alpha_4'],
$entry['numeric']
);
}
/**
* @return string[]
*/
protected function getIndexDefinition(): array
{
return [
'alpha_4',
'numeric'
];
}
public function getByAlpha4(string $alpha4): ?Script
{
return $this->find('alpha_4', $alpha4);
}
/**
* Using int code argument is deprecated due to it can be with leading 0 (e.g. '042').
* Please, use numeric strings.
*
* @param string|int $code
*
* @return Script|null
*
* @throws \TypeError
*/
public function getByNumericCode($code): ?Script
{
if (!is_numeric($code)) {
throw new \TypeError('Argument must be int or string');
}
return $this->find('numeric', (string)$code);
}
}

View File

@@ -0,0 +1,75 @@
<?php
declare(strict_types=1);
namespace Sokil\IsoCodes\Database\Scripts;
use Sokil\IsoCodes\Database\Scripts;
use Sokil\IsoCodes\TranslationDriver\TranslatorInterface;
class Script
{
/**
* @var string
*/
private $name;
/**
* @var string|null
*/
private $localName;
/**
* @var string
*/
private $alpha4;
/**
* @var string
*/
private $numericCode;
/**
* @var TranslatorInterface
*/
private $translator;
public function __construct(
TranslatorInterface $translator,
string $name,
string $alpha4,
string $numericCode
) {
$this->translator = $translator;
$this->name = $name;
$this->alpha4 = $alpha4;
$this->numericCode = $numericCode;
}
public function getName(): string
{
return $this->name;
}
public function getLocalName(): string
{
if ($this->localName === null) {
$this->localName = $this->translator->translate(
Scripts::getISONumber(),
$this->name
);
}
return $this->localName;
}
public function getAlpha4(): string
{
return $this->alpha4;
}
public function getNumericCode(): string
{
return $this->numericCode;
}
}

View File

@@ -0,0 +1,77 @@
<?php
declare(strict_types=1);
namespace Sokil\IsoCodes\Database;
use Sokil\IsoCodes\AbstractNotPartitionedDatabase;
use Sokil\IsoCodes\Database\Subdivisions\Subdivision;
/**
* @method Subdivision|Subdivision[]|null find(string $indexedFieldName, string $fieldValue)
*/
class Subdivisions extends AbstractNotPartitionedDatabase implements SubdivisionsInterface
{
/**
* ISO Standard Number
*
* @psalm-pure
*/
public static function getISONumber(): string
{
return '3166-2';
}
/**
* @param array<string, string> $entry
*/
protected function arrayToEntry(array $entry): Subdivision
{
return new Subdivision(
$this->translationDriver,
$entry['name'],
$entry['code'],
$entry['type'],
!empty($entry['parent']) ? $entry['parent'] : null
);
}
/**
* @return mixed[]
*/
protected function getIndexDefinition(): array
{
return [
'code',
'country_code' => [['code', 2], 'code'],
];
}
/**
* @param string $subdivisionCode in format "alpha2country-subdivision", e.g. "UA-43"
*/
public function getByCode(string $subdivisionCode): ?Subdivision
{
/** @var Subdivision|null $subdivision */
$subdivision = $this->find('code', $subdivisionCode);
return $subdivision;
}
/**
* @param string $alpha2CountryCode e.g. "UA"
*
* @return Subdivision[]
*/
public function getAllByCountryCode(string $alpha2CountryCode): array
{
/** @var Subdivision[]|null $subdivisions */
$subdivisions = $this->find('country_code', $alpha2CountryCode);
if (empty($subdivisions)) {
$subdivisions = [];
}
return $subdivisions;
}
}

View File

@@ -0,0 +1,87 @@
<?php
declare(strict_types=1);
namespace Sokil\IsoCodes\Database\Subdivisions;
use Sokil\IsoCodes\Database\Subdivisions;
use Sokil\IsoCodes\TranslationDriver\TranslatorInterface;
class Subdivision
{
/**
* @var string
*/
private $name;
/**
* @var string|null
*/
private $localName;
/**
* @var string
*/
private $code;
/**
* @var string
*/
private $type;
/**
* @var string|null
*/
private $parent;
/**
* @var TranslatorInterface
*/
private $translator;
public function __construct(
TranslatorInterface $translator,
string $name,
string $code,
string $type,
?string $parent = null
) {
$this->translator = $translator;
$this->name = $name;
$this->code = $code;
$this->type = $type;
$this->parent = $parent;
}
public function getName(): string
{
return $this->name;
}
public function getLocalName(): string
{
if ($this->localName === null) {
$this->localName = $this->translator->translate(
Subdivisions::getISONumber(),
$this->name
);
}
return $this->localName;
}
public function getCode(): string
{
return $this->code;
}
public function getType(): string
{
return $this->type;
}
public function getParent(): ?string
{
return $this->parent;
}
}

View File

@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace Sokil\IsoCodes\Database;
use Sokil\IsoCodes\Database\Subdivisions\Subdivision;
interface SubdivisionsInterface extends \Iterator, \Countable
{
/**
* @param string $subdivisionCode in format "alpha2country-subdivision", e.g. "UA-43"
*/
public function getByCode(string $subdivisionCode): ?Subdivision;
/**
* @param string $alpha2CountryCode e.g. "UA"
*
* @return Subdivision[]
*/
public function getAllByCountryCode(string $alpha2CountryCode): array;
}

View File

@@ -0,0 +1,64 @@
<?php
declare(strict_types=1);
namespace Sokil\IsoCodes\Database;
use Sokil\IsoCodes\AbstractPartitionedDatabase;
use Sokil\IsoCodes\Database\Subdivisions\Subdivision;
class SubdivisionsPartitioned extends AbstractPartitionedDatabase implements SubdivisionsInterface
{
/**
* ISO Standard Number
*
* @psalm-pure
*/
public static function getISONumber(): string
{
return '3166-2';
}
/**
* @param array<string, string> $entry
*/
protected function arrayToEntry(array $entry): Subdivision
{
return new Subdivision(
$this->translationDriver,
$entry['name'],
$entry['code'],
$entry['type'],
!empty($entry['parent']) ? $entry['parent'] : null
);
}
/**
* @param string $subdivisionCode in format "alpha2country-subdivision", e.g. "UA-43"
*/
public function getByCode(string $subdivisionCode): ?Subdivision
{
if (strpos($subdivisionCode, '-') === false) {
return null;
}
[$alpha2CountryCode] = explode('-', $subdivisionCode);
return $this->getAllByCountryCode($alpha2CountryCode)[$subdivisionCode] ?? null;
}
/**
* @param string $alpha2CountryCode e.g. "UA"
*
* @return Subdivision[]
*/
public function getAllByCountryCode(string $alpha2CountryCode): array
{
$subdivisions = [];
foreach ($this->loadFromJSONFile($alpha2CountryCode) as $subdivision) {
$subdivisions[$subdivision['code']] = $this->arrayToEntry($subdivision);
}
return $subdivisions;
}
}

View File

@@ -0,0 +1,148 @@
<?php
declare(strict_types=1);
namespace Sokil\IsoCodes;
use Sokil\IsoCodes\Database\Countries;
use Sokil\IsoCodes\Database\Currencies;
use Sokil\IsoCodes\Database\HistoricCountries;
use Sokil\IsoCodes\Database\Languages;
use Sokil\IsoCodes\Database\LanguagesInterface;
use Sokil\IsoCodes\Database\LanguagesPartitioned;
use Sokil\IsoCodes\Database\Scripts;
use Sokil\IsoCodes\Database\Subdivisions;
use Sokil\IsoCodes\Database\SubdivisionsInterface;
use Sokil\IsoCodes\Database\SubdivisionsPartitioned;
use Sokil\IsoCodes\TranslationDriver\GettextExtensionDriver;
use Sokil\IsoCodes\TranslationDriver\TranslationDriverInterface;
/**
* Factory class to build ISO databases
*/
class IsoCodesFactory
{
/**
* Database splits into partition files.
*
* Fetching some entry will load only little part of database.
* Loaded entries not stored statically.
*
* This scenario may be useful when just few entries need
* to be loaded, for example on web request when one entry fetched.
*
* This may require a lot of file read operations.
*/
public const OPTIMISATION_MEMORY = 1;
/**
* Entire database loaded into memory from single JSON file once.
*
* All entries created and stored into RAM. Next read of save
* entry will just return it without io operations with files and building objects.
*
* This scenario may be useful for daemons to decrease file operations,
* or when most entries will be fetched from database.
*
* This may require a lot of RAM for storing all entries.
*/
public const OPTIMISATION_IO = 2;
/**
* Path to directory with databases
*
* @var string
*/
private $baseDirectory;
/**
* @var TranslationDriverInterface
*/
private $translationDriver;
public function __construct(
?string $baseDirectory = null,
?TranslationDriverInterface $translationDriver = null
) {
$this->baseDirectory = $baseDirectory;
$this->translationDriver = $translationDriver ?? new GettextExtensionDriver();
}
/**
* ISO 3166-1
*/
public function getCountries(): Countries
{
return new Countries($this->baseDirectory, $this->translationDriver);
}
/**
* ISO 3166-2
*
* @param int $optimisation One of self::OPTIMISATION_* constants
*
* @throws \InvalidArgumentException When invalid optimisation specified
*/
public function getSubdivisions(int $optimisation = self::OPTIMISATION_MEMORY): SubdivisionsInterface
{
switch ($optimisation) {
case self::OPTIMISATION_MEMORY:
$database = new SubdivisionsPartitioned($this->baseDirectory, $this->translationDriver);
break;
case self::OPTIMISATION_IO:
$database = new Subdivisions($this->baseDirectory, $this->translationDriver);
break;
default:
throw new \InvalidArgumentException('Invalid optimisation specified');
}
return $database;
}
/**
* ISO 3166-3
*/
public function getHistoricCountries(): HistoricCountries
{
return new HistoricCountries($this->baseDirectory, $this->translationDriver);
}
/**
* ISO 15924
*/
public function getScripts(): Scripts
{
return new Scripts($this->baseDirectory, $this->translationDriver);
}
/**
* ISO 4217
*/
public function getCurrencies(): Currencies
{
return new Currencies($this->baseDirectory, $this->translationDriver);
}
/**
* ISO 639-3
*
* @param int $optimisation One of self::OPTIMISATION_* constants
*
* @throws \InvalidArgumentException When invalid optimisation specified
*/
public function getLanguages(int $optimisation = self::OPTIMISATION_MEMORY): LanguagesInterface
{
switch ($optimisation) {
case self::OPTIMISATION_MEMORY:
$database = new LanguagesPartitioned($this->baseDirectory, $this->translationDriver);
break;
case self::OPTIMISATION_IO:
$database = new Languages($this->baseDirectory, $this->translationDriver);
break;
default:
throw new \InvalidArgumentException('Invalid optimisation specified');
}
return $database;
}
}

View File

@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace Sokil\IsoCodes\TranslationDriver;
/**
* This driver may be used, when localisation of names does not required, and only database of codes is required.
*/
class DummyDriver implements TranslationDriverInterface
{
public function configureDirectory(string $isoNumber, string $directory): void
{
// do nothing
}
public function setLocale(string $locale): void
{
// do nothing
}
/**
* @param string $isoNumber
* @param string $message
*
* @return string
*/
public function translate(string $isoNumber, string $message): string
{
return $message;
}
}

View File

@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
namespace Sokil\IsoCodes\TranslationDriver;
class GettextExtensionDriver implements TranslationDriverInterface
{
public function configureDirectory(string $isoNumber, string $directory): void
{
// add gettext domain
\bindtextdomain(
$isoNumber,
$directory
);
\bind_textdomain_codeset(
$isoNumber,
'UTF-8'
);
}
/**
* Warning: If defined, will configure system locale.
*
* @param string $locale
*/
public function setLocale(string $locale): void
{
$fullLocaleName = sprintf('%s.UTF-8', $locale);
if (\getenv('LANGUAGE') !== $fullLocaleName) {
\putenv(sprintf('LANGUAGE=%s', $fullLocaleName));
}
if (\setlocale(LC_MESSAGES, '0') !== $fullLocaleName) {
\setlocale(LC_MESSAGES, sprintf('%s.UTF-8', $locale));
}
}
/**
* @param string $isoNumber
* @param string $message
*
* @return string
*/
public function translate(string $isoNumber, string $message): string
{
return \dgettext($isoNumber, $message);
}
}

View File

@@ -0,0 +1,94 @@
<?php
declare(strict_types=1);
namespace Sokil\IsoCodes\TranslationDriver;
use Symfony\Component\Translation\Loader\MoFileLoader;
use Symfony\Component\Translation\Translator;
use Symfony\Contracts\Translation\TranslatorInterface;
class SymfonyTranslationDriver implements TranslationDriverInterface
{
/**
* @var Translator
*/
private $translator;
/**
* @var string
*/
private $locale = 'en';
/**
* @param string|null $cacheDirectory useful only if the given $translator is null.
*/
public function __construct(?string $cacheDirectory = null, ?TranslatorInterface $translator = null)
{
$this->translator = $translator ?: new Translator($this->locale, null, $cacheDirectory);
$this->translator->addLoader('mo', new MoFileLoader());
}
public function configureDirectory(string $isoNumber, string $directory): void
{
$locales = [$this->locale];
if (strpos($this->locale, '_') === 2) {
$locales[] = substr($this->locale, 0, 2);
}
$validPathToMoFile = null;
foreach ($locales as $locale) {
$pathToMoFile = $this->getPathToMoFile($directory, $locale, $isoNumber);
if (file_exists($pathToMoFile)) {
$validPathToMoFile = $pathToMoFile;
break;
}
}
if ($validPathToMoFile !== null) {
$this->translator->addResource(
'mo',
$validPathToMoFile,
$locale,
$isoNumber
);
}
}
private function getPathToMoFile(string $directory, string $locale, string $isoNumber): string
{
$pathToMoFile = sprintf(
'%s/%s/LC_MESSAGES/%s.mo',
$directory,
$locale,
$isoNumber
);
return $pathToMoFile;
}
/**
* Warning: If defined, will configure system locale.
*
* @param string $locale
*/
public function setLocale(string $locale): void
{
$this->locale = $locale;
$this->translator->setLocale($locale);
}
/**
* @param string $isoNumber
* @param string $message
*
* @return string
*/
public function translate(string $isoNumber, string $message): string
{
return $this->translator->trans($message, [], $isoNumber);
}
}

View File

@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace Sokil\IsoCodes\TranslationDriver;
interface TranslationDriverInterface extends TranslatorInterface
{
public function configureDirectory(string $isoNumber, string $directory): void;
/**
* @param string $locale
*/
public function setLocale(string $locale): void;
}

View File

@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace Sokil\IsoCodes\TranslationDriver;
interface TranslatorInterface
{
/**
* @param string $isoNumber
* @param string $message
*
* @return string
*/
public function translate(string $isoNumber, string $message): string;
}