Completed
Pull Request — master (#438)
by Anton
06:33
created

AbstractRule::validate()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 1
dl 0
loc 1
ccs 0
cts 0
cp 0
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