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

ArrayAdapter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 76.47%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 58
ccs 13
cts 17
cp 0.7647
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isStrictUniqueControl() 0 4 1
A setStrictUniqueControl() 0 4 1
B validate() 0 18 7
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