Skip to content

Commit

Permalink
Add Warmable interface
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Aug 7, 2024
1 parent 359116d commit 0c485d0
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 8 deletions.
1 change: 1 addition & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
### New features

- `LocaleId` is an enum of available locales.
- Added the `Warmable` interface to features that can warm the CLDR cache.

### Backward Incompatible Changes

Expand Down
22 changes: 17 additions & 5 deletions lib/CurrencyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace ICanBoogie\CLDR;

use Closure;
use ICanBoogie\Accessor\AccessorTrait;

use function array_combine;
Expand All @@ -36,7 +37,7 @@
*
* @property-read string[] $codes Alphabetic list of currency codes.
*/
final class CurrencyCollection extends AbstractCollection
final class CurrencyCollection extends AbstractCollection implements Warmable
{
/**
* @uses get_codes
Expand Down Expand Up @@ -70,10 +71,7 @@ public function __construct(
private function get_codes(): array
{
$make = function () {
$codes = array_keys($this->repository->fetch(
'numbers/en-001/currencies',
'main/en-001/numbers/currencies'
));
$codes = array_keys($this->fetch_currencies());

return array_combine($codes, $codes);
};
Expand Down Expand Up @@ -105,4 +103,18 @@ public function assert_defined(string $currency_code): void
throw new CurrencyNotDefined($currency_code);
}
}

private function fetch_currencies(): array

Check failure on line 107 in lib/CurrencyCollection.php

View workflow job for this annotation

GitHub Actions / phpstan

Method ICanBoogie\CLDR\CurrencyCollection::fetch_currencies() return type has no value type specified in iterable type array.
{
return $this->repository->fetch(
'numbers/en-001/currencies',
'main/en-001/numbers/currencies'
);
}

public function warm_up(Closure $progress): void
{
$progress("Warming up currencies");
$this->fetch_currencies();
}
}
17 changes: 15 additions & 2 deletions lib/Locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

namespace ICanBoogie\CLDR;

use Closure;
use ICanBoogie\Accessor\AccessorTrait;
use InvalidArgumentException;
use LogicException;

use function str_replace;
Expand Down Expand Up @@ -40,7 +40,7 @@
* @property-read Units $units
* @uses self::get_units()
*/
class Locale extends AbstractSectionCollection
class Locale extends AbstractSectionCollection implements Warmable
{
use AccessorTrait;

Expand Down Expand Up @@ -94,6 +94,19 @@ public function offsetExists($offset): bool
return isset(self::OFFSET_MAPPING[$offset]);
}

/**
* Warm up with locale relevant data.
*/
public function warm_up(Closure $progress): void
{
$progress("Warming up locale '{$this->id->value}':");

foreach (array_keys(self::OFFSET_MAPPING) as $offset) {
$progress("- $offset");
$this[$offset];

Check failure on line 106 in lib/Locale.php

View workflow job for this annotation

GitHub Actions / phpstan

Expression "$this[$offset]" on a separate line does not do anything.
}
}

protected function path_for(string $offset): string
{
return str_replace('{locale}', $this->id->value, self::OFFSET_MAPPING[$offset][0]);
Expand Down
13 changes: 12 additions & 1 deletion lib/Supplemental.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace ICanBoogie\CLDR;

use Closure;
use ICanBoogie\Accessor\AccessorTrait;
use ICanBoogie\CLDR\Supplemental\CurrencyData;

Expand All @@ -30,7 +31,7 @@
* @property-read CurrencyData $currency_data
* @uses self::get_currency_data()
*/
final class Supplemental extends AbstractSectionCollection
final class Supplemental extends AbstractSectionCollection implements Warmable
{
/**
* @uses get_currency_data
Expand Down Expand Up @@ -86,6 +87,16 @@ public function offsetExists($offset): bool
return isset(self::OFFSET_MAPPING[$offset]);
}

public function warm_up(Closure $progress): void
{
$progress("Warming up supplemental:");

foreach (array_keys(self::OFFSET_MAPPING) as $offset) {
$progress("- $offset");
$this[$offset];

Check failure on line 96 in lib/Supplemental.php

View workflow job for this annotation

GitHub Actions / phpstan

Expression "$this[$offset]" on a separate line does not do anything.
}
}

protected function path_for(string $offset): string
{
return "core/supplemental/$offset";
Expand Down
16 changes: 16 additions & 0 deletions lib/Warmable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace ICanBoogie\CLDR;

use Closure;

/**
* An interface for components that can warm the CLDR cache.
*/
interface Warmable
{
/**
* @param Closure(string $progress):void $progress
*/
public function warm_up(Closure $progress): void;
}
9 changes: 9 additions & 0 deletions tests/CurrencyCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,13 @@ public function test_assert_defined_success(): void
$this->sut->assert_defined('EUR');
$this->assertTrue(true);
}

public function test_warm_up(): void
{
$n = 0;

$this->sut->warm_up(function() use (&$n) { $n++; });

$this->assertEquals(1, $n);
}
}
9 changes: 9 additions & 0 deletions tests/LocaleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,13 @@ public function test_context_transform(): void
)
);
}

public function test_warm_up(): void
{
$n = 0;

self::$locale->warm_up(function() use (&$n) { $n++; });

$this->assertEquals(30, $n);
}
}
9 changes: 9 additions & 0 deletions tests/SupplementalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,13 @@ public function test_should_throw_exception_in_attempt_to_unset_offset(): void
$this->expectException(OffsetNotWritable::class);
unset($s['timeData']);
}

public function test_warm_up(): void
{
$n = 0;

self::$sut->warm_up(function() use (&$n) { $n++; });

$this->assertEquals(28, $n);
}
}

0 comments on commit 0c485d0

Please sign in to comment.