Passed
Pull Request — main (#51)
by Thierry
13:39
created

TargetValidator::validateItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 15
rs 9.9666
c 1
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\ValidationException;
9
10
class TargetValidator extends AbstractValidator
11
{
12
    /**
13
     * @var LocaleService
14
     */
15
    protected LocaleService $localeService;
16
17
    /**
18
     * @param LocaleService $localeService
19
     */
20
    public function __construct(LocaleService $localeService)
21
    {
22
        $this->localeService = $localeService;
23
    }
24
25
    /**
26
     * Validate settlement target data
27
     *
28
     * @param array $values
29
     *
30
     * @return array
31
     */
32
    public function validateItem(array $values): array
33
    {
34
        $validator = Validator::make($this->values($values), [
35
            'global' => 'required|boolean',
36
            'amount' => 'required|regex:/^\d+(\.\d{1,2})?$/',
37
            'deadline' => 'required|integer|min:1',
38
        ]);
39
        if($validator->fails())
40
        {
41
            throw new ValidationException($validator);
42
        }
43
44
        $validated = $validator->validated();
45
        $validated['amount'] = $this->localeService->convertMoneyToInt((float)$validated['amount']);
46
        return $validated;
47
    }
48
}
49