Passed
Push — 1.x ( 65701d...8d8d80 )
by Milwad
01:15 queued 15s
created

ValidCountry::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Milwad\LaravelValidate\Rules;
4
5
use Illuminate\Contracts\Validation\Rule;
6
7
class ValidCountry implements Rule
8
{
9
    public function __construct(
10
        protected bool $validByKey = false,
11
    ) {}
12
13
    /**
14
     * Check country name is valid.
15
     */
16
    public function passes($attribute, $value): bool
17
    {
18
        $countries = config('laravel-validate.countries', []);
19
20
        // Check value exists in country keys
21
        if ($this->validByKey) {
22
            return array_key_exists($value, $countries);
23
        }
24
25
        return in_array($value, $countries);
26
    }
27
28
    /**
29
     * Get the validation error message.
30
     */
31
    public function message(): string
32
    {
33
        return __('validate.country');
34
    }
35
}
36