Skip to content

Commit

Permalink
Add LocaleNotAvailable
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Aug 9, 2024
1 parent bc89ac9 commit 7848e85
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 23 deletions.
13 changes: 7 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@

### New features

- `LocaleId` is an enum of available locales.
- `LocaleId` is a representation of an available locale.
- `LocaleNotAvailable` is thrown when a requested locale ID is not available.
- Added the `Warmable` interface to features that can warm the CLDR cache.

### Backward Incompatible Changes

- `Units::LENGTH_*` constants have been replaced by the `UnitLength` enum.
- `LocalizedListFormatter:TYPE_*` constants have been replaced by the `ListType` enum.
- `DateTimeFormatter::WIDTH_*` constants have been replaced by the `DateTimeFormatLength` enum.
- Most functions require a `LocaleId` instead of a locale id string.
- `$clrd->locales['fr']` as been replaced with `$clrd->locale_for('fr')`.
- `Units::LENGTH_*` constants are replaced by the `UnitLength` enum.
- `LocalizedListFormatter:TYPE_*` constants are replaced by the `ListType` enum.
- `DateTimeFormatter::WIDTH_*` constants are replaced by the `DateTimeFormatLength` enum.
- `$clrd->locales['fr']` is replaced by `$clrd->locale_for('fr')`.

### Deprecated Features

Expand All @@ -29,6 +29,7 @@ None

- Use CLDR 45.0.0.
- Some code is now generated from CLDR data, such as `Units` getters and methods, or `LocaleId`.
- JSON data is stored as PHP instead of JSON to leverage opcache.



Expand Down
8 changes: 7 additions & 1 deletion generator/src/Command/GenerateHasContextTransforms.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int

