44
55namespace Asseco \JsonQueryBuilder \Config ;
66
7- use Doctrine \ DBAL \ DBALException ;
7+ use Exception ;
88use Illuminate \Database \Eloquent \Model ;
9+ use Illuminate \Support \Arr ;
910use Illuminate \Support \Facades \Cache ;
10- use Illuminate \Support \Facades \Config ;
1111use Illuminate \Support \Facades \DB ;
1212use Illuminate \Support \Facades \Schema ;
1313
@@ -35,19 +35,19 @@ protected function getConfig(): array
3535 return config ('asseco-json-query-builder.model_options. ' . get_class ($ this ->model ));
3636 }
3737
38- public function getReturns ()
38+ public function getReturns (): array
3939 {
4040 if (array_key_exists ('returns ' , $ this ->config ) && $ this ->config ['returns ' ]) {
41- return $ this ->config ['returns ' ];
41+ return Arr:: wrap ( $ this ->config ['returns ' ]) ;
4242 }
4343
4444 return ['* ' ];
4545 }
4646
47- public function getRelations ()
47+ public function getRelations (): array
4848 {
4949 if (array_key_exists ('relations ' , $ this ->config ) && $ this ->config ['relations ' ]) {
50- return $ this ->config ['relations ' ];
50+ return Arr:: wrap ( $ this ->config ['relations ' ]) ;
5151 }
5252
5353 return [];
@@ -82,27 +82,29 @@ public function getForbidden(array $forbiddenKeys)
8282
8383 protected function getEloquentExclusion ($ forbiddenKeys ): array
8484 {
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 );
9496 }
9597
9698 return $ forbiddenKeys ;
9799 }
98100
99101 protected function getForbiddenColumns (array $ forbiddenKeys ): array
100102 {
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 ;
103105 }
104106
105- return $ forbiddenKeys ;
107+ return array_merge ( $ forbiddenKeys, $ this -> config [ ' forbidden_columns ' ]) ;
106108 }
107109
108110 /**
@@ -122,19 +124,36 @@ public function getModelColumns(): array
122124 $ columns = Schema::getColumnListing ($ table );
123125 $ modelColumns = [];
124126
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+
128129 try {
129130 foreach ($ columns as $ column ) {
130131 $ modelColumns [$ column ] = DB ::getSchemaBuilder ()->getColumnType ($ table , $ column );
131132 }
132- } catch (DBALException $ e ) {
133+ } catch (Exception $ e ) {
133134 // leave model columns as an empty array and cache it.
134135 }
135136
136137 Cache::put (self ::CACHE_PREFIX . $ table , $ modelColumns , self ::CACHE_TTL );
137138
138139 return $ modelColumns ;
139140 }
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+ }
140159}
0 commit comments