Completed
Push — master ( b794f5...f9eb18 )
by Andrii
03:04
created

HstoreExpression   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 41
ccs 0
cts 17
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A buildExpression() 0 7 1
A buildUsing() 0 12 2
1
<?php
2
3
namespace hiapi\db;
4
5
use yii\db\Expression;
6
use yii\db\Query;
7
use yii\db\QueryBuilder;
8
use yii\db\QueryInterface;
9
10
/**
11
 * CallExpression represents a SQL function call expression.
12
 *
13
 * @author Andrii Vasyliev <[email protected]>
14
 */
15
class HstoreExpression implements ExpressionInterface
16
{
17
    const PARAM_PREFIX = ':hxp';
18
19
    /**
20
     * @var array hash
21
     */
22
    protected $hash;
23
24
    /**
25
     * CallExpression constructor.
26
     */
27
    public function __construct($hash)
28
    {
29
        $this->hash = $hash;
30
    }
31
32
    public function buildExpression(QueryBuilder $builder)
33
    {
34
        $params = [];
35
        $string = $this->buildUsing($builder, $params);
36
37
        return new Expression($string, $params);
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43
    public function buildUsing(QueryBuilder $queryBuilder, &$params = [])
44
    {
45
        $array = [];
46
        foreach ($this->hash as $key => $value) {
47
            $array[] = $key;
48
            $array[] = $value;
49
        }
50
        $arrayExp = new ArrayExpression($array, 'text');
51
        $callExp = new CallExpression('hstore', [$arrayExp]);
52
53
        return $callExp->buildUsing($queryBuilder, $params);
54
    }
55
}
56