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

HstoreExpression::buildUsing()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 0
cts 8
cp 0
rs 9.4285
cc 2
eloc 8
nc 2
nop 2
crap 6
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