1 | <?php |
||
2 | /** |
||
3 | * PiggyBankTransformer.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\Models\Note; |
||
28 | use FireflyIII\Models\PiggyBank; |
||
29 | use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; |
||
30 | use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface; |
||
31 | use League\Fractal\Resource\Collection as FractalCollection; |
||
32 | use League\Fractal\Resource\Item; |
||
33 | use League\Fractal\TransformerAbstract; |
||
34 | use Symfony\Component\HttpFoundation\ParameterBag; |
||
35 | |||
36 | /** |
||
37 | * Class PiggyBankTransformer |
||
38 | */ |
||
39 | class PiggyBankTransformer extends TransformerAbstract |
||
40 | { |
||
41 | /** |
||
42 | * List of resources possible to include |
||
43 | * |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $availableIncludes = ['account', 'user', 'piggy_bank_events']; |
||
47 | /** |
||
48 | * List of resources to automatically include |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | protected $defaultIncludes = []; |
||
53 | |||
54 | /** @var ParameterBag */ |
||
55 | protected $parameters; |
||
56 | |||
57 | /** |
||
58 | * PiggyBankTransformer constructor. |
||
59 | * |
||
60 | * @codeCoverageIgnore |
||
61 | * |
||
62 | * @param ParameterBag $parameters |
||
63 | */ |
||
64 | public function __construct(ParameterBag $parameters) |
||
65 | { |
||
66 | $this->parameters = $parameters; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Include account. |
||
71 | * |
||
72 | * @codeCoverageIgnore |
||
73 | * |
||
74 | * @param PiggyBank $piggyBank |
||
75 | * |
||
76 | * @return Item |
||
77 | */ |
||
78 | public function includeAccount(PiggyBank $piggyBank): Item |
||
79 | { |
||
80 | return $this->item($piggyBank->account, new AccountTransformer($this->parameters), 'accounts'); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Include events. |
||
85 | * |
||
86 | * @codeCoverageIgnore |
||
87 | * |
||
88 | * @param PiggyBank $piggyBank |
||
89 | * |
||
90 | * @return FractalCollection |
||
91 | */ |
||
92 | public function includePiggyBankEvents(PiggyBank $piggyBank): FractalCollection |
||
93 | { |
||
94 | return $this->collection($piggyBank->piggyBankEvents, new PiggyBankEventTransformer($this->parameters), 'piggy_bank_events'); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Include the user. |
||
99 | * |
||
100 | * @param PiggyBank $piggyBank |
||
101 | * |
||
102 | * @codeCoverageIgnore |
||
103 | * @return Item |
||
104 | */ |
||
105 | public function includeUser(PiggyBank $piggyBank): Item |
||
106 | { |
||
107 | return $this->item($piggyBank->account->user, new UserTransformer($this->parameters), 'users'); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Transform the piggy bank. |
||
112 | * |
||
113 | * @param PiggyBank $piggyBank |
||
114 | * |
||
115 | * @return array |
||
116 | */ |
||
117 | public function transform(PiggyBank $piggyBank): array |
||
118 | { |
||
119 | $account = $piggyBank->account; |
||
120 | $currencyId = intval($account->getMeta('currency_id')); |
||
121 | $decimalPlaces = 2; |
||
122 | if ($currencyId > 0) { |
||
123 | /** @var CurrencyRepositoryInterface $repository */ |
||
124 | $repository = app(CurrencyRepositoryInterface::class); |
||
125 | $repository->setUser($account->user); |
||
126 | $currency = $repository->findNull($currencyId); |
||
127 | $decimalPlaces = $currency->decimal_places; |
||
128 | } |
||
129 | |||
130 | // get currently saved amount: |
||
131 | /** @var PiggyBankRepositoryInterface $piggyRepos */ |
||
132 | $piggyRepos = app(PiggyBankRepositoryInterface::class); |
||
133 | $piggyRepos->setUser($account->user); |
||
134 | $currentAmount = round($piggyRepos->getCurrentAmount($piggyBank), $decimalPlaces); |
||
135 | |||
136 | $startDate = is_null($piggyBank->startdate) ? null : $piggyBank->startdate->format('Y-m-d'); |
||
137 | $targetDate = is_null($piggyBank->targetdate) ? null : $piggyBank->targetdate->format('Y-m-d'); |
||
138 | $targetAmount = round($piggyBank->targetamount, $decimalPlaces); |
||
139 | $data = [ |
||
140 | 'id' => (int)$piggyBank->id, |
||
141 | 'updated_at' => $piggyBank->updated_at->toAtomString(), |
||
142 | 'created_at' => $piggyBank->created_at->toAtomString(), |
||
143 | 'name' => $piggyBank->name, |
||
144 | 'target_amount' => $targetAmount, |
||
145 | 'current_amount' => $currentAmount, |
||
146 | 'startdate' => $startDate, |
||
147 | 'targetdate' => $targetDate, |
||
148 | 'order' => (int)$piggyBank->order, |
||
149 | 'active' => intval($piggyBank->active) === 1, |
||
0 ignored issues
–
show
|
|||
150 | 'notes' => null, |
||
151 | 'links' => [ |
||
152 | [ |
||
153 | 'rel' => 'self', |
||
154 | 'uri' => '/piggy_banks/' . $piggyBank->id, |
||
155 | ], |
||
156 | ], |
||
157 | ]; |
||
158 | /** @var Note $note */ |
||
159 | $note = $piggyBank->notes()->first(); |
||
160 | if (!is_null($note)) { |
||
161 | $data['notes'] = $note->text; |
||
162 | } |
||
163 | |||
164 | return $data; |
||
165 | } |
||
166 | } |
||
167 |
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.