- Addedd a new Money extension and a new Function token
This commit is contained in:
1 parent
48ecd9f7d9
commit
b08325049b
3 files changed
+89
No files matched your search
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Picea\Extension;
|
||||
|
||||
use Picea\Compiler\Context;
|
||||
|
||||
class MoneyExtension implements Extension {
|
||||
|
||||
public string $token = "money";
|
||||
|
||||
public string $title = "";
|
||||
|
||||
public string $locale;
|
||||
|
||||
public \NumberFormatter $formatter;
|
||||
|
||||
public function __construct(Context $context) {
|
||||
$this->register($context);
|
||||
$this->locale = explode('.', \Locale::getDefault())[0];
|
||||
$this->formatter = new \NumberFormatter($this->locale, \NumberFormatter::CURRENCY);
|
||||
}
|
||||
|
||||
public function register(Context $context) : void
|
||||
{
|
||||
$context->pushFunction("money", [ $this, 'money' ]);
|
||||
}
|
||||
|
||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
||||
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));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user