- Addedd a new Money extension and a new Function token

This commit is contained in:
Dave M. 2022-06-03 15:31:50 +00:00
parent 48ecd9f7d9
commit b08325049b
3 changed files with 89 additions and 0 deletions

View File

@ -0,0 +1,47 @@
<?php
namespace Picea\ControlStructure;
class FunctionToken implements ControlStructure {
public array $token = [ "function", "endfunction", "return" ];
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
switch($token) {
case "function":
return $this->printFunction($context, $arguments);
case "return":
if ( empty($context->functions) ) {
throw new \RuntimeException("A function return tag {% return %} was found without an opening {% function ... %} tag");
}
return $this->printReturn($context, $arguments);
case "endfunction":
if ( empty($context->functions) ) {
throw new \RuntimeException("A function closing tag {% endfunction %} was found without an opening {% function ... %} tag");
}
$context->functions--;
return $this->printEndFunction($context);
}
}
protected function printFunction($context, ?string $arguments) : string
{
$context->functions++;
return "<?php function $arguments { ?>";
}
protected function printReturn($context, ?string $arguments) : string
{
return "<?php return $arguments; ?>";
}
protected function printEndFunction($context) : string
{
return "<?php } ?>";
}
}

View File

@ -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));
}
}

View File

@ -52,6 +52,7 @@ class DefaultRegistrations implements LanguageRegistration
$compiler->registerControlStructure(new \Picea\ControlStructure\ContinueToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\ExtendsToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\SectionToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\FunctionToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\BlockToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\IncludeToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\ViewToken());