MatchesRule::run()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 3
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
4
namespace SolBianca\Validator\Rules;
5
6
7
use SolBianca\Validator\Exceptions\RuleException;
8
use SolBianca\Validator\Interfaces\RuleInterface;
9
10
class MatchesRule implements RuleInterface
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15
    public function run($value, array $input, array $args): bool
16
    {
17
        if (!$this->isParametersValid($input, $args)) {
18
            return false;
19
        }
20
        return $value === $input[$args[0]]['value'];
21
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function errorMessage(): string
27
    {
28
        return 'Field `{field}` must match `{$0}`.';
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function canSkip(): bool
35
    {
36
        return true;
37
    }
38
39
    /**
40
     * @param array $input
41
     * @param array $args
42
     * @return bool
43
     * @throws RuleException
44
     */
45
    private function isParametersValid(array $input, array $args): bool
46
    {
47
        if (empty($args[0])) {
48
            throw new RuleException("You must provide valid input field name.");
49
        }
50
        if (!isset($input[$args[0]])) {
51
            throw new RuleException("Input data don't contain field `{$args[0]}`.");
52
        }
53
        return true;
54
    }
55
}