Completed
Push — master ( afb93c...2180a3 )
by Freek
24:20 queued 09:20
created

CountryCode::nullable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Spatie\ValidationRules\Rules;
4
5
use League\ISO3166\ISO3166;
6
use Illuminate\Contracts\Validation\Rule;
7
8
class CountryCode implements Rule
9
{
10
    /** @var bool */
11
    protected $required;
12
13
    /** @var string */
14
    protected $attribute;
15
16 24
    public function __construct(bool $required = true)
17
    {
18 24
        $this->required = $required;
19 24
    }
20
21 6
    public function nullable(): self
22
    {
23 6
        $this->required = false;
24
25 6
        return $this;
26
    }
27
28 24
    public function passes($attribute, $value): bool
29
    {
30 24
        $this->attribute = $attribute;
31
32 24
        if (! $this->required && ($value === '0' || $value === 0 || $value === null)) {
33 6
            return true;
34
        }
35
36 24
        $countries = array_pluck((new ISO3166())->all(), ISO3166::KEY_ALPHA2);
0 ignored issues
show
Deprecated Code introduced by
The function array_pluck() has been deprecated with message: Arr::pluck() should be used directly instead. Will be removed in Laravel 5.9.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
37
38 24
        return in_array($value, $countries, true);
39
    }
40
41 12
    public function message(): string
42
    {
43 12
        return __('validationRules::messages.country_code', [
44 12
            'attribute' => $this->attribute,
45
        ]);
46
    }
47
}
48