Passed
Pull Request — master (#38)
by Bas
17:19
created

HasAliases::prefixAlias()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 9
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace LaravelFreelancerNL\Aranguent\Query\Concerns;
4
5
use Illuminate\Database\Query\Builder as IluminateBuilder;
6
use Illuminate\Support\Str;
7
use LaravelFreelancerNL\Aranguent\Query\Builder;
8
9
trait HasAliases
10
{
11
12
    protected $tableAliases = [];
13
14
    protected $columnAliases = [];
15
16
    /**
17
     * @param  string  $table
18
     * @param  string  $alias
19
     * @return string
20
     */
21
    public function registerTableAlias(string $table, string $alias = null): string
22
    {
23
        if ($alias == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $alias of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
24
            $alias = $this->generateTableAlias($table);
25
        }
26
        $this->tableAliases[$table] = $alias;
27
28
        return $alias;
29
    }
30
31
    protected function isTableAlias(string $alias)
32
    {
33
        return in_array($alias, $this->tableAliases);
34
    }
35
36
    /**
37
     * @param $table
38
     * @return mixed|null
39
     */
40
    protected function getTableAlias($table)
41
    {
42
        if (isset($this->tableAliases[$table])) {
43
            return $this->tableAliases[$table];
44
        }
45
46
        return null;
47
    }
48
49
    /**
50
     * Extract table and alias from sql alias notation (entity AS `alias`)
51
     *
52
     * @param  string  $entity
53
     * @return array|false|string[]
54
     */
55
    protected function extractAlias(string $entity)
56
    {
57
        $results = preg_split("/\sas\s/i", $entity);
58
        if (isset($results[1])) {
59
            $results[1] = trim($results[1], '`');
60
        }
61
        if (! isset($results[1])) {
62
            $results[1] = $results[0];
63
        }
64
65
        return $results;
66
    }
67
68
    /**
69
     * @param $table
70
     * @param string $postfix
71
     *
72
     * @return mixed
73
     */
74
    protected function generateTableAlias($table, $postfix = 'Doc')
75
    {
76
        return Str::singular($table) . $postfix;
77
    }
78
79
    protected function replaceTableForAlias($reference): string
80
    {
81
        $referenceParts = explode('.', $reference);
82
        $first = array_shift($referenceParts);
83
        $alias = $this->getTableAlias($first);
84
        if ($alias == null) {
85
            $alias = $first;
86
        }
87
        array_unshift($referenceParts, $alias);
88
89
        return implode('.', $referenceParts);
90
    }
91
92
    /**
93
     * @param string  $target
94
     * @param string  $value
95
     *
96
     * @return Builder
97
     */
98
    protected function prefixAlias(string $target, string $value): string
99
    {
100
        $alias = $this->getTableAlias($target);
101
102
        if (Str::startsWith($value, $alias . '.')) {
103
            return $value;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $value returns the type string which is incompatible with the documented return type LaravelFreelancerNL\Aranguent\Query\Builder.
Loading history...
104
        }
105
106
        return $alias . '.' . $value;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $alias . '.' . $value returns the type string which is incompatible with the documented return type LaravelFreelancerNL\Aranguent\Query\Builder.
Loading history...
107
    }
108
109
    /**
110
     * @param  string  $column
111
     * @param  string|null  $alias
112
     * @return bool
113
     */
114
    protected function registerColumnAlias(string $column, string $alias = null)
115
    {
116
        if (preg_match("/\sas\s/i", $column)) {
117
            [$column, $alias] = $this->extractAlias($column);
118
        }
119
120
        if (isset($alias)) {
121
            $this->columnAliases[$column] = $alias;
122
            return true;
123
        }
124
125
        return false;
126
    }
127
128
    /**
129
     * @param $column
130
     * @return mixed
131
     */
132
    protected function getColumnAlias(string $column)
133
    {
134
        if (isset($this->columnAliases[$column])) {
135
            return $this->columnAliases[$column];
136
        }
137
138
        return null;
139
    }
140
141
    /**
142
     * @param  IluminateBuilder  $query
143
     * @param $column
144
     * @param $table
145
     * @return string
146
     */
147
    protected function normalizeColumn(IluminateBuilder $query, $column, $table = null)
148
    {
149
        if ($table == null) {
150
            $table = $query->from;
151
        }
152
153
        // Replace SQL JSON arrow for AQL dot
154
        $column = str_replace('->', '.', $column);
155
156
        $references = explode('.', $column);
157
158
        //We check for an existing alias to determine of the first reference is a table.
159
        // In which case we replace it with the alias.
160
        $tableAlias = $this->getTableAlias($references[0]);
161
        if (isset($tableAlias)) {
162
            $references[0] = $tableAlias;
163
        }
164
165
        if ($tableAlias === null && ! $this->isTableAlias($references[0])) {
166
            $tableAlias = $this->generateTableAlias($table);
167
            array_unshift($references, $tableAlias);
168
        }
169
170
        return implode('.', $references);
171
    }
172
}
173