ValidPattern::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Milwad\LaravelValidate\Rules;
4
5
use Illuminate\Contracts\Validation\Rule;
6
7
class ValidPattern implements Rule
8
{
9
    public function __construct(
10
        private int $length,
11
        private string $separator = '-'
12
    ) {}
13
14
    /**
15
     * Check text with specific pattern.
16
     */
17
    public function passes($attribute, $value): bool
18
    {
19
        $texts = explode($this->separator, $value);
20
21
        foreach ($texts as $text) {
22
            if (strlen($text) !== $this->length) {
23
                return false;
24
            }
25
        }
26
27
        return true;
28
    }
29
30
    /**
31
     * Get the validation error message.
32
     */
33
    public function message(): string
34
    {
35
        return __('validate.pattern');
36
    }
37
}
38