Completed
Branch master (6d30bf)
by Philip
13:30
created

ReferenceValidator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 32
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isValid() 0 16 3
A getInvalidDetails() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the CRUDlex package.
5
 *
6
 * (c) Philip Lehmann-Böhm <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CRUDlex;
13
14
use \Valdi\Validator\ValidatorInterface;
15
16
/**
17
 * A validator to check references.
18
 */
19
class ReferenceValidator implements ValidatorInterface
20
{
21
22
    /**
23
     * {@inheritdoc}
24
     */
25 6
    public function isValid($value, array $parameters)
26
    {
27
28 6
        if (key_exists('id', $value) && in_array($value['id'], [null, ''])) {
29 5
            return true;
30
        }
31
32 6
        $data            = $parameters[0];
33 6
        $field           = $parameters[1];
34 6
        $definition      = $data->getDefinition();
35 6
        $paramsOperators = ['id' => '='];
36 6
        $referenceEntity = $definition->getSubTypeField($field, 'reference', 'entity');
37 6
        $table           = $definition->getService()->getData($referenceEntity)->getDefinition()->getTable();
38 6
        $amount          = $data->countBy($table, $value, $paramsOperators, false);
39 6
        return $amount > 0;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 2
    public function getInvalidDetails()
46
    {
47 2
        return 'reference';
48
    }
49
50
}
51