Completed
Push — master ( a9c6d4...c66f2a )
by Philip
03:08
created

ManyValidator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 0
cbo 0
dl 0
loc 28
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isValid() 0 14 2
A getInvalidDetails() 0 3 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
     * {@inheritdoc}
23
     */
24
    public function isValid($value, array $parameters) {
25
26
        if (in_array($value, [null, ''])) {
27
            return true;
28
        }
29
30
        $data         = $parameters[0];
31
        $field        = $parameters[1];
32
        $manyEntity   = $data->getDefinition()->getManyEntity($field);
33
        $validIds     = array_keys($data->getIdToNameMap($manyEntity, null));
34
        $candidateIds = array_column($value, 'id');
35
36
        return array_values(array_intersect($validIds, $candidateIds)) == $candidateIds;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getInvalidDetails() {
43
        return 'many';
44
    }
45
46
}
47