Passed
Push — 1.x ( 3587d8...eb5eae )
by Milwad
01:08 queued 13s
created

ValidPhoneNumber::__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 Milwad\LaravelValidate\Rules;
4
5
use Exception;
6
use Illuminate\Contracts\Validation\Rule;
7
use Milwad\LaravelValidate\Utils\CountryPhoneCallback;
8
9
class ValidPhoneNumber implements Rule
10
{
11
    public function __construct(protected ?string $code = null)
12
    {
13
    }
14
    /**
15
     * Check phone number is valid.
16
     *
17
     * @param  string  $attribute
18
     * @param  mixed  $value
19
     * @return bool
20
     */
21
    public function passes($attribute, $value)
22
    {
23
        if (is_string($this->code)) {
24
            $passes =  (new CountryPhoneCallback($value, $this->code))->callPhoneValidator();
25
            return collect($passes)->some(fn ($passe) => $passe);
0 ignored issues
show
Bug introduced by
$passes of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

25
            return collect(/** @scrutinizer ignore-type */ $passes)->some(fn ($passe) => $passe);
Loading history...
26
        }
27
        return preg_match('/^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/', $value);
0 ignored issues
show
Bug Best Practice introduced by
The expression return preg_match('/^[\+...?[0-9]{4,6}$/', $value) returns the type integer which is incompatible with the documented return type boolean.
Loading history...
28
    }
29
30
    /**
31
     * Get the validation error message.
32
     *
33
     * @return string
34
     */
35
    public function message()
36
    {
37
        return __('validate.phone-number');
38
    }
39
}
40