3
3
namespace Rappasoft \LaravelLivewireTables \Utilities ;
4
4
5
5
use Illuminate \Database \Eloquent \Builder as EloquentBuilder ;
6
- use Illuminate \Database \Query \Builder as Builder ;
6
+ use Illuminate \Database \Query \Builder ;
7
7
use Illuminate \Support \Str ;
8
8
9
+ /**
10
+ * Class ColumnUtilities
11
+ *
12
+ * @package Rappasoft\LaravelLivewireTables\Utilities
13
+ */
9
14
class ColumnUtilities
10
15
{
11
16
/**
@@ -14,7 +19,7 @@ class ColumnUtilities
14
19
* @param $column
15
20
* @return bool
16
21
*/
17
- public static function hasRelation ($ column )
22
+ public static function hasRelation ($ column ): bool
18
23
{
19
24
return Str::contains ($ column , '. ' );
20
25
}
@@ -25,7 +30,7 @@ public static function hasRelation($column)
25
30
* @param $column
26
31
* @return string
27
32
*/
28
- public static function parseRelation ($ column )
33
+ public static function parseRelation ($ column ): string
29
34
{
30
35
return Str::beforeLast ($ column , '. ' );
31
36
}
@@ -36,7 +41,7 @@ public static function parseRelation($column)
36
41
* @param $column
37
42
* @return string
38
43
*/
39
- public static function parseField ($ column )
44
+ public static function parseField ($ column ): string
40
45
{
41
46
return Str::afterLast ($ column , '. ' );
42
47
}
@@ -48,9 +53,9 @@ public static function parseField($column)
48
53
* @param $searchColumns
49
54
* @return bool
50
55
*/
51
- public static function hasMatch ($ column , $ searchColumns )
56
+ public static function hasMatch ($ column , $ searchColumns ): bool
52
57
{
53
- return array_search ($ column , $ searchColumns ?? []) !== false ;
58
+ return in_array ($ column , $ searchColumns ?? [], true ) ;
54
59
}
55
60
56
61
/**
@@ -60,42 +65,44 @@ public static function hasMatch($column, $searchColumns)
60
65
* @param $searchColumns
61
66
* @return bool
62
67
*/
63
- public static function hasWildcardMatch ($ column , $ searchColumns )
68
+ public static function hasWildcardMatch ($ column , $ searchColumns ): bool
64
69
{
65
70
return count (array_filter ($ searchColumns ?? [], function ($ searchColumn ) use ($ column ) {
66
-
67
- // match wildcards such as * or table.*
71
+ // Match wildcards such as * or table.*
68
72
$ hasWildcard = Str::endsWith ($ searchColumn , '* ' );
69
73
70
- // if no wildcard, skip
74
+ // If no wildcard, skip
71
75
if (! $ hasWildcard ) {
72
76
return false ;
73
77
}
74
78
75
79
if (! self ::hasRelation ($ column )) {
76
80
return true ;
77
- } else {
78
- $ selectColumnPrefix = self ::parseRelation ($ searchColumn );
79
- $ columnPrefix = self ::parseRelation ($ column );
80
-
81
- return $ selectColumnPrefix === $ columnPrefix ;
82
81
}
82
+
83
+ $ selectColumnPrefix = self ::parseRelation ($ searchColumn );
84
+ $ columnPrefix = self ::parseRelation ($ column );
85
+
86
+ return $ selectColumnPrefix === $ columnPrefix ;
83
87
})) > 0 ;
84
88
}
85
89
86
90
/**
87
- * @param EloquentBuilder|Builder $queryBuilder
88
- * @return null
91
+ * @param null $queryBuilder
92
+ *
93
+ * @return array|null
89
94
*/
90
- public static function columnsFromBuilder ($ queryBuilder = null )
95
+ public static function columnsFromBuilder ($ queryBuilder = null ): ? array
91
96
{
92
97
if ($ queryBuilder instanceof EloquentBuilder) {
93
98
return $ queryBuilder ->getQuery ()->columns ;
94
- } elseif ($ queryBuilder instanceof Builder) {
99
+ }
100
+
101
+ if ($ queryBuilder instanceof Builder) {
95
102
return $ queryBuilder ->columns ;
96
- } else {
97
- return null ;
98
103
}
104
+
105
+ return null ;
99
106
}
100
107
101
108
/**
@@ -105,48 +112,48 @@ public static function columnsFromBuilder($queryBuilder = null)
105
112
* @param $queryBuilder
106
113
* @return string
107
114
*/
108
- public static function mapToSelected ($ column , $ queryBuilder )
115
+ public static function mapToSelected ($ column , $ queryBuilder ): ? string
109
116
{
110
- // grab select
117
+ // Grab select
111
118
$ select = self ::columnsFromBuilder ($ queryBuilder );
112
119
113
- // can 't match anything if no select
120
+ // Can 't match anything if no select
114
121
if (is_null ($ select )) {
115
122
return null ;
116
123
}
117
124
118
- // search builder select for a match
125
+ // Search builder select for a match
119
126
$ hasMatch = self ::hasMatch ($ column , $ select );
120
127
121
128
// example 2 - match
122
129
// column: service_statuses.name
123
130
// select: service_statuses.name
124
131
// maps to: service_statuses.name
125
132
126
- // if we found a match, lets use that instead of searching relations
133
+ // If we found a match, lets use that instead of searching relations
127
134
if ($ hasMatch ) {
128
135
return $ column ;
129
136
}
130
137
131
- // search builder select for a wildcard match
138
+ // Search builder select for a wildcard match
132
139
$ hasWildcardMatch = self ::hasWildcardMatch ($ column , $ select );
133
140
134
141
// example 3 - wildcard match
135
142
// column: service_statuses.name
136
143
// select: service_statuses.*
137
144
// maps to: service_statuses.name
138
145
139
- // if we found a wildcard match, lets use that instead of matching relations
146
+ // If we found a wildcard match, lets use that instead of matching relations
140
147
if ($ hasWildcardMatch ) {
141
148
return $ column ;
142
149
}
143
150
144
- // split the relation and field
151
+ // Split the relation and field
145
152
$ hasRelation = self ::hasRelation ($ column );
146
153
$ relationName = self ::parseRelation ($ column );
147
154
$ fieldName = self ::parseField ($ column );
148
155
149
- // we know there is a relation and we know it doesn't match any of the
156
+ // We know there is a relation and we know it doesn't match any of the
150
157
// select columns. Let's try to grab the table name for the relation
151
158
// and see if that matches something in the select
152
159
//
@@ -155,49 +162,45 @@ public static function mapToSelected($column, $queryBuilder)
155
162
// select: service_statuses.name
156
163
// maps to: service_statuses.name
157
164
158
- // if we didn't previously match the column and there isn't a relation
165
+ // If we didn't previously match the column and there isn't a relation
159
166
if (! $ hasRelation ) {
160
-
161
- // there's nothing else to do
167
+ // There's nothing else to do
162
168
return null ;
169
+ }
163
170
164
- // this is easiest when using the eloquent query builder
165
- } elseif ($ queryBuilder instanceof EloquentBuilder) {
171
+ // This is easiest when using the eloquent query builder
172
+ if ($ queryBuilder instanceof EloquentBuilder) {
166
173
$ relation = $ queryBuilder ->getRelation ($ relationName );
167
174
$ possibleTable = $ relation ->getModel ()->getTable ();
168
175
} elseif ($ queryBuilder instanceof Builder) {
169
-
170
- // @todo: possible ways to do this?
176
+ // TODO: Possible ways to do this?
171
177
$ possibleTable = null ;
172
178
} else {
173
-
174
- // we would have already returned before this is possible
179
+ // We would have already returned before this is possible
175
180
$ possibleTable = null ;
176
181
}
177
182
178
- // if we found a possible table
183
+ // If we found a possible table
179
184
if (! is_null ($ possibleTable )) {
180
-
181
- // build possible selected column
185
+ // Build possible selected column
182
186
$ possibleSelectColumn = $ possibleTable . '. ' . $ fieldName ;
183
187
184
188
$ possibleMatch = self ::hasMatch ($ possibleSelectColumn , $ select );
185
189
186
- // if we found a possible match for a relation to an already selected
187
- // column, let's use that
190
+ // If we found a possible match for a relation to an already selected column, let's use that
188
191
if ($ possibleMatch ) {
189
192
return $ possibleSelectColumn ;
190
193
}
191
194
192
195
$ possibleWildcardMatch = self ::hasWildcardMatch ($ possibleSelectColumn , $ select );
193
196
194
- // ditto with a possible wildcard match
197
+ // Ditto with a possible wildcard match
195
198
if ($ possibleWildcardMatch ) {
196
199
return $ possibleSelectColumn ;
197
200
}
198
201
}
199
202
200
- // we couldn't match to a selected column
203
+ // We couldn't match to a selected column
201
204
return null ;
202
205
}
203
206
}
0 commit comments