1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DoS\ResourceBundle\Twig\Extension; |
4
|
|
|
|
5
|
|
|
use Sylius\Bundle\MoneyBundle\Templating\Helper\MoneyHelper; |
6
|
|
|
use Sylius\Bundle\MoneyBundle\Templating\Helper\MoneyHelperInterface; |
7
|
|
|
use Sylius\Bundle\SettingsBundle\Templating\Helper\SettingsHelper; |
8
|
|
|
use Symfony\Component\Intl\Intl as SymfonyIntl; |
9
|
|
|
use Symfony\Component\Templating\Helper\HelperInterface; #fix small bug in console with Intl naming |
10
|
|
|
|
11
|
|
|
class Currency extends \Twig_Extension |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var MoneyHelper |
15
|
|
|
*/ |
16
|
|
|
protected $moneyHelper; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var SettingsHelper |
20
|
|
|
*/ |
21
|
|
|
protected $helper; |
22
|
|
|
|
23
|
|
|
protected $generalCurrencyKey; |
24
|
|
|
|
25
|
|
|
protected $currencySymbols = array( |
26
|
|
|
'THB' => '฿', |
27
|
|
|
); |
28
|
|
|
|
29
|
|
|
public function __construct( |
30
|
|
|
MoneyHelperInterface $moneyHelper, |
31
|
|
|
HelperInterface $helper, |
32
|
|
|
$generalCurrencyKey = 'general.currency', |
33
|
|
|
array $currencySymbols = array() |
34
|
|
|
) { |
35
|
|
|
$this->moneyHelper = $moneyHelper; |
|
|
|
|
36
|
|
|
$this->helper = $helper; |
|
|
|
|
37
|
|
|
$this->generalCurrencyKey = $generalCurrencyKey; |
38
|
|
|
$this->currencySymbols = array_merge($this->currencySymbols, $currencySymbols); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getFunctions() |
42
|
|
|
{ |
43
|
|
|
$self = array('is_safe' => array('all')); |
44
|
|
|
|
45
|
|
|
return array( |
46
|
|
|
new \Twig_SimpleFunction( |
|
|
|
|
47
|
|
|
'ui_currency_symbol', array($this, 'getCurrencySymbol'), $self |
48
|
|
|
), |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getFilters() |
53
|
|
|
{ |
54
|
|
|
return array( |
55
|
|
|
new \Twig_SimpleFilter('ui_money', array($this, 'formatAmount')), |
|
|
|
|
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getCurrencySymbol($currency = null) |
60
|
|
|
{ |
61
|
|
|
$currency = $currency ?: $this->helper->getSettingsParameter($this->generalCurrencyKey); |
62
|
|
|
|
63
|
|
|
if (array_key_exists($currency, $this->currencySymbols)) { |
64
|
|
|
return $this->currencySymbols[$currency]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return SymfonyIntl::getCurrencyBundle()->getCurrencySymbol($currency); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param $amount |
72
|
|
|
* @param null $currency |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public function formatAmount($amount, $currency = null) |
77
|
|
|
{ |
78
|
|
|
return str_replace('.00', '', $this->moneyHelper->formatAmount($amount, $currency)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns the canonical name of this helper. |
83
|
|
|
* |
84
|
|
|
* @return string The canonical name |
85
|
|
|
* |
86
|
|
|
* @api |
87
|
|
|
*/ |
88
|
|
|
public function getName() |
89
|
|
|
{ |
90
|
|
|
return 'ui_currency'; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..