UniqueItems   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 93.75%

Importance

Changes 6
Bugs 3 Features 3
Metric Value
wmc 7
c 6
b 3
f 3
lcom 0
cbo 1
dl 0
loc 36
ccs 15
cts 16
cp 0.9375
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 12 3
A checkIfAllUniques() 0 15 4
1
<?php
2
/**
3
 * This file is part of php-simple-request.
4
 *
5
 * php-simple-request is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * php-simple-request is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with php-simple-request.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
namespace Mcustiel\SimpleRequest\Validator;
19
20
/**
21
 * Checks if all the elements of a given array are unique values.
22
 *
23
 * @author mcustiel
24
 */
25
class UniqueItems extends AbstractEmptySpecificationValidator
26
{
27
    /**
28
     * {@inheritdoc}
29
     *
30
     * @see \Mcustiel\SimpleRequest\Interfaces\ValidatorInterface::validate()
31
     */
32 82
    public function validate($value)
33
    {
34 82
        if (!is_array($value)) {
35
            return false;
36
        }
37
38 82
        if (empty($value)) {
39 1
            return true;
40
        }
41
42 82
        return $this->checkIfAllUniques($value);
43
    }
44
45 82
    private function checkIfAllUniques($value)
46
    {
47 82
        $keys = array_keys($value);
48 82
        $count = count($keys);
49
50 82
        for ($i = 0; $i < $count - 1; $i++) {
51 82
            for ($j = $i + 1; $j < $count; $j++) {
52 82
                if ($value[$keys[$i]] == $value[$keys[$j]]) {
53 1
                    return false;
54
                }
55 82
            }
56 82
        }
57
58 81
        return true;
59
    }
60
}
61