2018-06-28 00:32:58 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* CurrencyExchangeRateTransformer.php
|
2020-02-16 06:57:18 -06:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2018-06-28 00:32:58 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2018-06-28 00:32:58 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
2018-06-28 00:32:58 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2018-06-28 00:32:58 -05:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-01 23:37:26 -05:00
|
|
|
* GNU Affero General Public License for more details.
|
2018-06-28 00:32:58 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-06-28 00:32:58 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Transformers;
|
|
|
|
|
|
|
|
|
|
|
|
use FireflyIII\Models\CurrencyExchangeRate;
|
|
|
|
|
2018-08-06 12:14:30 -05:00
|
|
|
/**
|
|
|
|
* Class CurrencyExchangeRateTransformer
|
|
|
|
*/
|
2018-12-15 15:03:42 -06:00
|
|
|
class CurrencyExchangeRateTransformer extends AbstractTransformer
|
2018-06-28 00:32:58 -05:00
|
|
|
{
|
2018-08-06 12:14:30 -05:00
|
|
|
/**
|
|
|
|
* @param CurrencyExchangeRate $rate
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-06-28 00:32:58 -05:00
|
|
|
public function transform(CurrencyExchangeRate $rate): array
|
|
|
|
{
|
2020-07-12 10:32:48 -05:00
|
|
|
$result = number_format((float) $rate->rate * (float) $this->parameters->get('amount'), $rate->toCurrency->decimal_places, '.', '');
|
2018-12-08 23:51:53 -06:00
|
|
|
$result = 0.0 === $result ? null : $result;
|
2020-10-23 12:11:25 -05:00
|
|
|
return [
|
2018-12-19 12:02:16 -06:00
|
|
|
'id' => (int)$rate->id,
|
|
|
|
'created_at' => $rate->created_at->toAtomString(),
|
|
|
|
'updated_at' => $rate->updated_at->toAtomString(),
|
2020-07-12 10:54:51 -05:00
|
|
|
'from_currency_id' => (int) $rate->fromCurrency->id,
|
2018-12-19 12:02:16 -06:00
|
|
|
'from_currency_name' => $rate->fromCurrency->name,
|
|
|
|
'from_currency_code' => $rate->fromCurrency->code,
|
|
|
|
'from_currency_symbol' => $rate->fromCurrency->symbol,
|
2020-07-12 10:54:51 -05:00
|
|
|
'from_currency_decimal_places' => (int) $rate->fromCurrency->decimal_places,
|
|
|
|
'to_currency_id' => (int) $rate->toCurrency->id,
|
2018-12-19 12:02:16 -06:00
|
|
|
'to_currency_name' => $rate->toCurrency->name,
|
|
|
|
'to_currency_code' => $rate->toCurrency->code,
|
|
|
|
'to_currency_symbol' => $rate->toCurrency->symbol,
|
2020-07-12 10:54:51 -05:00
|
|
|
'to_currency_decimal_places' => (int) $rate->toCurrency->decimal_places,
|
2018-12-19 12:02:16 -06:00
|
|
|
'date' => $rate->date->format('Y-m-d'),
|
|
|
|
'rate' => (float)$rate->rate,
|
|
|
|
'amount' => $result,
|
|
|
|
'links' => [
|
2018-06-28 00:32:58 -05:00
|
|
|
[
|
|
|
|
'rel' => 'self',
|
|
|
|
'uri' => '/currency_exchange_rates/' . $rate->id,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
2018-07-22 13:32:02 -05:00
|
|
|
}
|