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

ManyValidator::getInvalidDetails()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
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