PostgresGrammar   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 86.67%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 7
c 4
b 0
f 1
lcom 1
cbo 1
dl 0
loc 65
ccs 13
cts 15
cp 0.8667
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B wrapValue() 0 21 5
A whereNull() 0 4 1
A whereNotNull() 0 4 1
1
<?php
2
namespace Bosnadev\Database\Query\Grammars;
3
4
use Illuminate\Database\Query\Builder;
5
use Illuminate\Database\Query\Grammars\PostgresGrammar as LaravelPostgresGrammar;
6
7
/**
8
 * Class PostgresGrammar
9
 *
10
 * @package Bosnadev\Database\Query\Grammars
11
 */
12
class PostgresGrammar extends LaravelPostgresGrammar
13
{
14
    /**
15
     * @var array
16
     */
17
    protected $jsonOperators = [
18
        '->',
19
        '->>',
20
        '#>',
21
        '#>>',
22
    ];
23
24
    /**
25
     * @param string $value
26
     *
27
     * @return string
28
     */
29 12
    protected function wrapValue($value)
30
    {
31 12
        if ($value === '*') {
32
            return $value;
33
        }
34
35
        // If querying hstore
36 12
        if (preg_match('/\[(.*?)\]/', $value, $match)) {
37 3
            return (string)str_replace(array('[', ']'), '', $match[1]);
38
        }
39
40
        // If querying json column
41 9
        foreach ($this->jsonOperators as $operator) {
42 9
            if (stripos($value, $operator)) {
43 9
                list($value, $key) = explode($operator, $value, 2);
44 9
                return parent::wrapValue($value) . $operator . $key;
45
            }
46 3
        }
47
48
        return parent::wrapValue($value);
49
    }
50
51
    /**
52
     * Compile a "where null" clause.
53
     *
54
     * @param  Builder $query
55
     * @param  array   $where
56
     *
57
     * @return string
58
     */
59 3
    protected function whereNull(Builder $query, $where)
60
    {
61 3
        return '(' . $this->wrap($where['column']) . ') is null';
62
    }
63
64
    /**
65
     * Compile a "where not null" clause.
66
     *
67
     * @param  Builder $query
68
     * @param  array   $where
69
     *
70
     * @return string
71
     */
72 3
    protected function whereNotNull(Builder $query, $where)
73
    {
74 3
        return '(' . $this->wrap($where['column']) . ') is not null';
75
    }
76
}
77