UniqueValidator   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 3
dl 0
loc 83
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isValidUniqueMany() 0 4 1
A isValidUnique() 0 11 3
A isValid() 0 18 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 for an unique field.
18
 */
19
class UniqueValidator implements ValidatorInterface
20
{
21
22
    /**
23
     * Checks whether the unique constraint is valid for a many-to-many field.
24
     *
25
     * @param array $value
26
     * the value to check
27
     * @param AbstractData $data
28
     * the data to perform the check with
29
     * @param Entity $entity
30
     * the entity to perform the check on
31
     * @param $field
32
     * the many field to perform the check on
33
     *
34
     * @return boolean
35
     * true if it is a valid unique many-to-many constraint
36
     */
37 1
    protected function isValidUniqueMany(array $value, AbstractData $data, Entity $entity, $field)
38
    {
39 1
        return !$data->hasManySet($field, array_column($value, 'id'), $entity->get('id'));
40
    }
41
42
    /**
43
     * Performs the regular unique validation.
44
     *
45
     * @param $value
46
     * the value to validate
47
     * @param $data
48
     * the data instance to validate with
49
     * @param $entity
50
     * the entity of the field
51
     * @param $field
52
     * the field to validate
53
     * @param $type
54
     * the type of the field to validate
55
     *
56
     * @return boolean
57
     * true if everything is valid
58
     */
59 2
    protected function isValidUnique($value, AbstractData $data, Entity $entity, $field, $type)
60
    {
61 2
        $params          = [$field => $type === 'reference' ? $value['id'] : $value];
62 2
        $paramsOperators = [$field => '='];
63 2
        if ($entity->get('id') !== null) {
64 2
            $params['id']          = $entity->get('id');
65 2
            $paramsOperators['id'] = '!=';
66
        }
67 2
        $amount = intval($data->countBy($data->getDefinition()->getTable(), $params, $paramsOperators, true));
68 2
        return $amount == 0;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 2
    public function isValid($value, array $parameters)
75
    {
76
77 2
        if (in_array($value, [null, ''])) {
78 1
            return true;
79
        }
80
81 2
        $data   = $parameters[0];
82 2
        $entity = $parameters[1];
83 2
        $field  = $parameters[2];
84 2
        $type   = $data->getDefinition()->getType($field);
85
86 2
        if ($type === 'many') {
87 1
            return $this->isValidUniqueMany($value, $data, $entity, $field);
88
        }
89
90 2
        return $this->isValidUnique($value, $data, $entity, $field, $type);
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 2
    public function getInvalidDetails()
97
    {
98 2
        return 'unique';
99
    }
100
101
}
102