AbstractRule   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 49
ccs 12
cts 13
cp 0.9231
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getDescription() 0 3 1
A setDescription() 0 4 1
A __toString() 0 3 1
A assert() 0 4 2
A __invoke() 0 3 1
1
<?php
2
3
/**
4
 * Bluz Framework Component
5
 *
6
 * @copyright Bluz PHP Team
7
 * @link      https://github.com/bluzphp/framework
8
 */
9
10
declare(strict_types=1);
11
12
namespace Bluz\Validator\Rule;
13
14
use Bluz\Validator\Exception\ValidatorException;
15
16
/**
17
 * Abstract validation rule
18
 *
19
 * @package  Bluz\Validator\Rule
20
 * @author   Anton Shevchuk
21
 */
22
abstract class AbstractRule implements RuleInterface
23
{
24
    /**
25
     * Message for error output
26
     * @var string
27
     */
28
    protected $description = 'is invalid';
29
30
    /**
31
     * @inheritdoc
32
     */
33 7
    public function assert($input): void
34
    {
35 7
        if (!$this->validate($input)) {
36 7
            throw new ValidatorException($this->description);
37
        }
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43 14
    public function __invoke($input): bool
44
    {
45 14
        return $this->validate($input);
46
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51 435
    public function __toString(): string
52
    {
53 435
        return $this->getDescription();
54
    }
55
56
    /**
57
     * @inheritdoc
58
     */
59 197
    public function getDescription(): string
60
    {
61 197
        return __($this->description);
62
    }
63
64
    /**
65
     * @inheritdoc
66
     */
67 4
    public function setDescription(string $description): RuleInterface
68
    {
69 4
        $this->description = $description;
70 4
        return $this;
71
    }
72
}
73