From b5f6b4ade9681ad618d2b50eca3b72fdc2ec6e6b Mon Sep 17 00:00:00 2001 From: Semenov Date: Tue, 26 Jul 2022 15:20:10 +0300 Subject: [PATCH] dev --- src/Currency.php | 17 ++++++++++++++++- src/Money.php | 6 ++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Currency.php b/src/Currency.php index 494aa79..97d1ca6 100644 --- a/src/Currency.php +++ b/src/Currency.php @@ -21,6 +21,7 @@ * @property integer subUnit * * @property float baseCourse + * @property string symbol * * new Currency('RUB'); * @@ -1262,7 +1263,7 @@ public function getSubUnit() //return (integer)$this->model->sub_unit; } - return (integer) ArrayHelper::getValue($this->_baseData, 'sub_unit'); + return (integer)ArrayHelper::getValue($this->_baseData, 'sub_unit'); } /** @@ -1307,4 +1308,18 @@ public function getCurrencyCode() { return $this->code; } + + /** + * @param $locale + * @return false|string + */ + function getSymbol($locale = '') + { + if (!$locale) { + $locale = \Yii::$app->formatter->locale; + } + + $formatter = new \NumberFormatter($locale.'@currency='.$this->code, \NumberFormatter::CURRENCY); + return $formatter->getSymbol(\NumberFormatter::CURRENCY_SYMBOL); + } } \ No newline at end of file diff --git a/src/Money.php b/src/Money.php index a917457..1dc98bc 100644 --- a/src/Money.php +++ b/src/Money.php @@ -211,7 +211,13 @@ public function jsonSerialize() { return [ 'amount' => (float) $this->amount, + 'amountFormat' => \Yii::$app->formatter->asDecimal($this->amount), + + 'amountRound' => (float) round($this->amount, 2), + 'amountRoundFormat' => \Yii::$app->formatter->asDecimal((float) round($this->amount, 2)), + 'currency' => $this->currency->code, + 'convertSymbol' => $this->currency->symbol, 'convertAndFormat' => \Yii::$app->money->convertAndFormat($this), ]; }