2018-02-11 20:45:33 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* BudgetTransformer.php
|
2020-02-16 13:57:18 +01:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2018-02-11 20:45:33 +01:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2018-02-11 20:45:33 +01:00
|
|
|
*
|
2019-10-02 06:37:26 +02: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-02-11 20:45:33 +01:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2018-02-11 20:45:33 +01:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-02 06:37:26 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2018-02-11 20:45:33 +01:00
|
|
|
*
|
2019-10-02 06:37:26 +02: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-02-11 20:45:33 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Transformers;
|
|
|
|
|
|
|
|
|
|
|
|
use FireflyIII\Models\Budget;
|
2018-12-12 20:30:25 +01:00
|
|
|
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
2019-09-23 16:42:21 +02:00
|
|
|
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
|
2018-02-17 10:47:06 +01:00
|
|
|
use Illuminate\Support\Collection;
|
2018-12-15 07:59:02 +01:00
|
|
|
use Log;
|
2018-02-11 20:45:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class BudgetTransformer
|
|
|
|
*/
|
2018-12-16 13:55:19 +01:00
|
|
|
class BudgetTransformer extends AbstractTransformer
|
2018-02-11 20:45:33 +01:00
|
|
|
{
|
2019-09-23 16:42:21 +02:00
|
|
|
/** @var OperationsRepositoryInterface */
|
|
|
|
private $opsRepository;
|
2018-12-19 19:02:16 +01:00
|
|
|
|
2018-02-11 20:45:33 +01:00
|
|
|
/**
|
2018-02-17 10:47:06 +01:00
|
|
|
* BudgetTransformer constructor.
|
|
|
|
*
|
|
|
|
* @codeCoverageIgnore
|
2018-02-11 20:45:33 +01:00
|
|
|
*/
|
2018-12-16 13:55:19 +01:00
|
|
|
public function __construct()
|
2018-02-11 20:45:33 +01:00
|
|
|
{
|
2019-09-23 16:42:21 +02:00
|
|
|
$this->opsRepository = app(OperationsRepositoryInterface::class);
|
2018-12-15 07:59:02 +01:00
|
|
|
if ('testing' === config('app.env')) {
|
2019-06-07 18:20:15 +02:00
|
|
|
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this)));
|
2018-12-15 07:59:02 +01:00
|
|
|
}
|
2018-02-11 20:45:33 +01:00
|
|
|
}
|
|
|
|
|
2018-02-17 10:47:06 +01:00
|
|
|
/**
|
|
|
|
* Transform a budget.
|
|
|
|
*
|
2018-02-11 20:45:33 +01:00
|
|
|
* @param Budget $budget
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function transform(Budget $budget): array
|
|
|
|
{
|
2019-09-23 16:42:21 +02:00
|
|
|
$this->opsRepository->setUser($budget->user);
|
2018-12-12 20:30:25 +01:00
|
|
|
$start = $this->parameters->get('start');
|
|
|
|
$end = $this->parameters->get('end');
|
|
|
|
$spent = [];
|
|
|
|
if (null !== $start && null !== $end) {
|
2019-09-23 16:42:21 +02:00
|
|
|
$spent = array_values($this->opsRepository->sumExpenses($start, $end, null, new Collection([$budget])));
|
2018-12-12 20:30:25 +01:00
|
|
|
}
|
|
|
|
|
2018-02-11 20:45:33 +01:00
|
|
|
$data = [
|
|
|
|
'id' => (int)$budget->id,
|
|
|
|
'created_at' => $budget->created_at->toAtomString(),
|
2018-12-12 20:30:25 +01:00
|
|
|
'updated_at' => $budget->updated_at->toAtomString(),
|
2018-12-19 19:02:16 +01:00
|
|
|
'active' => $budget->active,
|
2018-02-11 20:45:33 +01:00
|
|
|
'name' => $budget->name,
|
2018-12-12 20:30:25 +01:00
|
|
|
'spent' => $spent,
|
2018-02-11 20:45:33 +01:00
|
|
|
'links' => [
|
|
|
|
[
|
|
|
|
'rel' => 'self',
|
|
|
|
'uri' => '/budgets/' . $budget->id,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2018-03-05 19:35:58 +01:00
|
|
|
}
|