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

AbstractRule   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 53.85%

Importance

Changes 0
Metric Value
dl 0
loc 56
ccs 7
cts 13
cp 0.5385
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
validate() 0 1 ?
A assert() 0 6 2
A __invoke() 0 4 1
A __toString() 0 4 1
A getDescription() 0 4 1
A setDescription() 0 5 1
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