Completed
Push — master ( adf0b7...fb9b49 )
by Andreas
13s
created

CriteriaMerger   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 0
dl 0
loc 27
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

1 Method

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