Completed
Pull Request — master (#1984)
by Maciej
21:32
created

CriteriaMerger   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 71.43%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 0
dl 0
loc 25
ccs 5
cts 7
cp 0.7143
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A merge() 0 17 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ODM\MongoDB\Query;
6
7
use function array_filter;
8
use function array_values;
9
use function count;
10
11
/**
12
 * Utility class for merging query criteria.
13
 *
14
 * This is mainly used to incorporate filter and ReferenceMany mapping criteria
15
 * into a query. Each criteria array will be joined with "$and" to avoid cases
16
 * where criteria might be inadvertently overridden with array_merge().
17
 */
18
final class CriteriaMerger
19
{
20
    /**
21
     * Combines any number of criteria arrays as clauses of an "$and" query.
22
     *
23
     * @param array ...$criterias Any number of query criteria arrays
24
     */
25 1148
    public function merge(...$criterias) : array
26
    {
27 1148
        $nonEmptyCriterias = array_values(array_filter($criterias, static function (array $criteria) {
28 1148
            return ! empty($criteria);
29
        }));
30
31
        switch (count($nonEmptyCriterias)) {
32
            case 0:
33
                return [];
34
35
            case 1:
36
                return $nonEmptyCriterias[0];
37
38
            default:
39 74
                return ['$and' => $nonEmptyCriterias];
40
        }
41
    }
42
}
43