ValidPattern   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 29
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A message() 0 3 1
A passes() 0 11 3
A __construct() 0 4 1
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