|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Bakame\Intl\Laravel; |
| 6 | + |
| 7 | +use Bakame\Intl\DateFactory; |
| 8 | +use Bakame\Intl\DateResolver; |
| 9 | +use Bakame\Intl\Formatter; |
| 10 | +use Bakame\Intl\NumberFactory; |
| 11 | +use Bakame\Intl\Option\AttributeFormat; |
| 12 | +use Bakame\Intl\Option\CalendarFormat; |
| 13 | +use Bakame\Intl\Option\DateFormat; |
| 14 | +use Bakame\Intl\Option\PaddingPosition; |
| 15 | +use Bakame\Intl\Option\RoundingMode; |
| 16 | +use Bakame\Intl\Option\StyleFormat; |
| 17 | +use Bakame\Intl\Option\SymbolFormat; |
| 18 | +use Bakame\Intl\Option\TextFormat; |
| 19 | +use Bakame\Intl\Option\TimeFormat; |
| 20 | +use Locale; |
| 21 | +use Money\Currencies\ISOCurrencies; |
| 22 | +use Money\Formatter\IntlMoneyFormatter; |
| 23 | + |
| 24 | +final class Factory |
| 25 | +{ |
| 26 | + /** @readonly */ |
| 27 | + public DateFactory $dateFactory; |
| 28 | + /** @readonly */ |
| 29 | + public NumberFactory $numberFactory; |
| 30 | + |
| 31 | + public function __construct(DateFactory $dateFactory, NumberFactory $numberFactory) |
| 32 | + { |
| 33 | + $this->dateFactory = $dateFactory; |
| 34 | + $this->numberFactory = $numberFactory; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @param array{ |
| 39 | + * date:array{ |
| 40 | + * dateFormat:key-of<DateFormat::INTL_MAPPER>, |
| 41 | + * timeFormat:key-of<TimeFormat::INTL_MAPPER>, |
| 42 | + * calendar:key-of<CalendarFormat::INTL_MAPPER>, |
| 43 | + * pattern?:?string, |
| 44 | + * }, |
| 45 | + * number:array{ |
| 46 | + * style:key-of<StyleFormat::INTL_MAPPER>, |
| 47 | + * pattern?:?string, |
| 48 | + * attributes?:array<key-of<AttributeFormat::INTL_MAPPER>, int|float|key-of<RoundingMode::INTL_MAPPER>|key-of<PaddingPosition::INTL_MAPPER>>, |
| 49 | + * textAttributes?:array<key-of<TextFormat::INTL_MAPPER>, string>, |
| 50 | + * symbolAttributes?:array<key-of<SymbolFormat::INTL_MAPPER>, string> |
| 51 | + * } |
| 52 | + * } $settings |
| 53 | + */ |
| 54 | + public static function fromAssociative(array $settings): self |
| 55 | + { |
| 56 | + return new self( |
| 57 | + DateFactory::fromAssociative($settings['date']), |
| 58 | + NumberFactory::fromAssociative($settings['number']) |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + public function newFormatter(DateResolver $dateResolver): Formatter |
| 63 | + { |
| 64 | + return new Formatter($this->dateFactory, $this->numberFactory, $dateResolver); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @param array<key-of<AttributeFormat::INTL_MAPPER>, int|float|key-of<RoundingMode::INTL_MAPPER>|key-of<PaddingPosition::INTL_MAPPER>> $attrs |
| 69 | + */ |
| 70 | + public function newIntlMoneyFormatter(?string $locale = null, ?string $style = null, array $attrs = []): IntlMoneyFormatter |
| 71 | + { |
| 72 | + $locale = $locale ?? Locale::getDefault(); |
| 73 | + $hash = $locale.'|'.json_encode($style).'|'.json_encode($attrs); |
| 74 | + static $instances = []; |
| 75 | + if (!isset($instances[$hash])) { |
| 76 | + $instances[$hash] = new IntlMoneyFormatter( |
| 77 | + $this->numberFactory->createNumberFormatter($locale, $style, $attrs), |
| 78 | + new ISOCurrencies() |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + return $instances[$hash]; |
| 83 | + } |
| 84 | +} |
0 commit comments