Skip to content

Commit

Permalink
Tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Aug 10, 2024
1 parent e6360ed commit a5ddb0d
Show file tree
Hide file tree
Showing 34 changed files with 315 additions and 483 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"ext-curl": "*",
"ext-json": "*",
"ext-mbstring": "*",
"icanboogie/common": "^6.0",
"icanboogie/accessor": "^6.0"
},
"require-dev": {
Expand Down
2 changes: 0 additions & 2 deletions generator/src/Command/GenerateCurrency.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
* @var string[] $codes
*
* @link https://github.com/unicode-org/cldr-json/blob/45.0.0/cldr-json/cldr-numbers-full/main/en-001/currencies.json
* @phpstan-ignore-next-line
*/
$codes = array_keys($this->repository->locale_for('en-001')['currencies']);

Expand All @@ -41,7 +40,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
* }> $fractions
*
* @link https://github.com/unicode-org/cldr-json/blob/45.0.0/cldr-json/cldr-core/supplemental/currencyData.json
* @phpstan-ignore-next-line
*/
$fractions = $this->repository->supplemental['currencyData']['fractions'];

Expand Down
1 change: 0 additions & 1 deletion generator/src/Command/GenerateSequenceCompanion.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public function __construct(

protected function execute(InputInterface $input, OutputInterface $output): int
{
// @phpstan-ignore-next-line
$units = $this->repository->locale_for('en-001')['units']['long'];
$methods = [];

Expand Down
1 change: 0 additions & 1 deletion generator/src/Command/GenerateTerritoryCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
* @var string[] $codes
*
* @link https://github.com/unicode-org/cldr-json/blob/45.0.0/cldr-json/cldr-localenames-full/main/en-001/territories.json
* @phpstan-ignore-next-line
*/
$codes = array_keys($this->repository->locale_for('en-001')['territories']);
$codes = array_values(array_filter($codes, fn($code) => !str_contains($code, '-alt')));
Expand Down
1 change: 0 additions & 1 deletion generator/src/Command/GenerateUnitsCompanion.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public function __construct(

protected function execute(InputInterface $input, OutputInterface $output): int
{
// @phpstan-ignore-next-line
$units = $this->repository->locale_for('en-001')['units']['long'];
$properties = [];
$methods = [];
Expand Down
21 changes: 10 additions & 11 deletions src/AbstractSectionCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
namespace ICanBoogie\CLDR;

use ArrayAccess;
use ICanBoogie\OffsetNotDefined;
use LogicException;

/**
* @implements ArrayAccess<string, array>
* @template TKey of array-key
*
* @implements ArrayAccess<TKey, mixed>
*/
abstract class AbstractSectionCollection implements ArrayAccess
{
Expand All @@ -17,7 +19,7 @@ public function __construct(
) {
}

abstract public function offsetExists($offset): bool;
abstract public function offsetExists(mixed $offset): bool;

/**
* @var array<string, array>
Expand All @@ -28,17 +30,14 @@ abstract public function offsetExists($offset): bool;
private array $sections = [];

/**
* @param string $offset
*
* @throws OffsetNotDefined
* @throws LogicException
* @throws ResourceNotFound
*/
#[\ReturnTypeWillChange] // @phpstan-ignore-line
public function offsetGet(
$offset
) {
#[\ReturnTypeWillChange]
public function offsetGet(mixed $offset)
{
if (!$this->offsetExists($offset)) {
throw new OffsetNotDefined(offset: $offset, container: $this);
throw new LogicException("Offset '$offset' does not exist");
}

return $this->sections[$offset] ??= $this->repository->fetch(
Expand Down
16 changes: 7 additions & 9 deletions src/CollectionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace ICanBoogie\CLDR;

use ICanBoogie\OffsetNotWritable;
use LogicException;

/**
* A trait for classes implementing collection.
Expand All @@ -13,20 +13,18 @@ trait CollectionTrait
* @param string $offset
* @param mixed $value
*
* @throw OffsetNotWritable in attempt to set the offset.
* @throw LogicException in an attempt to set the offset.
*/
public function offsetSet($offset, $value): void
public function offsetSet(mixed $offset, mixed $value): void
{
throw new OffsetNotWritable(offset: $offset, container: $this);
throw new LogicException("Offset '$offset' is not writable");
}

/**
* @param string $offset
*
* @throw OffsetNotWritable in attempt to unset the offset.
* @throw LogicException in an attempt to unset the offset.
*/
public function offsetUnset($offset): void
public function offsetUnset(mixed $offset): void
{
throw new OffsetNotWritable(offset: $offset, container: $this);
throw new LogicException("Offset '$offset' is not writable");
}
}
16 changes: 10 additions & 6 deletions src/DateFormatPattern.php → src/DateFormatPatternParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,36 @@
/**
* @link https://www.unicode.org/reports/tr35/tr35-72/tr35-dates.html#Date_Format_Patterns
*/
final class DateFormatPattern
final class DateFormatPatternParser
{
private const QUOTE = "'";

/**
* Parses a date format pattern.
*
* @param string $pattern
* A date format pattern; for example, "yyyy.MM.dd G 'at' HH:mm:ss zzz".
* A date format pattern; for example, "hh 'o''clock' a, zzzz".
*
* @return array<string|array{ string, int }>
* Where _value_ is either a literal or an array where `0` is a pattern character and `1` its length.
*/
public static function tokenize(string $pattern): array
public static function parse(string $pattern): array
{
static $cache = [];

return $cache[$pattern] ??= self::do_tokenize($pattern);
return $cache[$pattern] ??= self::do_parse($pattern);
}

/**
* Parses a date format pattern.
*
* @param string $pattern
* A date format pattern; for example, "yyyy.MM.dd G 'at' HH:mm:ss zzz".
* A date format pattern; for example, "hh 'o''clock' a, zzzz".
*
* @return array<string|array{ string, int }>
* Where _value_ is either a literal or an array where `0` is a pattern character and `1` its length.
*/
private static function do_tokenize(string $pattern): array
private static function do_parse(string $pattern): array
{
$tokens = [];
$is_literal = false;
Expand Down
53 changes: 23 additions & 30 deletions src/DateTimeAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,47 @@
/**
* @property-read int $timestamp Unix timestamp.
* @property-read int $year Year.
* @property-read int $month Month of the year.
* @property-read int $day Day of the month.
* @property-read int $hour Hour of the day.
* @property-read int $minute Minute of the hour.
* @property-read int $second Second of the minute.
* @property-read int $quarter Quarter of the year.
* @property-read int $week Week of the year.
* @property-read int $weekday Day of the week.
* @property-read int<1, 12> $month Month of the year.
* @property-read int<1, 31> $day Day of the month.
* @property-read int<0, 23> $hour Hour of the day.
* @property-read int<1, 59> $minute Minute of the hour.
* @property-read int<1, 59> $second Second of the minute.
* @property-read int<1, 4> $quarter Quarter of the year.
* @property-read int<1, 53> $week Week of the year.
* @property-read int<1, 7> $weekday Day of the week.
* @property-read int $year_day Day of the year.
*
* @method string format(string $format)
*/
class DateTimeAccessor
{
public function __construct(
private readonly DateTimeInterface $datetime
public readonly DateTimeInterface $delegate
) {
}

/**
* @return mixed
*/
public function __get(string $property)
public function __get(string $property): int
{
$dt = $this->datetime;
$f = $this->delegate->format(...);

return match ($property) {
'year' => (int)$dt->format('Y'),
'month' => (int)$dt->format('m'),
'day' => (int)$dt->format('d'),
'hour' => (int)$dt->format('H'),
'minute' => (int)$dt->format('i'),
'second' => (int)$dt->format('s'),
'year' => (int)$f('Y'),
'month' => (int)$f('m'),
'day' => (int)$f('d'),
'hour' => (int)$f('H'),
'minute' => (int)$f('i'),
'second' => (int)$f('s'),
'quarter' => (int)floor(($this->month - 1) / 3) + 1,
'week' => (int)$dt->format('W'),
'year_day' => (int)$dt->format('z') + 1,
'weekday' => (int)$dt->format('w') ?: 7,
'week' => (int)$f('W'),
'year_day' => (int)$f('z') + 1,
'weekday' => (int)$f('w') ?: 7,
default => throw new LogicException("Undefined property: $property"),
};
}

/**
* @param mixed[] $params
*
* @return mixed
* @see DateTimeInterface::format
*/
public function __call(string $name, array $params)
public function format(string $pattern): string
{
return $this->datetime->$name(...$params);
return $this->delegate->format($pattern);
}
}
Loading

0 comments on commit a5ddb0d

Please sign in to comment.