Passed
Push — master ( 6137ca...01151b )
by Kevin
02:21
created

CompositeNormalizer::classMethodMap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 5
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Zenstruck\Porpaginas\Doctrine\DBAL\Specification\Normalizer;
4
5
use Doctrine\DBAL\Query\Expression\CompositeExpression;
6
use Zenstruck\Porpaginas\Doctrine\DBAL\Specification\DBALContext;
7
use Zenstruck\Porpaginas\Specification\Logic\AndX;
8
use Zenstruck\Porpaginas\Specification\Logic\Composite;
9
use Zenstruck\Porpaginas\Specification\Logic\OrX;
10
use Zenstruck\Porpaginas\Specification\Normalizer;
11
use Zenstruck\Porpaginas\Specification\Normalizer\ClassMethodMap;
12
use Zenstruck\Porpaginas\Specification\Normalizer\NormalizerAware;
13
use Zenstruck\Porpaginas\Specification\Normalizer\WithNormalizer;
14
15
/**
16
 * @author Kevin Bond <[email protected]>
17
 */
18
final class CompositeNormalizer implements Normalizer, NormalizerAware
19
{
20
    use DBALNormalizer, WithNormalizer, ClassMethodMap;
21
22
    /**
23
     * @param Composite   $specification
24
     * @param DBALContext $context
25
     */
26 3
    public function normalize($specification, $context): ?CompositeExpression
27
    {
28 3
        $children = \array_filter(\array_map(
29
            function($child) use ($context) {
30 3
                return $this->normalizer()->normalize($child, $context);
31 3
            },
32 3
            $specification->children()
33
        ));
34
35 3
        if (empty($children)) {
36
            return null;
37
        }
38
39 3
        return $context->qb()->expr()->{self::methodFor($specification)}(...$children);
40
    }
41
42 6
    protected static function classMethodMap(): array
43
    {
44
        return [
45 6
            AndX::class => 'andX',
46
            OrX::class => 'orX',
47
        ];
48
    }
49
}
50