4
4
5
5
namespace Asseco \JsonQueryBuilder \Config ;
6
6
7
- use Doctrine \ DBAL \ DBALException ;
7
+ use Exception ;
8
8
use Illuminate \Database \Eloquent \Model ;
9
+ use Illuminate \Support \Arr ;
9
10
use Illuminate \Support \Facades \Cache ;
10
- use Illuminate \Support \Facades \Config ;
11
11
use Illuminate \Support \Facades \DB ;
12
12
use Illuminate \Support \Facades \Schema ;
13
13
@@ -35,19 +35,19 @@ protected function getConfig(): array
35
35
return config ('asseco-json-query-builder.model_options. ' . get_class ($ this ->model ));
36
36
}
37
37
38
- public function getReturns ()
38
+ public function getReturns (): array
39
39
{
40
40
if (array_key_exists ('returns ' , $ this ->config ) && $ this ->config ['returns ' ]) {
41
- return $ this ->config ['returns ' ];
41
+ return Arr:: wrap ( $ this ->config ['returns ' ]) ;
42
42
}
43
43
44
44
return ['* ' ];
45
45
}
46
46
47
- public function getRelations ()
47
+ public function getRelations (): array
48
48
{
49
49
if (array_key_exists ('relations ' , $ this ->config ) && $ this ->config ['relations ' ]) {
50
- return $ this ->config ['relations ' ];
50
+ return Arr:: wrap ( $ this ->config ['relations ' ]) ;
51
51
}
52
52
53
53
return [];
@@ -82,27 +82,29 @@ public function getForbidden(array $forbiddenKeys)
82
82
83
83
protected function getEloquentExclusion ($ forbiddenKeys ): array
84
84
{
85
- if (array_key_exists ('eloquent_exclusion ' , $ this ->config ) && $ this ->config ['eloquent_exclusion ' ]) {
86
- $ guarded = $ this ->model ->getGuarded ();
87
- $ fillable = $ this ->model ->getFillable ();
88
-
89
- if ($ guarded [0 ] != '* ' ) { // Guarded property is never empty. It is '*' by default.
90
- $ forbiddenKeys = array_merge ($ forbiddenKeys , $ guarded );
91
- } elseif (count ($ fillable ) > 0 ) {
92
- $ forbiddenKeys = array_diff (array_keys ($ this ->getModelColumns ()), $ fillable );
93
- }
85
+ if (!array_key_exists ('eloquent_exclusion ' , $ this ->config ) || !($ this ->config ['eloquent_exclusion ' ])) {
86
+ return $ forbiddenKeys ;
87
+ }
88
+
89
+ $ guarded = $ this ->model ->getGuarded ();
90
+ $ fillable = $ this ->model ->getFillable ();
91
+
92
+ if ($ guarded [0 ] != '* ' ) { // Guarded property is never empty. It is '*' by default.
93
+ $ forbiddenKeys = array_merge ($ forbiddenKeys , $ guarded );
94
+ } elseif (count ($ fillable ) > 0 ) {
95
+ $ forbiddenKeys = array_diff (array_keys ($ this ->getModelColumns ()), $ fillable );
94
96
}
95
97
96
98
return $ forbiddenKeys ;
97
99
}
98
100
99
101
protected function getForbiddenColumns (array $ forbiddenKeys ): array
100
102
{
101
- if (array_key_exists ('forbidden_columns ' , $ this ->config ) && $ this ->config ['forbidden_columns ' ]) {
102
- $ forbiddenKeys = array_merge ( $ forbiddenKeys, $ this -> config [ ' forbidden_columns ' ]) ;
103
+ if (! array_key_exists ('forbidden_columns ' , $ this ->config ) || !( $ this ->config ['forbidden_columns ' ]) ) {
104
+ return $ forbiddenKeys ;
103
105
}
104
106
105
- return $ forbiddenKeys ;
107
+ return array_merge ( $ forbiddenKeys, $ this -> config [ ' forbidden_columns ' ]) ;
106
108
}
107
109
108
110
/**
@@ -122,19 +124,36 @@ public function getModelColumns(): array
122
124
$ columns = Schema::getColumnListing ($ table );
123
125
$ modelColumns = [];
124
126
125
- // having 'enum' in table definition will throw Doctrine error because it is not defined in their types.
126
- // Registering it manually.
127
- DB ::connection ()->getDoctrineSchemaManager ()->getDatabasePlatform ()->registerDoctrineTypeMapping ('enum ' , 'string ' );
127
+ $ this ->registerEnumTypeForDoctrine ();
128
+
128
129
try {
129
130
foreach ($ columns as $ column ) {
130
131
$ modelColumns [$ column ] = DB ::getSchemaBuilder ()->getColumnType ($ table , $ column );
131
132
}
132
- } catch (DBALException $ e ) {
133
+ } catch (Exception $ e ) {
133
134
// leave model columns as an empty array and cache it.
134
135
}
135
136
136
137
Cache::put (self ::CACHE_PREFIX . $ table , $ modelColumns , self ::CACHE_TTL );
137
138
138
139
return $ modelColumns ;
139
140
}
141
+
142
+ /**
143
+ * Having 'enum' in table definition will throw Doctrine error because it is not defined in their types.
144
+ * Registering it manually.
145
+ */
146
+ protected function registerEnumTypeForDoctrine (): void
147
+ {
148
+ $ connection = DB ::connection ();
149
+
150
+ if (!class_exists ('Doctrine\DBAL\Driver\AbstractSQLiteDriver ' )) {
151
+ return ;
152
+ }
153
+
154
+ $ connection
155
+ ->getDoctrineSchemaManager ()
156
+ ->getDatabasePlatform ()
157
+ ->registerDoctrineTypeMapping ('enum ' , 'string ' );
158
+ }
140
159
}
0 commit comments