Pattern   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 34
ccs 0
cts 7
cp 0
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A check() 0 20 5
1
<?php
2
3
/**
4
 * This file is part of Dimtrovich/Validation.
5
 *
6
 * (c) 2023 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Dimtrovich\Validation\Rules;
13
14
class Pattern extends AbstractRule
15
{
16
    /**
17
     * @var array
18
     */
19
    protected $fillableParams = ['length', 'separator'];
20
21
    /**
22
     * Check texts with specific pattern.
23
     *
24
     * @credit <a href="https://github.com/milwad-dev/laravel-validate">milwad/laravel-validate - Milwad\LaravelValidate\Rules\ValidPattern</a>
25
     *
26
     * @param mixed $value
27
     */
28
    public function check($value): bool
29
    {
30
        if (! is_string($value)) {
31
            return false;
32
        }
33
34
        $this->requireParameters(['length']);
35
36
        $length    = (int) $this->parameter('length');
37
        $separator = $this->parameter('separator');
38
39
        $texts = explode($separator ?: '-', $value);
40
41
        foreach ($texts as $text) {
42
            if (strlen($text) !== $length) {
43
                return false;
44
            }
45
        }
46
47
        return true;
48
    }
49
}
50