99use  ApiSkeletons \Doctrine \ORM \GraphQL \Filter \InputObjectType \Field ;
1010use  ApiSkeletons \Doctrine \ORM \GraphQL \Type \Entity \Entity ;
1111use  ApiSkeletons \Doctrine \ORM \GraphQL \Type \TypeManager ;
12+ use  Doctrine \Common \Collections \ArrayCollection ;
1213use  Doctrine \ORM \EntityManager ;
1314use  Doctrine \ORM \Mapping \ClassMetadata ;
1415use  GraphQL \Type \Definition \InputObjectType  as  GraphQLInputObjectType ;
@@ -118,10 +119,16 @@ protected function addFields(Entity $targetEntity, string $typeName, array $allo
118119                continue ;
119120            }
120121
121-             $ graphQLType $ this typeManager 
122+             $ type $ this typeManager 
122123                ->get ($ entityMetadata'fields ' ][$ fieldName'type ' ]);
123124
124-             if  (! $ graphQLTypeinstanceof  ScalarType) {
125+             // Custom types may hit this condition 
126+             if  (! $ typeinstanceof  ScalarType) {
127+                 continue ;
128+             }
129+ 
130+             // Skip Blob fields 
131+             if  ($ typename () === 'Blob ' ) {
125132                continue ;
126133            }
127134
@@ -139,21 +146,24 @@ static function ($value) use ($fieldExcludeFilters) {
139146                );
140147            }
141148
149+             // Remove filters that are not allowed for this field type 
150+             $ filteredFilters$ this filterFiltersByType ($ allowedFilters$ type
151+ 
142152            // ScalarType field filters are named by their field type 
143153            // and a hash of the allowed filters 
144-             $ filterTypeName'Filters_ '  . $ graphQLType name  . '_ '  . md5 (serialize ($ allowedFilters 
154+             $ filterTypeName'Filters_ '  . $ type name ()  . '_ '  . md5 (serialize ($ filteredFilters 
145155
146156            if  ($ this typeManager ->has ($ filterTypeName
147-                 $ type $ this typeManager ->get ($ filterTypeName
157+                 $ fieldType $ this typeManager ->get ($ filterTypeName
148158            } else  {
149-                 $ type new  Field ($ this typeManager , $ typeName $ fieldName ,  $ graphQLType ,  $ allowedFilters 
150-                 $ this typeManager ->set ($ filterTypeName$ type 
159+                 $ fieldType new  Field ($ this typeManager , $ type $ filteredFilters 
160+                 $ this typeManager ->set ($ filterTypeName$ fieldType 
151161            }
152162
153163            $ fields$ fieldName
154164                'name '         => $ fieldName
155-                 'type '         => $ type 
156-                 'description '  => ' Filters for  .  $ fieldName 
165+                 'type '         => $ fieldType 
166+                 'description '  => $ type -> name () .  ' Filters  '  ,
157167            ];
158168        }
159169
@@ -198,20 +208,99 @@ protected function addAssociations(Entity $targetEntity, string $typeName, array
198208            $ filterTypeName'Filters_ID_ '  . md5 (serialize ($ allowedFilters
199209
200210            if  ($ this typeManager ->has ($ filterTypeName
201-                 $ type $ this typeManager ->get ($ filterTypeName
211+                 $ associationType $ this typeManager ->get ($ filterTypeName
202212            } else  {
203-                 $ type new  Association ($ this typeManager ,  $ typeName ,  $ associationName id (), [Filters::EQ ]);
204-                 $ this typeManager ->set ($ filterTypeName$ type 
213+                 $ associationType new  Association ($ this typeManager , Type::id (), [Filters::EQ ]);
214+                 $ this typeManager ->set ($ filterTypeName$ associationType 
205215            }
206216
207217            // eq filter is for association id from parent entity 
208218            $ fields$ associationName
209219                'name '  => $ associationName
210-                 'type '  => $ type 
211-                 'description '  => 'Filters for  '   .  $ associationName 
220+                 'type '  => $ associationType 
221+                 'description '  => 'Association Filters '  ,
212222            ];
213223        }
214224
215225        return  $ fields
216226    }
227+ 
228+     /** 
229+      * Filter the allowed filters based on the field type 
230+      * 
231+      * @param Filters[] $filters 
232+      * 
233+      * @return Filters[] 
234+      */ 
235+     protected  function  filterFiltersByType (array  $ filtersScalarType $ typearray 
236+     {
237+         $ filterCollectionnew  ArrayCollection ($ filters
238+ 
239+         // Numbers 
240+         if  (
241+             in_array ($ typename (), [
242+                 'Float ' ,
243+                 'ID ' ,
244+                 'Int ' ,
245+                 'Integer ' ,
246+             ])
247+         ) {
248+             $ filterCollectionremoveElement (Filters::CONTAINS );
249+             $ filterCollectionremoveElement (Filters::STARTSWITH );
250+             $ filterCollectionremoveElement (Filters::ENDSWITH );
251+ 
252+             return  $ filterCollectiontoArray ();
253+         }
254+ 
255+         // Booleans 
256+         if  ($ typename () === 'Boolean ' ) {
257+             $ filterCollectionremoveElement (Filters::LT );
258+             $ filterCollectionremoveElement (Filters::LTE );
259+             $ filterCollectionremoveElement (Filters::GT );
260+             $ filterCollectionremoveElement (Filters::GTE );
261+             $ filterCollectionremoveElement (Filters::BETWEEN );
262+             $ filterCollectionremoveElement (Filters::CONTAINS );
263+             $ filterCollectionremoveElement (Filters::STARTSWITH );
264+             $ filterCollectionremoveElement (Filters::ENDSWITH );
265+ 
266+             return  $ filterCollectiontoArray ();
267+         }
268+ 
269+         // Strings 
270+         if  (
271+             in_array ($ typename (), [
272+                 'String ' ,
273+                 'Text ' ,
274+             ])
275+         ) {
276+             $ filterCollectionremoveElement (Filters::LT );
277+             $ filterCollectionremoveElement (Filters::LTE );
278+             $ filterCollectionremoveElement (Filters::GT );
279+             $ filterCollectionremoveElement (Filters::GTE );
280+             $ filterCollectionremoveElement (Filters::BETWEEN );
281+ 
282+             return  $ filterCollectiontoArray ();
283+         }
284+ 
285+         // Dates and times 
286+         if  (
287+             in_array ($ typename (), [
288+                 'Date ' ,
289+                 'DateTime ' ,
290+                 'DateTimeImmutable ' ,
291+                 'DateTimeTZ ' ,
292+                 'DateTimeTZImmutable ' ,
293+                 'Time ' ,
294+                 'TimeImmutable ' ,
295+             ])
296+         ) {
297+             $ filterCollectionremoveElement (Filters::CONTAINS );
298+             $ filterCollectionremoveElement (Filters::STARTSWITH );
299+             $ filterCollectionremoveElement (Filters::ENDSWITH );
300+ 
301+             return  $ filterCollectiontoArray ();
302+         }
303+ 
304+         return  $ filterCollectiontoArray ();
305+     }
217306}
0 commit comments