Passed
Push — master ( ea62c3...cd2ef0 )
by Bruno
09:11
created

SameAs   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 28
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 14 4
A getMetadata() 0 10 1
1
<?php declare(strict_types=1);
2
3
namespace Formularium\Validator;
4
5
use Formularium\Exception\ValidatorException;
6
use Formularium\Field;
7
use Formularium\Model;
8
use Formularium\ValidatorInterface;
9
use Formularium\ValidatorMetadata;
10
11
/**
12
 * May not be present, but if it is must not be empty.
13
 */
14
class SameAs implements ValidatorInterface
15
{
16
    public function validate($value, array $options = [], Model $model = null)
17
    {
18
        $same = $options['target'];
19
        if (!$model) {
20
            throw new ValidatorException('Same as requires a model.');
21
        }
22
        $modelData = $model->getData();
23
        if (!array_key_exists($same, $modelData)) {
24
            throw new ValidatorException('Same as field not found.');
25
        }
26
        if ($modelData[$same] !== $value) {
27
            throw new ValidatorException('Field does not match ' . $same);
28
        }
29
        return $value;
30
    }
31
32
    public function getMetadata(): ValidatorMetadata
33
    {
34
        return new ValidatorMetadata(
35
            __CLASS__,
36
            "Must be the same as a target field.",
37
            [
38
                new ValidatorArgs(
0 ignored issues
show
Bug introduced by
The type Formularium\Validator\ValidatorArgs was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
39
                    'target',
40
                    'String',
41
                    'Target field'
42
                )
43
            ]
44
        );
45
    }
46
}
47