Completed
Push — master ( 38deb0...7544ac )
by Anton
11s
created

SlugRule::validate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 1
dl 0
loc 16
ccs 8
cts 8
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link      https://github.com/bluzphp/framework
7
 */
8
9
declare(strict_types=1);
10
11
namespace Bluz\Validator\Rule;
12
13
/**
14
 * Check for slug by regular expressions
15
 *
16
 * @package Bluz\Validator\Rule
17
 */
18
class SlugRule extends AbstractRule
19
{
20
    /**
21
     * @var string error template
22
     */
23
    protected $description = 'must be a valid slug';
24
25
    /**
26
     * Check input data
27
     *
28
     * @param  string $input
29
     *
30
     * @return bool
31
     */
32 14
    public function validate($input): bool
33
    {
34 14
        if (false !== strpos($input, '--')) {
35 2
            return false;
36
        }
37
38 12
        if (!preg_match('/^[0-9a-z\-]+$/', $input)) {
39 6
            return false;
40
        }
41
42 6
        if (preg_match('/^-|-$/', $input)) {
43 2
            return false;
44
        }
45
46 4
        return true;
47
    }
48
}
49