diff --git a/src/ControlStructure/FunctionToken.php b/src/ControlStructure/FunctionToken.php new file mode 100644 index 0000000..da82ea7 --- /dev/null +++ b/src/ControlStructure/FunctionToken.php @@ -0,0 +1,47 @@ +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 ""; + } + + protected function printReturn($context, ?string $arguments) : string + { + return ""; + } + + protected function printEndFunction($context) : string + { + return ""; + } +} \ No newline at end of file diff --git a/src/Extension/MoneyExtension.php b/src/Extension/MoneyExtension.php new file mode 100644 index 0000000..48c62c0 --- /dev/null +++ b/src/Extension/MoneyExtension.php @@ -0,0 +1,41 @@ +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 ""; + } + + 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/Language/DefaultRegistrations.php b/src/Language/DefaultRegistrations.php index 051d071..cd5166a 100644 --- a/src/Language/DefaultRegistrations.php +++ b/src/Language/DefaultRegistrations.php @@ -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());