Skip to content

Commit

Permalink
fix get and find numeric code
Browse files Browse the repository at this point in the history
  • Loading branch information
sokil committed Dec 20, 2020
1 parent cb86be9 commit 95f496b
Show file tree
Hide file tree
Showing 20 changed files with 140 additions and 48 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/AbstractDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function __construct(
abstract public static function getISONumber(): string;

/**
* @param mixed[] $entry
* @param array<string, string> $entry
*
* @return object
*/
Expand Down
11 changes: 8 additions & 3 deletions src/AbstractNotPartitionedDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -64,9 +63,11 @@ private function getIndex(string $indexedFieldName): array
} else {
$indexDefinitionPartValue = $entryArray[$indexDefinitionPart];
}

if (!isset($reference[$indexDefinitionPartValue])) {
$reference[$indexDefinitionPartValue] = [];
}

$reference = &$reference[$indexDefinitionPartValue];
}

Expand All @@ -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;
}
Expand All @@ -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);

Expand Down
7 changes: 5 additions & 2 deletions src/Database/Countries.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,7 +18,7 @@ public static function getISONumber(): string
}

/**
* @param string[] $entry
* @param array<string, string> $entry
*/
protected function arrayToEntry(array $entry): Country
{
Expand Down Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/Database/Countries/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Country
private $name;

/**
* @var string
* @var string|null
*/
private $localName;

Expand Down
25 changes: 21 additions & 4 deletions src/Database/Currencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,15 +18,15 @@ public static function getISONumber(): string
}

/**
* @param mixed[] $entry
* @param array<string, string> $entry
*/
protected function arrayToEntry(array $entry): Currency
{
return new Currency(
$this->translationDriver,
$entry['name'],
$entry['alpha_3'],
(int)$entry['numeric']
$entry['numeric']
);
}

Expand All @@ -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);
}
}
10 changes: 6 additions & 4 deletions src/Database/Currencies/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
class Currency
{
/**
* Alpha3
*
* @var string
*/
private $letterCode;

/**
* @var integer
* @var string
*/
private $numericCode;

Expand All @@ -25,7 +27,7 @@ class Currency
private $name;

/**
* @var string
* @var string|null
*/
private $localName;

Expand All @@ -38,7 +40,7 @@ public function __construct(
TranslatorInterface $translator,
string $name,
string $letterCode,
int $numericCode
string $numericCode
) {
$this->translator = $translator;
$this->name = $name;
Expand Down Expand Up @@ -68,7 +70,7 @@ public function getLetterCode(): string
return $this->letterCode;
}

public function getNumericCode(): int
public function getNumericCode(): string
{
return $this->numericCode;
}
Expand Down
25 changes: 21 additions & 4 deletions src/Database/HistoricCountries.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,7 +18,7 @@ public static function getISONumber(): string
}

/**
* @param string[] $entry
* @param array<string, string> $entry
*/
protected function arrayToEntry(array $entry): Country
{
Expand All @@ -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
);
}

Expand Down Expand Up @@ -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);
}
}
8 changes: 4 additions & 4 deletions src/Database/HistoricCountries/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Country
private $name;

/**
* @var string
* @var string|null
*/
private $localName;

Expand All @@ -40,7 +40,7 @@ class Country
private $withdrawalDate;

/**
* @var integer|null
* @var string|null
*/
public $numericCode;

Expand All @@ -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;
Expand Down Expand Up @@ -104,7 +104,7 @@ public function getWithdrawalDate(): string
return $this->withdrawalDate;
}

public function getNumericCode(): ?int
public function getNumericCode(): ?string
{
return $this->numericCode;
}
Expand Down
5 changes: 4 additions & 1 deletion src/Database/Languages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,7 +18,7 @@ public static function getISONumber(): string
}

/**
* @param string[] $entry
* @param array<string, string> $entry
*/
protected function arrayToEntry(array $entry): Language
{
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Languages/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Language
private $name;

/**
* @var string
* @var string|null
*/
private $localName;

Expand Down
2 changes: 1 addition & 1 deletion src/Database/LanguagesPartitioned.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static function getISONumber(): string
}

/**
* @param string[] $entry
* @param array<string, string> $entry
*/
protected function arrayToEntry(array $entry): Language
{
Expand Down
26 changes: 22 additions & 4 deletions src/Database/Scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,15 +18,16 @@ public static function getISONumber(): string
}

/**
* @param string[] $entry
* @param array<string, string> $entry
*
*/
protected function arrayToEntry(array $entry): Script
{
return new Script(
$this->translationDriver,
$entry['name'],
$entry['alpha_4'],
(int)$entry['numeric']
$entry['numeric']
);
}

Expand All @@ -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);
}
}
Loading

0 comments on commit 95f496b

Please sign in to comment.