Skip to content

Commit

Permalink
crmMoney filter
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaphoneJon committed May 30, 2023
1 parent f01eacf commit 5387c12
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
15 changes: 15 additions & 0 deletions Civi/Token/StandardFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,21 @@ public static function crmDate($value, array $filter, string $format) {
return static::default($value, $filter, $format);
}

public static function crmMoney($value, array $filter, string $format) {
if ($value instanceof \Brick\Money\Money) {
$amount = $value->getAmount()->toFloat();
}
elseif (is_numeric($value)) {
$amount = $value;
}
else {
return $value;
}
$currency = $filter[2] ?? '';
$locale = $filter[1] ?? NULL;
return \Civi::format()->money($amount, $currency, $locale);
}

/**
* Return value, falling back to default.
*
Expand Down
2 changes: 1 addition & 1 deletion Civi/Token/TokenProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ private function filterTokenValue($value, ?array $filter, TokenRow $row, string
}

// TODO: Move this to StandardFilters
if ($value instanceof Money) {
if ($value instanceof Money && $filter === NULL) {
switch ($filter[0] ?? NULL) {
case NULL:
case 'crmMoney':
Expand Down
28 changes: 27 additions & 1 deletion tests/phpunit/Civi/Token/TokenProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,16 @@ public function getFilterExamples() {
'empty_string' => '',
'and_such' => '<strong>testing &amp; such</strong>',
],
'my_numbers' => [
'amount' => 123,
'currency' => 'EUR',
'locale' => 'fr_FR',
],
'my_currencies' => [
'amount' => \Brick\Money\Money::of(123, 'USD', new \Brick\Money\Context\DefaultContext()),
'currency' => 'EUR',
'locale' => 'fr_FR',
],
];

$testCases = [];
Expand Down Expand Up @@ -523,6 +533,20 @@ public function getFilterExamples() {
],
$exampleTokens,
];
$testCases['crmMoney testing'] = [
'text/plain',
[
'Amount: {my_numbers.amount}' => 'Amount: 123',
'Amount as money: {my_numbers.amount|crmMoney}' => 'Amount as money: $123.00',
'Amount as money in France: {my_numbers.amount|crmMoney:"fr_FR"}' => 'Amount as money in France: 123,00 $US',
'Amount as money in France (Euros): {my_numbers.amount|crmMoney:"fr_FR":"EUR"}' => 'Amount as money in France (Euros): 123,00 €',
'Amount: {my_currencies.amount}' => 'Amount: $123.00',
'Amount as money: {my_currencies.amount|crmMoney}' => 'Amount as money: $123.00',
'Amount as money in France: {my_currencies.amount|crmMoney:"fr_FR"}' => 'Amount as money in France: 123,00 $US',
'Amount as money in France (Euros): {my_currencies.amount|crmMoney:"fr_FR":"EUR"}' => 'Amount as money in France (Euros): 123,00 €',
],
$exampleTokens,
];
return $testCases;
}

Expand All @@ -546,7 +570,9 @@ public function testFilters(string $messageFormat, array $exampleMessages, array
$p->addMessage('example', $inputMessage, $messageFormat);
$p->addRow()
->format('text/plain')->tokens(\CRM_Utils_Array::subset($exampleTokens, ['my_text']))
->format('text/html')->tokens(\CRM_Utils_Array::subset($exampleTokens, ['my_rich_text']));
->format('text/html')->tokens(\CRM_Utils_Array::subset($exampleTokens, ['my_rich_text']))
->format('text/plain')->tokens(\CRM_Utils_Array::subset($exampleTokens, ['my_numbers']))
->format('text/plain')->tokens(\CRM_Utils_Array::subset($exampleTokens, ['my_currencies']));
foreach ($p->evaluate()->getRows() as $row) {
$this->assertEquals($expectOutput, $row->render('example'));
$actualExampleCount++;
Expand Down

0 comments on commit 5387c12

Please sign in to comment.