Passed
Push — master ( 4e5334...4eb7e1 )
by Jonas
04:13
created

PostgresGrammar   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
eloc 8
c 2
b 0
f 0
dl 0
loc 53
ccs 11
cts 11
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getRecursivePathBindings() 0 3 1
A selectPathList() 0 6 1
A compileRecursivePath() 0 3 1
A compileInitialPath() 0 3 1
1
<?php
2
3
namespace Staudenmeir\LaravelAdjacencyList\Query\Grammars;
4
5
use Illuminate\Database\Query\Builder;
6
use Illuminate\Database\Query\Grammars\PostgresGrammar as Base;
7
8
class PostgresGrammar extends Base implements ExpressionGrammar
9
{
10
    /**
11
     * Compile an initial path.
12
     *
13
     * @param string $column
14
     * @param string $alias
15
     * @return string
16
     */
17 49
    public function compileInitialPath($column, $alias)
18
    {
19 49
        return 'array[('.$this->wrap($column)." || '')::varchar] as ".$this->wrap($alias);
20
    }
21
22
    /**
23
     * Compile a recursive path.
24
     *
25
     * @param string $column
26
     * @param string $alias
27
     * @return string
28
     */
29 49
    public function compileRecursivePath($column, $alias)
30
    {
31 49
        return $this->wrap($alias).' || '.$this->wrap($column).'::varchar';
32
    }
33
34
    /**
35
     * Get the recursive path bindings.
36
     *
37
     * @param string $separator
38
     * @return array
39
     */
40 49
    public function getRecursivePathBindings($separator)
41
    {
42 49
        return [];
43
    }
44
45
    /**
46
     * Select a concatenated list of paths.
47
     *
48
     * @param \Illuminate\Database\Query\Builder $query
49
     * @param string $expression
50
     * @param string $column
51
     * @param string $pathSeparator
52
     * @param string $listSeparator
53
     * @return \Illuminate\Database\Query\Builder
54
     */
55 4
    public function selectPathList(Builder $query, $expression, $column, $pathSeparator, $listSeparator)
56
    {
57 4
        return $query->selectRaw(
58 4
            'string_agg(array_to_string('.$this->wrap($column).', ?), ?)',
59 4
            [$pathSeparator, $listSeparator]
60 4
        )->from($expression);
61
    }
62
}
63