1 | <?php |
||
2 | /** |
||
3 | * BudgetTransformer.php |
||
4 | * Copyright (c) 2018 [email protected] |
||
5 | * |
||
6 | * This file is part of Firefly III. |
||
7 | * |
||
8 | * Firefly III is free software: you can redistribute it and/or modify |
||
9 | * it under the terms of the GNU General Public License as published by |
||
10 | * the Free Software Foundation, either version 3 of the License, or |
||
11 | * (at your option) any later version. |
||
12 | * |
||
13 | * Firefly III is distributed in the hope that it will be useful, |
||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
16 | * GNU General Public License for more details. |
||
17 | * |
||
18 | * You should have received a copy of the GNU General Public License |
||
19 | * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. |
||
20 | */ |
||
21 | |||
22 | declare(strict_types=1); |
||
23 | |||
24 | namespace FireflyIII\Transformers; |
||
25 | |||
26 | |||
27 | use FireflyIII\Helpers\Collector\JournalCollectorInterface; |
||
28 | use FireflyIII\Models\Budget; |
||
29 | use Illuminate\Support\Collection; |
||
30 | use League\Fractal\Resource\Collection as FractalCollection; |
||
31 | use League\Fractal\Resource\Item; |
||
32 | use League\Fractal\TransformerAbstract; |
||
33 | use Symfony\Component\HttpFoundation\ParameterBag; |
||
34 | |||
35 | /** |
||
36 | * Class BudgetTransformer |
||
37 | */ |
||
38 | class BudgetTransformer extends TransformerAbstract |
||
39 | { |
||
40 | /** |
||
41 | * List of resources possible to include |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $availableIncludes = ['user', 'transactions']; |
||
46 | /** |
||
47 | * List of resources to automatically include |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $defaultIncludes = []; |
||
52 | |||
53 | /** @var ParameterBag */ |
||
54 | protected $parameters; |
||
55 | |||
56 | /** |
||
57 | * BudgetTransformer constructor. |
||
58 | * |
||
59 | * @codeCoverageIgnore |
||
60 | * |
||
61 | * @param ParameterBag $parameters |
||
62 | */ |
||
63 | public function __construct(ParameterBag $parameters) |
||
64 | { |
||
65 | $this->parameters = $parameters; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Include any transactions. |
||
70 | * |
||
71 | * @param Budget $budget |
||
72 | * |
||
73 | * @codeCoverageIgnore |
||
74 | * @return FractalCollection |
||
75 | */ |
||
76 | public function includeTransactions(Budget $budget): FractalCollection |
||
77 | { |
||
78 | $pageSize = intval(app('preferences')->getForUser($budget->user, 'listPageSize', 50)->data); |
||
79 | |||
80 | // journals always use collector and limited using URL parameters. |
||
81 | $collector = app(JournalCollectorInterface::class); |
||
82 | $collector->setUser($budget->user); |
||
83 | $collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation(); |
||
84 | $collector->setAllAssetAccounts(); |
||
85 | $collector->setBudgets(new Collection([$budget])); |
||
86 | if (!is_null($this->parameters->get('start')) && !is_null($this->parameters->get('end'))) { |
||
87 | $collector->setRange($this->parameters->get('start'), $this->parameters->get('end')); |
||
88 | } |
||
89 | $collector->setLimit($pageSize)->setPage($this->parameters->get('page')); |
||
90 | $journals = $collector->getJournals(); |
||
91 | |||
92 | return $this->collection($journals, new TransactionTransformer($this->parameters), 'transactions'); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Include the user. |
||
97 | * |
||
98 | * @param Budget $budget |
||
99 | * |
||
100 | * @codeCoverageIgnore |
||
101 | * @return Item |
||
102 | */ |
||
103 | public function includeUser(Budget $budget): Item |
||
104 | { |
||
105 | return $this->item($budget->user, new UserTransformer($this->parameters), 'users'); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Transform a budget. |
||
110 | * |
||
111 | * @param Budget $budget |
||
112 | * |
||
113 | * @return array |
||
114 | */ |
||
115 | public function transform(Budget $budget): array |
||
116 | { |
||
117 | $data = [ |
||
118 | 'id' => (int)$budget->id, |
||
119 | 'updated_at' => $budget->updated_at->toAtomString(), |
||
120 | 'created_at' => $budget->created_at->toAtomString(), |
||
121 | 'active' => intval($budget->active) === 1, |
||
0 ignored issues
–
show
|
|||
122 | 'name' => $budget->name, |
||
123 | 'links' => [ |
||
124 | [ |
||
125 | 'rel' => 'self', |
||
126 | 'uri' => '/budgets/' . $budget->id, |
||
127 | ], |
||
128 | ], |
||
129 | ]; |
||
130 | |||
131 | return $data; |
||
132 | } |
||
133 | |||
134 | } |
||
135 |
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.