Passed
Push — main ( eeb558...207cac )
by Thierry
05:30
created

TargetValidator::amountFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Siak\Tontine\Validation\Meeting;
4
5
use Illuminate\Support\Facades\Validator;
6
use Siak\Tontine\Service\LocaleService;
7
use Siak\Tontine\Validation\AbstractValidator;
8
use Siak\Tontine\Validation\Traits\ValidationTrait;
9
use Siak\Tontine\Validation\ValidationException;
10
11
class TargetValidator extends AbstractValidator
12
{
13
    use ValidationTrait;
14
15
    /**
16
     * @param LocaleService $localeService
17
     */
18
    public function __construct(private LocaleService $localeService)
19
    {}
20
21
    /**
22
     * @return array<string>
23
     */
24
    protected function amountFields(): array
25
    {
26
        return ['amount'];
27
    }
28
29
    /**
30
     * Validate settlement target data
31
     *
32
     * @param array $values
33
     *
34
     * @return array
35
     */
36
    public function validateItem(array $values): array
37
    {
38
        $validator = Validator::make($this->values($values), [
39
            'global' => 'required|boolean',
40
            'amount' => $this->amountRule(),
41
            'deadline' => 'required|integer|min:1',
42
        ]);
43
        if($validator->fails())
44
        {
45
            throw new ValidationException($validator);
46
        }
47
48
        $validated = $validator->validated();
49
        $validated['amount'] = $this->localeService->convertMoneyToInt((float)$validated['amount']);
50
        return $validated;
51
    }
52
}
53