Passed
Push — master ( 270076...60d36c )
by Adam
22:00
created

VatIdRule::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
rs 10
1
<?php
2
3
namespace Coyote\Rules;
4
5
use Ddeboer\Vatin\Validator;
6
use Illuminate\Contracts\Validation\Rule;
7
8
class VatIdRule implements Rule
9
{
10
    public function __construct(private string $countryCode)
11
    {
12
    }
13
14
    public function passes($attribute, $vatId): bool
15
    {
16
        // can't validate without country code
17
        if (!$this->countryCode) {
18
            return true;
19
        }
20
21
        if ($this->countryCode !== 'CH') {
22
            $vatId = preg_replace('/[^0-9A-Za-z]/', '', $vatId);
23
        }
24
25
        return (new Validator)->isValid($this->countryCode . $vatId);
26
    }
27
28
    public function message()
29
    {
30
        return trans('validation.invalid_vat_id');
31
    }
32
}
33