firefly-iii/app/Transformers/BudgetLimitTransformer.php

93 lines
3.0 KiB
PHP
Raw Normal View History

2018-06-24 06:20:29 -05:00
<?php
/**
* BudgetLimitTransformer.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Transformers;
2018-06-29 22:21:21 -05:00
2018-06-24 06:20:29 -05:00
use FireflyIII\Models\BudgetLimit;
use League\Fractal\TransformerAbstract;
use Log;
2018-06-24 06:20:29 -05:00
use Symfony\Component\HttpFoundation\ParameterBag;
/**
* Class BudgetLimitTransformer
*/
2018-12-16 06:55:19 -06:00
class BudgetLimitTransformer extends AbstractTransformer
2018-06-24 06:20:29 -05:00
{
/**
* CurrencyTransformer constructor.
*
* @codeCoverageIgnore
*/
2018-12-16 06:55:19 -06:00
public function __construct()
2018-06-24 06:20:29 -05:00
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this)));
}
2018-06-24 06:20:29 -05:00
}
/**
* Transform the note.
*
* @param BudgetLimit $budgetLimit
*
* @return array
*/
public function transform(BudgetLimit $budgetLimit): array
{
2018-12-09 01:45:53 -06:00
$currency = $budgetLimit->transactionCurrency;
$amount = $budgetLimit->amount;
$currencyId = null;
$currencyName = null;
$currencyCode = null;
$currencySymbol = null;
if (null !== $currency) {
$amount = round($budgetLimit->amount, $budgetLimit->transactionCurrency->decimal_places);
$currencyId = $currency->id;
$currencyName = $currency->name;
$currencyCode = $currency->code;
$currencySymbol = $currency->symbol;
}
2018-06-24 06:20:29 -05:00
$data = [
2018-12-09 01:45:53 -06:00
'id' => (int)$budgetLimit->id,
'created_at' => $budgetLimit->created_at->toAtomString(),
2018-12-12 13:30:25 -06:00
'updated_at' => $budgetLimit->updated_at->toAtomString(),
2018-12-09 01:45:53 -06:00
'start_date' => $budgetLimit->start_date->format('Y-m-d'),
'end_date' => $budgetLimit->end_date->format('Y-m-d'),
'budget_id' => $budgetLimit->budget_id,
'currency_id' => $currencyId,
'currency_code' => $currencyCode,
'currency_name' => $currencyName,
'currency_symbol' => $currencySymbol,
'amount' => $amount,
'links' => [
2018-06-24 06:20:29 -05:00
[
'rel' => 'self',
2018-12-09 01:45:53 -06:00
'uri' => '/budgets/limits/' . $budgetLimit->id,
2018-06-24 06:20:29 -05:00
],
],
];
return $data;
}
}