Completed
Push — master ( a6c946...0fff30 )
by Dmytro
02:08
created

ArrayAdapter::validate()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 7.0671

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 8
cts 9
cp 0.8889
rs 8.8333
c 0
b 0
f 0
cc 7
nc 5
nop 1
crap 7.0671
1
<?php
2
declare(strict_types=1);
3
4
5
namespace Paymaxi\Component\Query\Validator\Adapter;
6
7
use Paymaxi\Component\Query\Validator\ValidatorInterface;
8
9
/**
10
 * Class ArrayAdapter
11
 *
12
 * @package Paymaxi\Component\Query\Validator\Adapter
13
 */
14
final class ArrayAdapter implements ValidatorInterface
15
{
16
    /** @var ValidatorInterface */
17
    private $validator;
18
19
    /** @var bool */
20
    private $strictUniqueControl = true;
21
22
    /**
23
     * ArrayAdapter constructor.
24
     *
25
     * @param ValidatorInterface $validator
26
     */
27 26
    public function __construct(ValidatorInterface $validator)
28
    {
29 26
        $this->validator = $validator;
30 26
    }
31
32
    /**
33
     * @param mixed $value
34
     *
35
     * @return bool
36
     */
37 18
    public function validate($value): bool
38
    {
39 18
        if (!\is_array($value) || 0 === \count($value)) {
40
            return false;
41
        }
42
43 18
        if ($this->isStrictUniqueControl() && \count($value) > \count(\array_unique($value))) {
44 2
            return false;
45
        }
46
47 16
        foreach ($value as $item) {
48 16
            if (!$this->validator->validate($item)) {
49 16
                return false;
50
            }
51
        }
52
53 16
        return true;
54
    }
55
56
    /**
57
     * @return bool
58
     */
59 18
    public function isStrictUniqueControl(): bool
60
    {
61 18
        return $this->strictUniqueControl;
62
    }
63
64
    /**
65
     * @param bool $strictUniqueControl
66
     */
67
    public function setStrictUniqueControl(bool $strictUniqueControl): void
68
    {
69
        $this->strictUniqueControl = $strictUniqueControl;
70
    }
71
}
72