MatchesRule   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A errorMessage() 0 3 1
A run() 0 6 2
A isParametersValid() 0 9 3
A canSkip() 0 3 1
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
}