Test Failed
Pull Request — 1.x (#56)
by
unknown
11:07
created

ValidPattern::message()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Milwad\LaravelValidate\Rules;
4
5
use Closure;
6
use Illuminate\Contracts\Validation\ValidationRule;
0 ignored issues
show
Bug introduced by
The type Illuminate\Contracts\Validation\ValidationRule was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
8
class ValidPattern implements ValidationRule
9
{
10
    public function __construct(
11
        private int $length,
12
        private string $seperator = '-'
13
    ) {
14
    }
15
16
    /**
17
     * Check texts with specific pattern.
18
     *
19
     * @param string $attribute
20
     * @param mixed $value
21
     * @param Closure $fail
22
     * @return void
23
     */
24
    public function validate(string $attribute, mixed $value, Closure $fail): void
25
    {
26
        if (! $this->isPatternValid($value)) {
27
            $fail('validate.pattern')->translate();
28
        }
29
    }
30
31
    private function isPatternValid($value)
32
    {
33
        $texts = explode($this->seperator, $value);
34
35
        foreach ($texts as $text) {
36
            if (strlen($text) !== $this->length) {
37
                return false;
38
            }
39
        }
40
41
        return true;
42
    }
43
}
44