Passed
Push — master ( 6f8b1f...142a48 )
by James
25:51 queued 11:45
created

ExpandedForm   A

Complexity

Total Complexity 39

Size/Duplication

Total Lines 399
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 39
eloc 175
c 0
b 0
f 0
dl 0
loc 399
rs 9.28

15 Methods

Rating   Name   Duplication   Size   Complexity  
A file() 0 14 2
A password() 0 14 2
A integer() 0 16 2
A location() 0 15 2
A number() 0 16 2
A makeSelectListWithEmpty() 0 19 5
A amountNoCurrency() 0 22 4
A staticText() 0 13 2
A text() 0 14 2
A optionsList() 0 10 2
A date() 0 15 2
A checkbox() 0 25 3
A percentage() 0 16 2
A textarea() 0 20 3
A nonSelectableAmount() 0 22 4
1
<?php
2
/**
3
 * ExpandedForm.php
4
 * Copyright (c) 2017 [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
declare(strict_types=1);
22
23
namespace FireflyIII\Support;
24
25
use Amount as Amt;
26
use Carbon\Carbon;
27
use Eloquent;
28
use FireflyIII\Exceptions\FireflyException;
29
use FireflyIII\Models\Account;
30
use FireflyIII\Models\AccountType;
31
use FireflyIII\Models\PiggyBank;
32
use FireflyIII\Models\RuleGroup;
33
use FireflyIII\Models\TransactionCurrency;
34
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
35
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
36
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
37
use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface;
38
use FireflyIII\Support\Form\FormSupport;
39
use Form;
40
use Illuminate\Support\Collection;
41
use Illuminate\Support\HtmlString;
42
use Illuminate\Support\MessageBag;
43
use Log;
44
use RuntimeException;
45
use Throwable;
46
47
/**
48
 * Class ExpandedForm.
49
 *
50
 * @SuppressWarnings(PHPMD.TooManyMethods)
51
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
52
 * @codeCoverageIgnore
53
 */
