Skip to content

Commit

Permalink
[Currency] Add event for currency formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
mysliwietzflorian committed Oct 9, 2023
1 parent ff6ff28 commit e0fdea8
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Model/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

use Pimcore\Bundle\EcommerceFrameworkBundle\Type\Decimal;
use Pimcore\Localization\IntlFormatter;
use Psr\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

class Currency
{
Expand All @@ -32,6 +34,8 @@ class Currency

const USE_NAME = 'longname';

protected EventDispatcherInterface $eventDispatcher;

protected string $currencyShortName;

protected string $currencySymbol;
Expand Down Expand Up @@ -65,6 +69,7 @@ class Currency
public function __construct(string $currencyShortName)
{
$this->currencyShortName = $currencyShortName;
$this->eventDispatcher = \Pimcore::getContainer()->get('event_dispatcher');
}

protected function getFormatter(): IntlFormatter
Expand All @@ -89,6 +94,14 @@ public function toCurrency(null|float|int|string|Decimal $value, array|string $p
$value = $value->asString();
}

$event = new GenericEvent($value, [
'value' => $value,
'pattern' => $pattern,
]);
$this->eventDispatcher->dispatch($event, 'ecommerce.on-currency-formatted');

Check failure on line 101 in src/Model/Currency.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.1, lowest, false)

Method Psr\EventDispatcher\EventDispatcherInterface::dispatch() invoked with 2 parameters, 1 required.

Check failure on line 101 in src/Model/Currency.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2, highest, 11.x-dev as 11.0.0, true)

Method Psr\EventDispatcher\EventDispatcherInterface::dispatch() invoked with 2 parameters, 1 required.
$value = $event->getArgument('value');
$pattern = $event->getArgument('pattern');

return $this->getFormatter()->formatCurrency($value, $this->currencyShortName, $pattern);
}

Expand Down

0 comments on commit e0fdea8

Please sign in to comment.