CriteriaMatcher::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\SkeletonMapper\DataSource;
6
7
use function in_array;
8
9
class CriteriaMatcher
10
{
11
    /** @var mixed[] */
12
    private $criteria;
13
14
    /** @var mixed[] */
15
    private $row;
16
17
    /**
18
     * @param mixed[] $criteria
19
     * @param mixed[] $row
20
     */
21 9
    public function __construct(array $criteria, array $row)
22
    {
23 9
        $this->criteria = $criteria;
24 9
        $this->row      = $row;
25 9
    }
26
27 9
    public function matches() : bool
28
    {
29 9
        $matches = true;
30
31 9
        foreach ($this->criteria as $key => $value) {
32 5
            if ($this->criteriaElementMatches($key, $value)) {
33 3
                continue;
34
            }
35
36 3
            $matches = false;
37
        }
38
39 9
        return $matches;
40
    }
41
42
    /**
43
     * @param mixed $value
44
     */
45 5
    private function criteriaElementMatches(string $key, $value) : bool
46
    {
47 5
        if (isset($value['$contains'])) {
48 2
            if ($this->contains($key, $value)) {
49 2
                return true;
50
            }
51 3
        } elseif ($this->equals($key, $value)) {
52 2
            return true;
53
        }
54
55 3
        return false;
56
    }
57
58
    /**
59
     * @param mixed[] $value
60
     */
61 2
    private function contains(string $key, array $value) : bool
62
    {
63 2
        return isset($this->row[$key]) && in_array($value['$contains'], $this->row[$key], true);
64
    }
65
66
    /**
67
     * @param mixed $value
68
     */
69 3
    private function equals(string $key, $value) : bool
70
    {
71 3
        return isset($this->row[$key]) && $this->row[$key] === $value;
72
    }
73
}
74