Passed
Push — master ( abbd29...fab7f5 )
by Dennis
07:07 queued 03:54
created

BankCardRule   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 37
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 4 2
A passes() 0 3 1
A message() 0 3 1
1
<?php
2
namespace SimpleCMS\Bank\Validation\Rule;
3
4
use Illuminate\Contracts\Validation\Rule;
5
use SimpleCMS\Bank\Validation\BankCard;
6
7
/**
8
 * 银行卡号
9
 *
10
 * @author Dennis Lui <[email protected]>
11
 */
12
class BankCardRule implements Rule
13
{
14
    /**
15
     * Run the validation rule.
16
     *
17
     * @param  string  $attribute
18
     * @param  mixed  $value
19
     * @param  \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail
20
     * @return void
21
     */
22
    public function validate(string $attribute, mixed $value, \Closure $fail): void
23
    {
24
        if (!$this->passes($attribute, $value)) {
25
            $fail($this->message());
26
        }
27
    }
28
29
    /**
30
     * Determine if the validation rule passes.
31
     *
32
     * @param  string  $attribute
33
     * @param  mixed  $value
34
     * @return bool
35
     */
36
    public function passes($attribute, $value)
37
    {
38
        return (new BankCard($value))->isValid();
39
    }
40
41
    /**
42
     * Get the validation error message.
43
     *
44
     * @return string|array
45
     */
46
    public function message()
47
    {
48
        return 'The :attribute is incorrect.';
49
    }
50
}