Merge branch 'master' of https://git.mcnd.ca/mcndave/picea
This commit is contained in:
commit
1611ebe047
|
@ -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')),
|
||||
|
||||
|
|
|
@ -20,6 +20,8 @@ abstract class Context {
|
|||
|
||||
public string $filePath = "";
|
||||
|
||||
public string $classPath = "";
|
||||
|
||||
public array $switchStack = [];
|
||||
|
||||
public array $iterateStack = [];
|
||||
|
|
|
@ -1,43 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Picea\Extension;
|
||||
|
||||
use Picea\Compiler\Context;
|
||||
|
||||
class MoneyExtension implements Extension, FunctionExtension {
|
||||
|
||||
public string $token = "money";
|
||||
|
||||
public string $title = "";
|
||||
|
||||
public string $locale;
|
||||
|
||||
public \NumberFormatter $formatter;
|
||||
|
||||
public function __construct() {
|
||||
$this->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 "<?php echo money($arguments) ?>";
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
<?php
|
||||
|
||||
namespace Picea\Extension;
|
||||
|
||||
use Picea\Compiler\Context;
|
||||
|
||||
class NumberExtension implements Extension, FunctionExtension {
|
||||
|
||||
public string $locale;
|
||||
|
||||
public \NumberFormatter $formatter;
|
||||
|
||||
public function __construct(
|
||||
public string|array $token = [ "money", "format", "format_zero" ],
|
||||
public string $title = "",
|
||||
) {
|
||||
$this->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 "<?php echo format($arguments) ?>";
|
||||
|
||||
case "format_zero":
|
||||
return "<?php echo format_zero($arguments) ?>";
|
||||
|
||||
case "money":
|
||||
return "<?php echo money($arguments) ?>";
|
||||
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue