QueryHelper::configure()   B
last analyzed

Complexity

Conditions 6
Paths 9

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 0
cts 17
cp 0
rs 8.439
c 0
b 0
f 0
cc 6
eloc 11
nc 9
nop 2
crap 42
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipbox/spark/blob/master/LICENSE
6
 * @link       https://github.com/flipbox/spark
7
 */
8
9
namespace flipbox\spark\helpers;
10
11
use yii\db\QueryInterface;
12
13
/**
14
 * @author Flipbox Factory <[email protected]>
15
 * @since 1.0.0
16
 */
17
class QueryHelper
18
{
19
20
    /**
21
     * @param QueryInterface $query
22
     * @param array $config
23
     * @return QueryInterface
24
     */
25
    public static function configure(QueryInterface $query, $config = []): QueryInterface
26
    {
27
28
        // Halt
29
        if (empty($config)) {
30
            return $query;
31
        }
32
33
        // Force array
34
        if (!is_array($config)) {
35
            $config = ArrayHelper::toArray($config, [], false);
36
        }
37
38
        // Populate query attributes
39
        foreach ($config as $name => $value) {
40
            if (property_exists($query, $name)) {
41
                $query->$name = $value;
42
            } elseif (method_exists($query, 'set' . $name)) {
43
                // set property
44
                $query->{'set' . $name}($value);
45
            }
46
        }
47
48
        return $query;
49
    }
50
}
51