Passed
Push — master ( 71375f...32edb5 )
by Alexander
07:08
created

ConditionBuilder::resolveConditionMethodName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php declare(strict_types=1);
2
3
namespace Indigerd\Repository\TableGateway\ConditionBuilder;
4
5
use yii\helpers\Inflector;
6
7
class ConditionBuilder
8
{
9
    public function build(array $params): array
10
    {
11
        $conditions = [];
12
        foreach ($params as $name => $value) {
13
            $method = $this->resolveConditionMethodName($name);
14
            if (\method_exists($this, $method)) {
15
                $newCondition = $this->$method($value);
16
            } else {
17
                $newCondition = $this->buildHashCondition($name, $value);
18
            }
19
20
            if (empty($newCondition)) {
21
                continue;
22
            }
23
24
            if (empty($conditions)) {
25
                $conditions = $newCondition;
26
            } else {
27
                $conditions = ['and', $conditions, $newCondition];
28
            }
29
        }
30
31
        return $conditions;
32
    }
33
34
    protected function resolveConditionMethodName($fieldName): string
35
    {
36
        return 'build' . Inflector::camelize($fieldName) . 'Condition';
37
    }
38
39
    protected function buildHashCondition(string $name, $value): array
40
    {
41
        return [$name => $value];
42
    }
43
}
44