2929
3030namespace whatwedo \TableBundle \Filter ;
3131
32- use Doctrine \Common \Annotations \AnnotationReader ;
3332use Doctrine \ORM \EntityManagerInterface ;
34- use Doctrine \ORM \Mapping \Column ;
35- use Doctrine \ORM \Mapping \ManyToMany ;
36- use Doctrine \ORM \Mapping \ManyToOne ;
37- use Doctrine \ORM \Mapping \OneToMany ;
33+ use Doctrine \ORM \Mapping \ClassMetadataInfo ;
34+ use Doctrine \ORM \Mapping \FieldMapping ;
35+ use Doctrine \ORM \Mapping \ManyToManyAssociationMapping ;
36+ use Doctrine \ORM \Mapping \ManyToManyInverseSideMapping ;
37+ use Doctrine \ORM \Mapping \ManyToManyOwningSideMapping ;
38+ use Doctrine \ORM \Mapping \ManyToOneAssociationMapping ;
39+ use Doctrine \ORM \Mapping \OneToManyAssociationMapping ;
40+ use Doctrine \ORM \Mapping \ToManyAssociationMapping ;
41+ use Doctrine \ORM \Mapping \ToOneAssociationMapping ;
3842use whatwedo \TableBundle \Filter \Type \AjaxManyToManyFilterType ;
3943use whatwedo \TableBundle \Filter \Type \AjaxOneToManyFilterType ;
4044use whatwedo \TableBundle \Filter \Type \AjaxRelationFilterType ;
@@ -58,39 +62,29 @@ class FilterGuesser
5862 'boolean ' => BooleanFilterType::class,
5963 ];
6064
61- private const RELATION_TYPE = [
62- OneToMany::class => AjaxOneToManyFilterType::class,
63- ManyToOne::class => AjaxRelationFilterType::class,
64- ManyToMany::class => AjaxManyToManyFilterType::class,
65- ];
66-
6765 public function __construct (
6866 protected EntityManagerInterface $ entityManager
6967 ) {
7068 }
7169
7270 public function getFilterType (
73- \ReflectionProperty $ property ,
7471 string $ accessor ,
7572 string $ acronym ,
7673 callable $ jsonSearchCallable ,
7774 array $ joins ,
78- string $ namespace
79- ): ?FilterTypeInterface {
80- $ all = $ this ->getAnnotationsAndAttributes ($ property );
81- foreach ($ all as $ holder ) {
82- $ class = $ this ->getClass ($ holder );
83- $ type = $ this ->getType ($ holder , $ property );
84- $ targetEntity = $ this ->getTargetEntity ($ holder , $ property );
85- $ result = match ($ class ) {
86- Column::class => $ this ->newColumnFilter ($ type , $ accessor ),
87- ManyToMany::class => $ this ->newManyToManyFilter ($ class , $ acronym , $ targetEntity , $ jsonSearchCallable , $ joins ),
88- OneToMany::class, ManyToOne::class => $ this ->newRelationFilter ($ class , $ acronym , $ targetEntity , $ namespace , $ jsonSearchCallable , $ joins ),
89- default => null ,
90- };
91- if ($ result ) {
92- return $ result ;
93- }
75+ array $ mapping ,
76+ ): ?FilterTypeInterface
77+ {
78+ $ result = match (true ) {
79+ is_string ($ mapping ['type ' ]) => $ this ->newColumnFilter ($ mapping ['type ' ], $ accessor ),
80+ $ mapping ['type ' ] === ClassMetadataInfo::MANY_TO_MANY => $ this ->newManyToManyFilter ($ acronym , $ mapping ['targetEntity ' ], $ jsonSearchCallable , $ joins ),
81+ $ mapping ['type ' ] === ClassMetadataInfo::ONE_TO_MANY => $ this ->newRelationFilter (AjaxOneToManyFilterType::class, $ acronym , $ mapping ['targetEntity ' ], $ jsonSearchCallable , $ joins ),
82+ $ mapping ['type ' ] === ClassMetadataInfo::MANY_TO_ONE => $ this ->newRelationFilter (AjaxRelationFilterType::class, $ acronym , $ mapping ['targetEntity ' ], $ jsonSearchCallable , $ joins ),
83+ default => null ,
84+ };
85+
86+ if ($ result ) {
87+ return $ result ;
9488 }
9589
9690 return null ;
@@ -105,13 +99,9 @@ private function newColumnFilter(string $type, string $accessor): ?FilterTypeInt
10599 return new (self ::SCALAR_TYPES [$ type ])($ accessor );
106100 }
107101
108- private function newManyToManyFilter (string $ class , string $ acronym , string $ targetEntity , callable $ jsonSearchCallable , array $ joins ): ? FilterTypeInterface
102+ private function newManyToManyFilter (string $ acronym , string $ targetEntity , callable $ jsonSearchCallable , array $ joins ): FilterTypeInterface
109103 {
110- if (! isset (self ::RELATION_TYPE [$ class ])) {
111- return null ;
112- }
113-
114- return new (self ::RELATION_TYPE [$ class ])(
104+ return new AjaxManyToManyFilterType (
115105 $ acronym ,
116106 $ targetEntity ,
117107 $ this ->entityManager ,
@@ -120,88 +110,14 @@ private function newManyToManyFilter(string $class, string $acronym, string $tar
120110 );
121111 }
122112
123- private function newRelationFilter (string $ class , string $ acronym , string $ targetEntity , string $ namespace , callable $ jsonSearchCallable , array $ joins ): ? FilterTypeInterface
113+ private function newRelationFilter (string $ filterClass , string $ acronym , string $ targetEntity , callable $ jsonSearchCallable , array $ joins ): FilterTypeInterface
124114 {
125- if (! isset (self ::RELATION_TYPE [$ class ])) {
126- return null ;
127- }
128-
129- if (mb_strpos ($ targetEntity , '\\' ) === false ) {
130- $ targetEntity = $ namespace . '\\' . $ targetEntity ;
131- }
132-
133- return new (self ::RELATION_TYPE [$ class ])(
115+ return new ($ filterClass )(
134116 $ acronym ,
135117 $ targetEntity ,
136118 $ this ->entityManager ,
137119 $ jsonSearchCallable ($ targetEntity ),
138120 $ joins
139121 );
140122 }
141-
142- private function getAnnotationsAndAttributes (\ReflectionProperty $ property ): ?array
143- {
144- $ annotations = (new AnnotationReader ())->getPropertyAnnotations ($ property );
145- $ attributes = $ property ->getAttributes ();
146-
147- return [...$ annotations , ...$ attributes ];
148- }
149-
150- private function getFieldMapping (\ReflectionProperty $ property ): array
151- {
152- $ meta = $ this ->entityManager ->getClassMetadata ($ property ->getDeclaringClass ()->getName ());
153- $ mappings = array_merge ($ meta ->fieldMappings , $ meta ->associationMappings );
154- if (! isset ($ mappings [$ property ->getName ()])) {
155- return [];
156- }
157-
158- return $ mappings [$ property ->getName ()];
159- }
160-
161- private function isAttribute (mixed $ x ): bool
162- {
163- return $ x instanceof \ReflectionAttribute;
164- }
165-
166- private function getClass (mixed $ x ): ?string
167- {
168- if ($ this ->isAttribute ($ x )) {
169- return $ x ->getName ();
170- }
171-
172- return get_class ($ x );
173- }
174-
175- private function getType (mixed $ x , \ReflectionProperty $ property ): null |string |int
176- {
177- return $ this ->getXYZ ($ x , $ property , 'type ' );
178- }
179-
180- private function getTargetEntity (mixed $ x , \ReflectionProperty $ property ): null |string |int
181- {
182- return $ this ->getXYZ ($ x , $ property , 'targetEntity ' );
183- }
184-
185- private function getXYZ (mixed $ x , \ReflectionProperty $ property , string $ what ): null |string |int
186- {
187- if ($ this ->isAttribute ($ x )) {
188- $ arguments = $ x ->getArguments ();
189- if (isset ($ arguments [$ what ])) {
190- return $ arguments [$ what ];
191- }
192- }
193-
194- if (! $ this ->isAttribute ($ x )) {
195- if (property_exists ($ x , $ what )) {
196- return $ x ->{$ what };
197- }
198- }
199-
200- $ fieldMappings = $ this ->getFieldMapping ($ property );
201- if (isset ($ fieldMappings [$ what ])) {
202- return $ fieldMappings [$ what ];
203- }
204-
205- return null ;
206- }
207123}
0 commit comments