QueryHelper   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 1
dl 0
loc 34
ccs 0
cts 17
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B configure() 0 25 6
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