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

ValidationTrait::amountRule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Siak\Tontine\Validation\Traits;
4
5
use function str_replace;
6
7
trait ValidationTrait
8
{
9
    /**
10
     * @return array<string>
11
     */
12
    abstract protected function amountFields(): array;
13
14
    /**
15
     * Process input values before validation
16
     *
17
     * @param array $values
18
     *
19
     * @return array
20
     */
21
    protected function values(array $values): array
22
    {
23
        $values = parent::values($values);
24
        $fields = $this->amountFields();
25
        foreach($fields as $field)
26
        {
27
            if(isset($values[$field]))
28
            {
29
                $values[$field] = str_replace(',', '.', $values[$field]);
30
            }
31
        }
32
        return $values;
33
    }
34
35
    /**
36
     * @return string
37
     */
38
    protected function amountRule(): string
39
    {
40
        return 'required|regex:/^\d+([\.\,]\d{1,2})?$/';
41
    }
42
43
    /**
44
     * @param string $field
45
     *
46
     * @return string
47
     */
48
    protected function amountIfRule(string $field): string
49
    {
50
        return "required_if:{$field},1|regex:/^\d+([\.\,]\d{1,2})?$/";
51
    }
52
}
53