54
class ExpandedForm
55
{
56
    use FormSupport;
57
    /**
58
     * @param string $name
59
     * @param mixed $value
60
     * @param array $options
61
     *
62
     * @return string
63
     *
64
     */
65
    public function amountNoCurrency(string $name, $value = null, array $options = null): string
66
    {
67
        $options         = $options ?? [];
68
        $label           = $this->label($name, $options);
69
        $options         = $this->expandOptionArray($name, $label, $options);
70
        $classes         = $this->getHolderClasses($name);
71
        $value           = $this->fillFieldValue($name, $value);
72
        $options['step'] = 'any';
73
        unset($options['currency'], $options['placeholder']);
74
75
        // make sure value is formatted nicely:
76
        if (null !== $value && '' !== $value) {
77
            $value = round($value, 8);
78
        }
79
        try {
80
            $html = view('form.amount-no-currency', compact('classes', 'name', 'label', 'value', 'options'))->render();
81
        } catch (Throwable $e) {
82
            Log::debug(sprintf('Could not render amountNoCurrency(): %s', $e->getMessage()));
83
            $html = 'Could not render amountNoCurrency.';
84
        }
85
86
        return $html;
87
    }
88
89
    /**
90
     * @param string $name
91
     * @param int $value
92
     * @param mixed $checked
93
     * @param array $options
94
     *
95
     * @return string
96
     *
97
     */
98
    public function checkbox(string $name, int $value = null, $checked = null, array $options = null): string
99
    {
100
        $options            = $options ?? [];
101
        $value              = $value ?? 1;
102
        $options['checked'] = true === $checked;
103
104
        if (app('session')->has('preFilled')) {
105
            $preFilled          = session('preFilled');
106
            $options['checked'] = $preFilled[$name] ?? $options['checked'];
107
        }
108
109
        $label   = $this->label($name, $options);
110
        $options = $this->expandOptionArray($name, $label, $options);
111
        $classes = $this->getHolderClasses($name);
112
        $value   = $this->fillFieldValue($name, $value);
113
114
        unset($options['placeholder'], $options['autocomplete'], $options['class']);
115
        try {
116
            $html = view('form.checkbox', compact('classes', 'name', 'label', 'value', 'options'))->render();
117
        } catch (Throwable $e) {
118
            Log::debug(sprintf('Could not render checkbox(): %s', $e->getMessage()));
119
            $html = 'Could not render checkbox.';
120
        }
121
122
        return $html;
123
    }
124
125
    /**
126
     * @param string $name
127
     * @param mixed $value
128
     * @param array $options
129
     *
130
     * @return string
131
     *
132
     */
133
    public function date(string $name, $value = null, array $options = null): string
134
    {
135
        $label   = $this->label($name, $options);
136
        $options = $this->expandOptionArray($name, $label, $options);
137
        $classes = $this->getHolderClasses($name);
138
        $value   = $this->fillFieldValue($name, $value);
139
        unset($options['placeholder']);
140
        try {
141
            $html = view('form.date', compact('classes', 'name', 'label', 'value', 'options'))->render();
142
        } catch (Throwable $e) {
143
            Log::debug(sprintf('Could not render date(): %s', $e->getMessage()));
144
            $html = 'Could not render date.';
145
        }
146
147
        return $html;
148
    }
149
150
    /**
151
     * @param string $name
152
     * @param array $options
153
     *
154
     * @return string
155
     *
156
     */
157
    public function file(string $name, array $options = null): string
158
    {
159
        $options = $options ?? [];
160
        $label   = $this->label($name, $options);
161
        $options = $this->expandOptionArray($name, $label, $options);
162
        $classes = $this->getHolderClasses($name);
163
        try {
164
            $html = view('form.file', compact('classes', 'name', 'label', 'options'))->render();
165
        } catch (Throwable $e) {
166
            Log::debug(sprintf('Could not render file(): %s', $e->getMessage()));
167
            $html = 'Could not render file.';
168
        }
169
170
        return $html;
171
    }
172
173
    /**
174
     * @param string $name
175
     * @param mixed $value
176
     * @param array $options
177
     *
178
     * @return string
179
     *
180
     */
181
    public function integer(string $name, $value = null, array $options = null): string
182
    {
183
        $options         = $options ?? [];
184
        $label           = $this->label($name, $options);
185
        $options         = $this->expandOptionArray($name, $label, $options);
186
        $classes         = $this->getHolderClasses($name);
187
        $value           = $this->fillFieldValue($name, $value);
188
        $options['step'] = '1';
189
        try {
190
            $html = view('form.integer', compact('classes', 'name', 'label', 'value', 'options'))->render();
191
        } catch (Throwable $e) {
192
            Log::debug(sprintf('Could not render integer(): %s', $e->getMessage()));
193
            $html = 'Could not render integer.';
194
        }
195
196
        return $html;
197
    }
198
199
    /**
200
     * @param string $name
201
     * @param mixed $value
202
     * @param array $options
203
     *
204
     * @return string
205
     *
206
     */
207
    public function location(string $name, $value = null, array $options = null): string
208
    {
209
        $options = $options ?? [];
210
        $label   = $this->label($name, $options);
211
        $options = $this->expandOptionArray($name, $label, $options);
212
        $classes = $this->getHolderClasses($name);
213
        $value   = $this->fillFieldValue($name, $value);
214
        try {
215
            $html = view('form.location', compact('classes', 'name', 'label', 'value', 'options'))->render();
216
        } catch (Throwable $e) {
217
            Log::debug(sprintf('Could not render location(): %s', $e->getMessage()));
218
            $html = 'Could not render location.';
219
        }
220
221
        return $html;
222
    }
223
224
    /**
225
     * @param \Illuminate\Support\Collection $set
226
     *
227
     * @return array
228
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
229
     */
230
    public function makeSelectListWithEmpty(Collection $set): array
231
    {
232
        $selectList    = [];
233
        $selectList[0] = '(none)';
234
        $fields        = ['title', 'name', 'description'];
235
        /** @var Eloquent $entry */
236
        foreach ($set as $entry) {
237
            $entryId = (int)$entry->id;
238
            $title   = null;
239
240
            foreach ($fields as $field) {
241
                if (isset($entry->$field) && null === $title) {
242
                    $title = $entry->$field;
243
                }
244
            }
245
            $selectList[$entryId] = $title;
246
        }
247
248
        return $selectList;
249
    }
250
251
    /**
252
     * @param string $name
253
     * @param mixed $value
254
     * @param array $options
255
     *
256
     * @return string
257
     */
258
    public function nonSelectableAmount(string $name, $value = null, array $options = null): string
259
    {
260
        $label            = $this->label($name, $options);
261
        $options          = $this->expandOptionArray($name, $label, $options);
262
        $classes          = $this->getHolderClasses($name);
263
        $value            = $this->fillFieldValue($name, $value);
264
        $options['step']  = 'any';
265
        $selectedCurrency = $options['currency'] ?? Amt::getDefaultCurrency();
0 ignored issues
show
Bug Best Practice introduced by James Cole
The method FireflyIII\Support\Facad...t::getDefaultCurrency() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

265
        $selectedCurrency = $options['currency'] ?? Amt::/** @scrutinizer ignore-call */ getDefaultCurrency();
Loading history...
266
        unset($options['currency'], $options['placeholder']);
267
268
        // make sure value is formatted nicely:
269
        if (null !== $value && '' !== $value) {
270
            $value = round($value, $selectedCurrency->decimal_places);
271
        }
272
        try {
273
            $html = view('form.non-selectable-amount', compact('selectedCurrency', 'classes', 'name', 'label', 'value', 'options'))->render();
274
        } catch (Throwable $e) {
275
            Log::debug(sprintf('Could not render nonSelectableAmount(): %s', $e->getMessage()));
276
            $html = 'Could not render nonSelectableAmount.';
277
        }
278
279
        return $html;
280
    }
281
282
    /**
283
     * @param string $name
284
     * @param mixed $value
285
     * @param array $options
286
     *
287
     * @return string
288
     *
289
     */
290
    public function number(string $name, $value = null, array $options = null): string
291
    {
292
        $label           = $this->label($name, $options);
293
        $options         = $this->expandOptionArray($name, $label, $options);
294
        $classes         = $this->getHolderClasses($name);
295
        $value           = $this->fillFieldValue($name, $value);
296
        $options['step'] = 'any';
297
        unset($options['placeholder']);
298
        try {
299
            $html = view('form.number', compact('classes', 'name', 'label', 'value', 'options'))->render();
300
        } catch (Throwable $e) {
301
            Log::debug(sprintf('Could not render number(): %s', $e->getMessage()));
302
            $html = 'Could not render number.';
303
        }
304
305
        return $html;
306
    }
307
308
    /**
309
     * @param string $type
310
     * @param string $name
311
     *
312
     * @return string
313
     *
314
     */
315
    public function optionsList(string $type, string $name): string
316
    {
317
        try {
318
            $html = view('form.options', compact('type', 'name'))->render();
319
        } catch (Throwable $e) {
320
            Log::debug(sprintf('Could not render select(): %s', $e->getMessage()));
321
            $html = 'Could not render optionsList.';
322
        }
323
324
        return $html;
325
    }
326
327
    /**
328
     * @param string $name
329
     * @param array $options
330
     *
331
     * @return string
332
     *
333
     */
334
    public function password(string $name, array $options = null): string
335
    {
336
337
        $label   = $this->label($name, $options);
338
        $options = $this->expandOptionArray($name, $label, $options);
339
        $classes = $this->getHolderClasses($name);
340
        try {
341
            $html = view('form.password', compact('classes', 'name', 'label', 'options'))->render();
342
        } catch (Throwable $e) {
343
            Log::debug(sprintf('Could not render password(): %s', $e->getMessage()));
344
            $html = 'Could not render password.';
345
        }
346
347
        return $html;
348
    }
349
350
    /**
351
     * Function to render a percentage.
352
     *
353
     * @param string $name
354
     * @param mixed $value
355
     * @param array $options
356
     *
357
     * @return string
358
     *
359
     */
360
    public function percentage(string $name, $value = null, array $options = null): string
361
    {
362
        $label           = $this->label($name, $options);
363
        $options         = $this->expandOptionArray($name, $label, $options);
364
        $classes         = $this->getHolderClasses($name);
365
        $value           = $this->fillFieldValue($name, $value);
366
        $options['step'] = 'any';
367
        unset($options['placeholder']);
368
        try {
369
            $html = view('form.percentage', compact('classes', 'name', 'label', 'value', 'options'))->render();
370
        } catch (Throwable $e) {
371
            Log::debug(sprintf('Could not render percentage(): %s', $e->getMessage()));
372
            $html = 'Could not render percentage.';
373
        }
374
375
        return $html;
376
    }
377
378
    /**
379
     * @param string $name
380
     * @param mixed $value
381
     * @param array $options
382
     *
383
     * @return string
384
     *
385
     */
386
    public function staticText(string $name, $value, array $options = null): string
387
    {
388
        $label   = $this->label($name, $options);
389
        $options = $this->expandOptionArray($name, $label, $options);
390
        $classes = $this->getHolderClasses($name);
391
        try {
392
            $html = view('form.static', compact('classes', 'name', 'label', 'value', 'options'))->render();
393
        } catch (Throwable $e) {
394
            Log::debug(sprintf('Could not render staticText(): %s', $e->getMessage()));
395
            $html = 'Could not render staticText.';
396
        }
397
398
        return $html;
399
    }
400
401
    /**
402
     * @param string $name
403
     * @param mixed $value
404
     * @param array $options
405
     *
406
     * @return string
407
     *
408
     */
409
    public function text(string $name, $value = null, array $options = null): string
410
    {
411
        $label   = $this->label($name, $options);
412
        $options = $this->expandOptionArray($name, $label, $options);
413
        $classes = $this->getHolderClasses($name);
414
        $value   = $this->fillFieldValue($name, $value);
415
        try {
416
            $html = view('form.text', compact('classes', 'name', 'label', 'value', 'options'))->render();
417
        } catch (Throwable $e) {
418
            Log::debug(sprintf('Could not render text(): %s', $e->getMessage()));
419
            $html = 'Could not render text.';
420
        }
421
422
        return $html;
423
    }
424
425
    /**
426
     * @param string $name
427
     * @param mixed $value
428
     * @param array $options
429
     *
430
     * @return string
431
     *
432
     */
433
    public function textarea(string $name, $value = null, array $options = null): string
434
    {
435
        $label           = $this->label($name, $options);
436
        $options         = $this->expandOptionArray($name, $label, $options);
437
        $classes         = $this->getHolderClasses($name);
438
        $value           = $this->fillFieldValue($name, $value);
439
        $options['rows'] = 4;
440
441
        if (null === $value) {
442
            $value = '';
443
        }
444
445
        try {
446
            $html = view('form.textarea', compact('classes', 'name', 'label', 'value', 'options'))->render();
447
        } catch (Throwable $e) {
448
            Log::debug(sprintf('Could not render textarea(): %s', $e->getMessage()));
449
            $html = 'Could not render textarea.';
450
        }
451
452
        return $html;
453
    }
454
}
455