private function render(string $has_by_locale): string
{
$class = __CLASS__;

return <<<PHP
<?php
/** CODE GENERATED; DO NOT EDIT. */
/**
* CODE GENERATED; DO NOT EDIT.
*
* {@see \\$class}
*/
namespace ICanBoogie\CLDR\Locale;
Expand Down
13 changes: 10 additions & 3 deletions generator/src/Command/GenerateLocaleId.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ICanBoogie\CLDR\Generator\Command;

use ICanBoogie\CLDR\LocaleNotAvailable;
use ICanBoogie\CLDR\Repository;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -42,10 +43,16 @@ public function render(
string $parent_locales,
string $available_locales,
): string {
$class = __CLASS__;

return <<<PHP
<?php
/** CODE GENERATED; DO NOT EDIT. */
/**
* CODE GENERATED; DO NOT EDIT.
*
* {@see \\$class}
*/
namespace ICanBoogie\CLDR;
Expand All @@ -66,12 +73,12 @@ public static function is_available(string \$value): bool
}
/**
* @throws InvalidArgumentException if the value is not one of the available locales.
* @throws LocaleNotAvailable
*/
public static function assert_is_available(string \$value): void
{
self::is_available(\$value)
or throw new InvalidArgumentException("Locale is not available: \$value");
or throw new LocaleNotAvailable(\$value);
}
/**
Expand Down
8 changes: 7 additions & 1 deletion generator/src/Command/GenerateSequenceCompanion.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,16 @@ public function $normalized(float|int|string \$number): self
private function render(
string $methods,
): string {
$class = __CLASS__;

return <<<PHP
<?php
/** CODE GENERATED; DO NOT EDIT. */
/**
* CODE GENERATED; DO NOT EDIT.
*
* {@see \\$class}
*/
namespace ICanBoogie\CLDR\Units;
Expand Down
8 changes: 7 additions & 1 deletion generator/src/Command/GenerateUnitsCompanion.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,16 @@ private function render(
string $properties,
string $methods,
): string {
$class = __CLASS__;

return <<<PHP
<?php
/** CODE GENERATED; DO NOT EDIT. */
/**
* CODE GENERATED; DO NOT EDIT.
*
* {@see \\$class}
*/
namespace ICanBoogie\CLDR\Units;
Expand Down
6 changes: 5 additions & 1 deletion lib/Locale/HasContextTransforms.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php

/** CODE GENERATED; DO NOT EDIT. */
/**
* CODE GENERATED; DO NOT EDIT.
*
* {@see \ICanBoogie\CLDR\Generator\Command\GenerateHasContextTransforms}
*/

namespace ICanBoogie\CLDR\Locale;

Expand Down
10 changes: 7 additions & 3 deletions lib/LocaleId.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php

/** CODE GENERATED; DO NOT EDIT. */
/**
* CODE GENERATED; DO NOT EDIT.
*
* {@see \ICanBoogie\CLDR\Generator\Command\GenerateLocaleId}
*/

namespace ICanBoogie\CLDR;

Expand All @@ -21,12 +25,12 @@ public static function is_available(string $value): bool
}

/**
* @throws InvalidArgumentException if the value is not one of the available locales.
* @throws LocaleNotAvailable
*/
public static function assert_is_available(string $value): void
{
self::is_available($value)
or throw new InvalidArgumentException("Locale is not available: $value");
or throw new LocaleNotAvailable($value);
}

/**
Expand Down
28 changes: 28 additions & 0 deletions lib/LocaleNotAvailable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace ICanBoogie\CLDR;

use InvalidArgumentException;
use Throwable;

/**
* Exception thrown when a requested locale ID is not available.
*
* @link https://github.com/unicode-org/cldr-json/blob/45.0.0/cldr-json/cldr-core/availableLocales.json
*/
final class LocaleNotAvailable extends InvalidArgumentException implements Exception
{
/**
* @param string $locale_id
* A locale ID.
*/
public function __construct(
public readonly string $locale_id,
string $message = null,
Throwable $previous = null
) {
$message ??= "Locale ID is not available: $locale_id";

parent::__construct($message, previous: $previous);
}
}
6 changes: 5 additions & 1 deletion lib/Units/SequenceCompanion.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php

/** CODE GENERATED; DO NOT EDIT. */
/**
* CODE GENERATED; DO NOT EDIT.
*
* {@see \ICanBoogie\CLDR\Generator\Command\GenerateSequenceCompanion}
*/

namespace ICanBoogie\CLDR\Units;

Expand Down
6 changes: 5 additions & 1 deletion lib/Units/UnitsCompanion.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php

/** CODE GENERATED; DO NOT EDIT. */
/**
* CODE GENERATED; DO NOT EDIT.
*
* {@see \ICanBoogie\CLDR\Generator\Command\GenerateUnitsCompanion}
*/

namespace ICanBoogie\CLDR\Units;

Expand Down
5 changes: 2 additions & 3 deletions tests/LocaleCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use BadMethodCallException;
use ICanBoogie\CLDR\Locale;
use ICanBoogie\CLDR\LocaleCollection;
use ICanBoogie\CLDR\LocaleNotAvailable;
use ICanBoogie\OffsetNotWritable;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

final class LocaleCollectionTest extends TestCase
Expand Down Expand Up @@ -47,8 +47,7 @@ public function test_existing_locale(): void
*/
public function should_fail_with_undefined_locale(): void
{
$this->expectExceptionMessage("Locale is not available: madonna");
$this->expectException(InvalidArgumentException::class);
$this->expectException(LocaleNotAvailable::class);
self::$sut['madonna']; // @phpstan-ignore-line
}
}
4 changes: 2 additions & 2 deletions tests/LocaleIdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Test\ICanBoogie\CLDR;

use ICanBoogie\CLDR\LocaleId;
use InvalidArgumentException;
use ICanBoogie\CLDR\LocaleNotAvailable;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -31,7 +31,7 @@ public static function provide_test_is_locale_available(): array

public function test_of_fails_on_unavailable_id(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectException(LocaleNotAvailable::class);

LocaleId::of('fr-FR');
}
Expand Down

0 comments on commit 7848e85

Please sign in to comment.