From b4964402fd16f3937fef27d0cd9d44690632201a Mon Sep 17 00:00:00 2001 From: Dave M Date: Wed, 29 Mar 2023 15:13:41 +0000 Subject: [PATCH] - Renamed MoneyExtension to NumberExtension --- docs/99-dependency-injection.md | 6 +-- src/Extension/MoneyExtension.php | 43 -------------------- src/Extension/NumberExtension.php | 67 +++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 46 deletions(-) delete mode 100644 src/Extension/MoneyExtension.php create mode 100644 src/Extension/NumberExtension.php diff --git a/docs/99-dependency-injection.md b/docs/99-dependency-injection.md index fb1e70e..5e45e98 100644 --- a/docs/99-dependency-injection.md +++ b/docs/99-dependency-injection.md @@ -10,7 +10,7 @@ use function DI\autowire, DI\create, DI\get; use Laminas\Diactoros\Response\HtmlResponse; use Picea\{ Picea, Caching\Cache, Caching\Opcache, Compiler, Compiler\Context, Compiler\BaseContext, FileFetcher, Language\DefaultRegistrations, Method\Request }; -use Picea\Extension\{ LanguageHandler, LanguageExtension, TitleExtension, MoneyExtension, UrlExtension }; +use Picea\Extension\{ LanguageHandler, LanguageExtension, TitleExtension, NumberExtension, UrlExtension }; use Picea\Ui\{ Method, Ui }; return [ @@ -26,7 +26,7 @@ return [ return new Compiler(new class(array_merge([ $c->get(LanguageExtension::class), $c->get(TitleExtension::class), - $c->get(MoneyExtension::class), + $c->get(NumberExtension::class), $c->get(UrlExtension::class), $c->get(Method\Form::class), $c->get(Method\Pagination::class), @@ -68,7 +68,7 @@ return [ TitleExtension::class => autowire(TitleExtension::class), - MoneyExtension::class => autowire(MoneyExtension::class), + NumberExtension::class => autowire(NumberExtension::class), UrlExtension::class => create(UrlExtension::class)->constructor(get(Context::class), getenv("URL_BASE"), get('git.commit')), diff --git a/src/Extension/MoneyExtension.php b/src/Extension/MoneyExtension.php deleted file mode 100644 index 7c2173d..0000000 --- a/src/Extension/MoneyExtension.php +++ /dev/null @@ -1,43 +0,0 @@ -locale = explode('.', \Locale::getDefault())[0]; - $this->formatter = new \NumberFormatter($this->locale, \NumberFormatter::CURRENCY); - } - - public function exportFunctions(): array - { - return [ - "money" => [ $this, 'money' ] - ]; - } - - public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string - { - return ""; - } - - public function money(float $money, ? string $currency = null) /* : string|false */ - { - $this->formatter->setTextAttribute(\NumberFormatter::CURRENCY_CODE, 'CAD'); - - $this->formatter->setPattern( str_replace('¤#','¤ #', $this->formatter->getPattern() ) ); - - return $this->formatter->formatCurrency($money, $currency ?? $this->formatter->getTextAttribute(\NumberFormatter::CURRENCY_CODE)); - } - -} diff --git a/src/Extension/NumberExtension.php b/src/Extension/NumberExtension.php new file mode 100644 index 0000000..d328412 --- /dev/null +++ b/src/Extension/NumberExtension.php @@ -0,0 +1,67 @@ +locale = explode('.', \Locale::getDefault())[0]; + } + + public function exportFunctions(): array + { + return [ + "money" => [ $this, 'money' ], + "format" => [ $this, 'format' ], + "format_zero" => [ $this, 'formatZero' ], + ]; + } + + public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string + { + switch($token) { + case "format": + return ""; + + case "format_zero": + return ""; + + case "money": + return ""; + + } + + return ""; + } + + public function format(int|float $num, int $decimals = 0, null|string $decimalSeparator = ".", null|string $thousandsSeparator = ",") : string + { + return number_format($num, $decimals, $decimalSeparator, $thousandsSeparator); + } + + public function formatZero(int $num, int $length) : string + { + return sprintf("%0{$length}d", $num); + } + + public function money(float $money, ? string $currency = null) /* : string|false */ + { + $this->formatter ??= new \NumberFormatter($this->locale, \NumberFormatter::CURRENCY); + + $this->formatter->setTextAttribute(\NumberFormatter::CURRENCY_CODE, 'CAD'); + + $this->formatter->setPattern( str_replace('¤#','¤ #', $this->formatter->getPattern() ) ); + + return $this->formatter->formatCurrency($money, $currency ?? $this->formatter->getTextAttribute(\NumberFormatter::CURRENCY_CODE)); + } + +}