diff --git a/CHANGELOG.md b/CHANGELOG.md index 093b71bc..01c4911e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ -## 3.2.1-RC (2020-12-20) +## 3.3.0-RC (2020-12-20) * Fix configure external directory to database files +* `SomeDatabase::getByNumericCode` now accept strings instead of ints, `SomeEntity::getNumericCode` now return strings ## 3.2.0 (2020-12-13) * Allow to configure own translation driver diff --git a/src/AbstractDatabase.php b/src/AbstractDatabase.php index 5a154aea..922bfeb4 100644 --- a/src/AbstractDatabase.php +++ b/src/AbstractDatabase.php @@ -69,7 +69,7 @@ public function __construct( abstract public static function getISONumber(): string; /** - * @param mixed[] $entry + * @param array $entry * * @return object */ diff --git a/src/AbstractNotPartitionedDatabase.php b/src/AbstractNotPartitionedDatabase.php index b5475fd7..e02feace 100644 --- a/src/AbstractNotPartitionedDatabase.php +++ b/src/AbstractNotPartitionedDatabase.php @@ -50,7 +50,6 @@ private function getIndex(string $indexedFieldName): array 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) { @@ -64,9 +63,11 @@ private function getIndex(string $indexedFieldName): array } else { $indexDefinitionPartValue = $entryArray[$indexDefinitionPart]; } + if (!isset($reference[$indexDefinitionPartValue])) { $reference[$indexDefinitionPartValue] = []; } + $reference = &$reference[$indexDefinitionPartValue]; } @@ -75,10 +76,12 @@ private function getIndex(string $indexedFieldName): array } else { // single index $indexName = $indexDefinition; + // skip empty field if (empty($entryArray[$indexDefinition])) { continue; } + // add to indexUA $this->index[$indexName][$entryArray[$indexDefinition]] = $entry; } @@ -102,11 +105,13 @@ private function getIndex(string $indexedFieldName): array } /** + * @param string $indexedFieldName * @param string $fieldValue * - * @return object|mixed[]|null + * @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, $fieldValue) + protected function find(string $indexedFieldName, string $fieldValue) { $fieldIndex = $this->getIndex($indexedFieldName); diff --git a/src/Database/Countries.php b/src/Database/Countries.php index 09a93f42..368aa17f 100644 --- a/src/Database/Countries.php +++ b/src/Database/Countries.php @@ -7,6 +7,9 @@ use Sokil\IsoCodes\AbstractNotPartitionedDatabase; use Sokil\IsoCodes\Database\Countries\Country; +/** + * @method Country|null find(string $indexedFieldName, string $fieldValue) + */ class Countries extends AbstractNotPartitionedDatabase { public static function getISONumber(): string @@ -15,7 +18,7 @@ public static function getISONumber(): string } /** - * @param string[] $entry + * @param array $entry */ protected function arrayToEntry(array $entry): Country { @@ -67,6 +70,6 @@ public function getByNumericCode($code): ?Country throw new \TypeError('Argument must be int or string'); } - return $this->find('numeric', $code); + return $this->find('numeric', (string)$code); } } diff --git a/src/Database/Countries/Country.php b/src/Database/Countries/Country.php index 7203907d..3147598c 100644 --- a/src/Database/Countries/Country.php +++ b/src/Database/Countries/Country.php @@ -15,7 +15,7 @@ class Country private $name; /** - * @var string + * @var string|null */ private $localName; diff --git a/src/Database/Currencies.php b/src/Database/Currencies.php index e8bfca6e..e955ccdf 100644 --- a/src/Database/Currencies.php +++ b/src/Database/Currencies.php @@ -7,6 +7,9 @@ use Sokil\IsoCodes\AbstractNotPartitionedDatabase; use Sokil\IsoCodes\Database\Currencies\Currency; +/** + * @method Currency|null find(string $indexedFieldName, string $fieldValue) + */ class Currencies extends AbstractNotPartitionedDatabase { public static function getISONumber(): string @@ -15,7 +18,7 @@ public static function getISONumber(): string } /** - * @param mixed[] $entry + * @param array $entry */ protected function arrayToEntry(array $entry): Currency { @@ -23,7 +26,7 @@ protected function arrayToEntry(array $entry): Currency $this->translationDriver, $entry['name'], $entry['alpha_3'], - (int)$entry['numeric'] + $entry['numeric'] ); } @@ -43,8 +46,22 @@ public function getByLetterCode(string $code): ?Currency return $this->find('alpha_3', $code); } - public function getByNumericCode(int $code): ?Currency + /** + * 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 { - return $this->find('numeric', $code); + if (!is_numeric($code)) { + throw new \TypeError('Argument must be int or string'); + } + + return $this->find('numeric', (string)$code); } } diff --git a/src/Database/Currencies/Currency.php b/src/Database/Currencies/Currency.php index 70b3e70b..dc86d50a 100644 --- a/src/Database/Currencies/Currency.php +++ b/src/Database/Currencies/Currency.php @@ -10,12 +10,14 @@ class Currency { /** + * Alpha3 + * * @var string */ private $letterCode; /** - * @var integer + * @var string */ private $numericCode; @@ -25,7 +27,7 @@ class Currency private $name; /** - * @var string + * @var string|null */ private $localName; @@ -38,7 +40,7 @@ public function __construct( TranslatorInterface $translator, string $name, string $letterCode, - int $numericCode + string $numericCode ) { $this->translator = $translator; $this->name = $name; @@ -68,7 +70,7 @@ public function getLetterCode(): string return $this->letterCode; } - public function getNumericCode(): int + public function getNumericCode(): string { return $this->numericCode; } diff --git a/src/Database/HistoricCountries.php b/src/Database/HistoricCountries.php index 25c75214..9b26e8b7 100644 --- a/src/Database/HistoricCountries.php +++ b/src/Database/HistoricCountries.php @@ -7,6 +7,9 @@ use Sokil\IsoCodes\AbstractNotPartitionedDatabase; use Sokil\IsoCodes\Database\HistoricCountries\Country; +/** + * @method Country|null find(string $indexedFieldName, string $fieldValue) + */ class HistoricCountries extends AbstractNotPartitionedDatabase { public static function getISONumber(): string @@ -15,7 +18,7 @@ public static function getISONumber(): string } /** - * @param string[] $entry + * @param array $entry */ protected function arrayToEntry(array $entry): Country { @@ -26,7 +29,7 @@ protected function arrayToEntry(array $entry): Country $entry['alpha_3'], $entry['alpha_2'], $entry['withdrawal_date'], - !empty($entry['numeric']) ? (int)$entry['numeric'] : null + !empty($entry['numeric']) ? $entry['numeric'] : null ); } @@ -58,8 +61,22 @@ public function getByAlpha2(string $code): ?Country return $this->find('alpha_2', $code); } - public function getByNumericCode(int $code): ?Country + /** + * 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 { - return $this->find('numeric', $code); + if (!is_numeric($code)) { + throw new \TypeError('Argument must be int or string'); + } + + return $this->find('numeric', (string)$code); } } diff --git a/src/Database/HistoricCountries/Country.php b/src/Database/HistoricCountries/Country.php index 494d3c37..111917ee 100644 --- a/src/Database/HistoricCountries/Country.php +++ b/src/Database/HistoricCountries/Country.php @@ -15,7 +15,7 @@ class Country private $name; /** - * @var string + * @var string|null */ private $localName; @@ -40,7 +40,7 @@ class Country private $withdrawalDate; /** - * @var integer|null + * @var string|null */ public $numericCode; @@ -56,7 +56,7 @@ public function __construct( string $alpha3, string $alpha2, string $withdrawalDate, - ?int $numericCode = null + ?string $numericCode = null ) { $this->translator = $translator; $this->name = $name; @@ -104,7 +104,7 @@ public function getWithdrawalDate(): string return $this->withdrawalDate; } - public function getNumericCode(): ?int + public function getNumericCode(): ?string { return $this->numericCode; } diff --git a/src/Database/Languages.php b/src/Database/Languages.php index 6c904edd..f6832807 100644 --- a/src/Database/Languages.php +++ b/src/Database/Languages.php @@ -7,6 +7,9 @@ use Sokil\IsoCodes\AbstractNotPartitionedDatabase; use Sokil\IsoCodes\Database\Languages\Language; +/** + * @method Language|null find(string $indexedFieldName, string $fieldValue) + */ class Languages extends AbstractNotPartitionedDatabase implements LanguagesInterface { public static function getISONumber(): string @@ -15,7 +18,7 @@ public static function getISONumber(): string } /** - * @param string[] $entry + * @param array $entry */ protected function arrayToEntry(array $entry): Language { diff --git a/src/Database/Languages/Language.php b/src/Database/Languages/Language.php index 5ae1ea7f..7987e451 100644 --- a/src/Database/Languages/Language.php +++ b/src/Database/Languages/Language.php @@ -38,7 +38,7 @@ class Language private $name; /** - * @var string + * @var string|null */ private $localName; diff --git a/src/Database/LanguagesPartitioned.php b/src/Database/LanguagesPartitioned.php index c718b852..242b861e 100644 --- a/src/Database/LanguagesPartitioned.php +++ b/src/Database/LanguagesPartitioned.php @@ -15,7 +15,7 @@ public static function getISONumber(): string } /** - * @param string[] $entry + * @param array $entry */ protected function arrayToEntry(array $entry): Language { diff --git a/src/Database/Scripts.php b/src/Database/Scripts.php index 5fca31fe..70a9fd08 100644 --- a/src/Database/Scripts.php +++ b/src/Database/Scripts.php @@ -7,6 +7,9 @@ use Sokil\IsoCodes\AbstractNotPartitionedDatabase; use Sokil\IsoCodes\Database\Scripts\Script; +/** + * @method Script|null find(string $indexedFieldName, string $fieldValue) + */ class Scripts extends AbstractNotPartitionedDatabase { public static function getISONumber(): string @@ -15,7 +18,8 @@ public static function getISONumber(): string } /** - * @param string[] $entry + * @param array $entry + * */ protected function arrayToEntry(array $entry): Script { @@ -23,7 +27,7 @@ protected function arrayToEntry(array $entry): Script $this->translationDriver, $entry['name'], $entry['alpha_4'], - (int)$entry['numeric'] + $entry['numeric'] ); } @@ -43,8 +47,22 @@ public function getByAlpha4(string $alpha4): ?Script return $this->find('alpha_4', $alpha4); } - public function getByNumericCode(int $code): ?Script + /** + * 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 { - return $this->find('numeric', $code); + if (!is_numeric($code)) { + throw new \TypeError('Argument must be int or string'); + } + + return $this->find('numeric', (string)$code); } } diff --git a/src/Database/Scripts/Script.php b/src/Database/Scripts/Script.php index 16b3dec5..cf006f98 100644 --- a/src/Database/Scripts/Script.php +++ b/src/Database/Scripts/Script.php @@ -15,7 +15,7 @@ class Script private $name; /** - * @var string + * @var string|null */ private $localName; @@ -25,7 +25,7 @@ class Script private $alpha4; /** - * @var integer + * @var string */ private $numericCode; @@ -38,7 +38,7 @@ public function __construct( TranslatorInterface $translator, string $name, string $alpha4, - int $numericCode + string $numericCode ) { $this->translator = $translator; $this->name = $name; @@ -46,7 +46,6 @@ public function __construct( $this->numericCode = $numericCode; } - public function getName(): string { return $this->name; @@ -69,7 +68,7 @@ public function getAlpha4(): string return $this->alpha4; } - public function getNumericCode(): int + public function getNumericCode(): string { return $this->numericCode; } diff --git a/src/Database/Subdivisions.php b/src/Database/Subdivisions.php index c5a1a668..e08b5b3d 100644 --- a/src/Database/Subdivisions.php +++ b/src/Database/Subdivisions.php @@ -7,6 +7,9 @@ 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 { public static function getISONumber(): string @@ -15,7 +18,7 @@ public static function getISONumber(): string } /** - * @param mixed[] $entry + * @param array $entry */ protected function arrayToEntry(array $entry): Subdivision { diff --git a/src/Database/Subdivisions/Subdivision.php b/src/Database/Subdivisions/Subdivision.php index c9035bf0..3364e115 100644 --- a/src/Database/Subdivisions/Subdivision.php +++ b/src/Database/Subdivisions/Subdivision.php @@ -15,7 +15,7 @@ class Subdivision private $name; /** - * @var string + * @var string|null */ private $localName; diff --git a/src/Database/SubdivisionsPartitioned.php b/src/Database/SubdivisionsPartitioned.php index f56fd7b0..27581c90 100644 --- a/src/Database/SubdivisionsPartitioned.php +++ b/src/Database/SubdivisionsPartitioned.php @@ -15,7 +15,7 @@ public static function getISONumber(): string } /** - * @param mixed[] $entry + * @param array $entry */ protected function arrayToEntry(array $entry): Subdivision { diff --git a/tests/Database/CurrenciesTest.php b/tests/Database/CurrenciesTest.php index 0c4369a2..7eda5ac8 100644 --- a/tests/Database/CurrenciesTest.php +++ b/tests/Database/CurrenciesTest.php @@ -10,7 +10,6 @@ class CurrenciesTest extends TestCase { - public function testIterator(): void { $isoCodes = new IsoCodesFactory(); @@ -28,6 +27,7 @@ public function testGetByLetterCode(): void $isoCodes = new IsoCodesFactory(); $currencies = $isoCodes->getCurrencies(); + $currency = $currencies->getByLetterCode('CZK'); $this->assertInstanceOf( @@ -51,17 +51,28 @@ public function testGetByLetterCode(): void ); $this->assertSame( - 203, + '203', $currency->getNumericCode() ); } - public function testGetByNumericCode(): void + public function getByNumericCodeDataProvider(): array + { + return [ + ['203'], + [203], + ]; + } + + /** + * @dataProvider getByNumericCodeDataProvider + */ + public function testGetByNumericCode($code): void { $isoCodes = new IsoCodesFactory(); $currencies = $isoCodes->getCurrencies(); - $currency = $currencies->getByNumericCode(203); + $currency = $currencies->getByNumericCode($code); $this->assertEquals( 'Czech Koruna', @@ -73,4 +84,17 @@ public function testGetByNumericCode(): void $currency->getLocalName() ); } + + public function testGetByNumericCodeLeadingZeroes(): void + { + $isoCodes = new IsoCodesFactory(); + + $currencies = $isoCodes->getCurrencies(); + $currency = $currencies->getByNumericCode('051'); + + $this->assertEquals( + 'AMD', + $currency->getLetterCode() + ); + } } diff --git a/tests/Database/HistoricCountriesTest.php b/tests/Database/HistoricCountriesTest.php index 789fc259..a597217b 100644 --- a/tests/Database/HistoricCountriesTest.php +++ b/tests/Database/HistoricCountriesTest.php @@ -62,7 +62,7 @@ public function testGetByAlpha4(): void ); $this->assertSame( - 180, + '180', $country->getNumericCode() ); } @@ -134,7 +134,7 @@ public function testGetByNumericCode(): void $isoCodes = new IsoCodesFactory(); $countries = $isoCodes->getHistoricCountries(); - $country = $countries->getByNumericCode(180); + $country = $countries->getByNumericCode('180'); $this->assertInstanceOf( Country::class, @@ -156,7 +156,7 @@ public function testGetLocalByNumericCode(): void $isoCodes = new IsoCodesFactory(); $countries = $isoCodes->getHistoricCountries(); - $country = $countries->getByNumericCode(180); + $country = $countries->getByNumericCode('180'); $this->assertInstanceOf( Country::class, diff --git a/tests/Database/ScriptsTest.php b/tests/Database/ScriptsTest.php index ecd94b06..8c8165f2 100644 --- a/tests/Database/ScriptsTest.php +++ b/tests/Database/ScriptsTest.php @@ -52,7 +52,7 @@ public function testGetByAlpha4(): void ); $this->assertSame( - 239, + '239', $script->getNumericCode() ); } @@ -62,7 +62,7 @@ public function testGetByNumericCode(): void $isoCodes = new IsoCodesFactory(); $scripts = $isoCodes->getScripts(); - $script = $scripts->getByNumericCode(239); + $script = $scripts->getByNumericCode('239'); $this->assertInstanceOf( Script::class,