Passed
Push — master ( 893d1e...31005e )
by Kirill
03:35
created

CheckerRule::getMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Validation;
13
14
use Spiral\Translator\Translator;
15
16
final class CheckerRule extends AbstractRule
17
{
18
    /** @var CheckerInterface */
19
    private $checker;
20
21
    /** @var string */
22
    private $method;
23
24
    /** @var array */
25
    private $args;
26
27
    /** @var string|null */
28
    private $message;
29
30
    /**
31
     * @param CheckerInterface $checker
32
     * @param string           $method
33
     * @param array            $args
34
     * @param null|string      $message
35
     */
36
    public function __construct(
37
        CheckerInterface $checker,
38
        string $method,
39
        array $args = [],
40
        ?string $message = null
41
    ) {
42
        $this->checker = $checker;
43
        $this->method = $method;
44
        $this->args = $args;
45
        $this->message = $message;
46
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51
    public function ignoreEmpty($value): bool
52
    {
53
        return $this->checker->ignoreEmpty($this->method, $value, $this->args);
54
    }
55
56
    /**
57
     * @inheritdoc
58
     */
59
    public function validate(ValidatorInterface $v, string $field, $value): bool
60
    {
61
        return $this->checker->check($v, $this->method, $field, $value, $this->args);
62
    }
63
64
    /**
65
     * @inheritdoc
66
     */
67
    public function getMessage(string $field, $value): string
68
    {
69
        if (!empty($this->message)) {
70
            return Translator::interpolate(
71
                $this->message,
72
                array_merge([$value, $field], $this->args)
73
            );
74
        }
75
76
        return $this->checker->getMessage($this->method, $field, $value, $this->args);
77
    }
78
}
79