Passed
Push — 1.x ( ced68d...fcb885 )
by Milwad
01:14 queued 14s
created

ValidLandlineNumber::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
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 1
rs 10
1
<?php
2
3
namespace Milwad\LaravelValidate\Rules;
4
5
use Illuminate\Contracts\Validation\Rule;
6
use Milwad\LaravelValidate\Utils\CountryLandlineCallback;
7
8
class ValidLandlineNumber implements Rule
9
{
10
    public function __construct(protected ?string $code = null) {}
11
12
    /**
13
     * Check phone number is valid.
14
     */
15
    public function passes($attribute, $value): bool
16
    {
17
        if (is_string($this->code)) {
18
            $passes = CountryLandlineCallback::callLandlineValidator($this->code, $value);
19
20
            return collect($passes)->some(fn ($passe) => $passe);
21
        }
22
23
        return preg_match('/^(?:\+|00)(?:[1-9]\d{0,2})(?:[ .\-]?\(?\d+\)?)+$/', $value);
0 ignored issues
show
Bug Best Practice introduced by
The expression return preg_match('/^(?:...\(?\d+\)?)+$/', $value) returns the type integer which is incompatible with the type-hinted return boolean.
Loading history...
24
    }
25
26
    /**
27
     * Get the validation error message.
28
     */
29
    public function message(): string
30
    {
31
        return __('validate.landline-number');
32
    }
33
}
34