Completed
Push — master ( ccf0eb...14b48b )
by Anton
10s
created

AbstractRule::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
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
use Bluz\Validator\Exception\ValidatorException;
14
15
/**
16
 * Abstract validation rule
17
 *
18
 * @package  Bluz\Validator\Rule
19
 * @author   Anton Shevchuk
20
 */
21
abstract class AbstractRule implements RuleInterface
22
{
23
    /**
24
     * Message for error output
25
     * @var string
26
     */
27
    protected $description = 'is invalid';
28
29
    /**
30
     * @inheritdoc
31
     */
32
    abstract public function validate($input) : bool;
33
34
    /**
35
     * @inheritdoc
36
     */
37
    public function assert($input)
38
    {
39
        if (!$this->validate($input)) {
40
            throw new ValidatorException($this->description);
41
        }
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47
    public function __invoke($input) : bool
48
    {
49
        return $this->validate($input);
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55 440
    public function __toString() : string
56
    {
57 440
        return $this->getDescription();
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63 198
    public function getDescription() : string
64
    {
65 198
        return __($this->description);
66
    }
67
68
    /**
69
     * @inheritdoc
70
     */
71 4
    public function setDescription(string $description) : RuleInterface
72
    {
73 4
        $this->description = $description;
74 4
        return $this;
75
    }
76
}
77