UniqueItems::validate()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 3
nop 3
dl 0
loc 14
ccs 7
cts 7
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace League\JsonGuard\Constraint\DraftFour;
4
5
use League\JsonGuard\Assert;
6
use League\JsonGuard\ConstraintInterface;
7
use League\JsonGuard\Validator;
8
use function League\JsonGuard\error;
9
10
final class UniqueItems implements ConstraintInterface
11
{
12
    const KEYWORD = 'uniqueItems';
13
14
    /**
15
     * {@inheritdoc}
16
     */
17 8
    public function validate($value, $parameter, Validator $validator)
18
    {
19 8
        Assert::type($parameter, 'boolean', self::KEYWORD, $validator->getSchemaPath());
20
21 6
        if (!is_array($value) || $parameter === false) {
22 2
            return null;
23
        }
24
25 4
        if (count($value) === count(array_unique(array_map('serialize', $value)))) {
26 4
            return null;
27
        }
28
29 2
        return error('The array must be unique.', $validator);
30
    }
31
}
32