1414use ReflectionClass ;
1515
1616use function assert ;
17- use function in_array ;
18- use function str_replace ;
19- use function strlen ;
20- use function strpos ;
21- use function substr ;
2217
23- class MetadataFactory
18+ class MetadataFactory extends AbstractMetadataFactory
2419{
2520 protected Metadata |null $ metadata = null ;
2621 protected EntityManager $ entityManager ;
@@ -48,74 +43,6 @@ public function getMetadata(): Metadata
4843 return $ this ->buildMetadata ();
4944 }
5045
51- /** @param string[] $entityClasses */
52- private function globalEnable (array $ entityClasses ): Metadata
53- {
54- $ globalIgnore = $ this ->config ->getGlobalIgnore ();
55-
56- foreach ($ entityClasses as $ entityClass ) {
57- // Get extract by value or reference
58- $ byValue = $ this ->config ->getGlobalByValue () ?? true ;
59-
60- // Save entity-level metadata
61- $ this ->metadataConfig [$ entityClass ] = [
62- 'entityClass ' => $ entityClass ,
63- 'byValue ' => $ byValue ,
64- 'namingStrategy ' => null ,
65- 'fields ' => [],
66- 'filters ' => [],
67- 'excludeCriteria ' => [],
68- 'description ' => $ entityClass ,
69- 'typeName ' => $ this ->getTypeName ($ entityClass ),
70- ];
71-
72- // Fetch fields
73- $ entityClassMetadata = $ this ->entityManager ->getMetadataFactory ()->getMetadataFor ($ entityClass );
74- $ fieldNames = $ entityClassMetadata ->getFieldNames ();
75-
76- foreach ($ fieldNames as $ fieldName ) {
77- if (in_array ($ fieldName , $ globalIgnore )) {
78- continue ;
79- }
80-
81- $ this ->metadataConfig [$ entityClass ]['fields ' ][$ fieldName ]['description ' ] =
82- $ fieldName ;
83-
84- $ this ->metadataConfig [$ entityClass ]['fields ' ][$ fieldName ]['type ' ] =
85- $ entityClassMetadata ->getTypeOfField ($ fieldName );
86-
87- // Set default strategy based on field type
88- $ this ->metadataConfig [$ entityClass ]['fields ' ][$ fieldName ]['strategy ' ] =
89- $ this ->getDefaultStrategy ($ entityClassMetadata ->getTypeOfField ($ fieldName ));
90-
91- $ this ->metadataConfig [$ entityClass ]['fields ' ][$ fieldName ]['excludeCriteria ' ] = [];
92- }
93-
94- // Fetch attributes for associations
95- $ associationNames = $ this ->entityManager ->getMetadataFactory ()
96- ->getMetadataFor ($ entityClass )->getAssociationNames ();
97-
98- foreach ($ associationNames as $ associationName ) {
99- if (in_array ($ associationName , $ globalIgnore )) {
100- continue ;
101- }
102-
103- $ this ->metadataConfig [$ entityClass ]['fields ' ][$ associationName ]['excludeCriteria ' ] = [];
104- $ this ->metadataConfig [$ entityClass ]['fields ' ][$ associationName ]['description ' ] = $ associationName ;
105- $ this ->metadataConfig [$ entityClass ]['fields ' ][$ associationName ]['filterCriteriaEventName ' ]
106- = null ;
107-
108- // NullifyOwningAssociation is not used for globalEnable
109- $ this ->metadataConfig [$ entityClass ]['fields ' ][$ associationName ]['strategy ' ] =
110- Strategy \AssociationDefault::class;
111- }
112- }
113-
114- $ this ->metadata = new Metadata ($ this ->container , $ this ->metadataConfig );
115-
116- return $ this ->metadata ;
117- }
118-
11946 protected function buildMetadata (): Metadata
12047 {
12148 $ entityClasses = [];
@@ -124,7 +51,9 @@ protected function buildMetadata(): Metadata
12451 }
12552
12653 if ($ this ->config ->getGlobalEnable ()) {
127- return $ this ->globalEnable ($ entityClasses );
54+ $ globalEnable = $ this ->container ->get (GlobalEnable::class);
55+
56+ return new Metadata ($ this ->container , $ globalEnable ($ entityClasses ));
12857 }
12958
13059 foreach ($ entityClasses as $ entityClass ) {
@@ -286,64 +215,4 @@ private function buildMetadataConfigForAssociations(
286215 }
287216 }
288217 }
289-
290- /**
291- * Strip the configured entityPrefix from the type name
292- */
293- private function stripEntityPrefix (string $ entityClass ): string
294- {
295- if ($ this ->config ->getEntityPrefix () !== null ) {
296- if (strpos ($ entityClass , $ this ->config ->getEntityPrefix ()) === 0 ) {
297- $ entityClass = substr ($ entityClass , strlen ($ this ->config ->getEntityPrefix ()));
298- }
299- }
300-
301- return str_replace ('\\' , '_ ' , $ entityClass );
302- }
303-
304- /**
305- * Append the configured groupSuffix from the type name
306- */
307- private function appendGroupSuffix (string $ entityClass ): string
308- {
309- if ($ this ->config ->getGroupSuffix () !== null ) {
310- if ($ this ->config ->getGroupSuffix ()) {
311- $ entityClass .= '_ ' . $ this ->config ->getGroupSuffix ();
312- }
313- } else {
314- $ entityClass .= '_ ' . $ this ->config ->getGroup ();
315- }
316-
317- return $ entityClass ;
318- }
319-
320- /**
321- * Compute the GraphQL type name
322- */
323- private function getTypeName (string $ entityClass ): string
324- {
325- return $ this ->appendGroupSuffix ($ this ->stripEntityPrefix ($ entityClass ));
326- }
327-
328- private function getDefaultStrategy (string |null $ fieldType ): string
329- {
330- // Set default strategy based on field type
331- switch ($ fieldType ) {
332- case 'tinyint ' :
333- case 'smallint ' :
334- case 'integer ' :
335- case 'int ' :
336- return Strategy \ToInteger::class;
337-
338- case 'boolean ' :
339- return Strategy \ToBoolean::class;
340-
341- case 'decimal ' :
342- case 'float ' :
343- return Strategy \ToFloat::class;
344-
345- default :
346- return Strategy \FieldDefault::class;
347- }
348- }
349218}
0 commit comments