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

ManyValidator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 0
loc 37
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isValid() 0 21 4
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 many.
18
 */
19
class ManyValidator implements ValidatorInterface
20
{
21
22
    /**
23
     * {@inheritdoc}
24
     */
25 2
    public function isValid($value, array $parameters)
26
    {
27
28 2
        if (in_array($value, [null, ''])) {
29 2
            return true;
30
        }
31
32 1
        $data         = $parameters[0];
33 1
        $field        = $parameters[1];
34 1
        $manyEntity   = $data->getDefinition()->getSubTypeField($field, 'many', 'entity');
35 1
        $validIds     = array_keys($data->getIdToNameMap($manyEntity, null));
36 1
        $candidateIds = array_column($value, 'id');
37
38 1
        foreach ($candidateIds as $candidateId) {
39 1
            if (!in_array($candidateId, $validIds)) {
40 1
                return false;
41
            }
42
        }
43
44 1
        return true;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 1
    public function getInvalidDetails()
51
    {
52 1
        return 'many';
53
    }
54
55
}
56