Passed
Push — main ( 926316...d9c688 )
by Dimitri
02:57
created

Pattern::check()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 20
ccs 7
cts 7
cp 1
crap 5
rs 9.6111
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 2
            return false;
32
        }
33
34 4
        $this->requireParameters(['length']);
35
36 4
        $length    = (int) $this->parameter('length');
37 4
        $separator = $this->parameter('separator');
38
39 4
        $texts = explode($separator ?: '-', $value);
40
41
        foreach ($texts as $text) {
42
            if (strlen($text) !== $length) {
43 4
                return false;
44
            }
45
        }
46
47 4
        return true;
48
    }
49
}
50