| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Cycle\Annotated\Utils; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Cycle\Annotated\Annotation\Entity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Cycle\Annotated\Entities; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Doctrine\Inflector\Rules\English\InflectorFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Spiral\Attributes\ReaderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @internal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class EntityUtils | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     private \Doctrine\Inflector\Inflector $inflector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 | 1056 |  |     public function __construct(private ReaderInterface $reader) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 1056 |  |         $this->inflector = (new InflectorFactory())->build(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 1056 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @param class-string $class | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 1032 |  |     public function hasParent(string $class, bool $root = true): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 1032 |  |         return $this->findParent($class, $root) !== null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param class-string $class | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @return class-string|null | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 1032 |  |     public function findParent(string $class, bool $root = true): ?string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         /** @var \ReflectionClass[] $parents */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 1032 |  |         $parents = $this->findParents($class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 1032 |  |         $parents = $root ? \array_reverse($parents) : $parents; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 1032 |  |         return isset($parents[0]) ? $parents[0]->getName() : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 46 | 688 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     public function findParents(string $class): array | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         $parents = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         /** @var class-string[] $classParents */ | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 688 |  |         $classParents = \class_parents($class); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         foreach ($classParents as $parent) { | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |             try { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 688 |  |                 $class = new \ReflectionClass($parent); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 688 |  |             } catch (\ReflectionException) { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 688 |  |                 continue; | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |             $ann = $this->reader->firstClassMetadata($class, Entity::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 1032 |  |             if ($ann !== null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |                 $parents[] = $class; | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 64 | 1032 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 66 | 1032 |  |         return $parents; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 996 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 9 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 1032 |  |     public function tableName(string $role, int $namingStrategy = Entities::TABLE_NAMING_PLURAL): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         return match ($namingStrategy) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             Entities::TABLE_NAMING_PLURAL => $this->inflector->pluralize($this->inflector->tableize($role)), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             Entities::TABLE_NAMING_SINGULAR => $this->inflector->singularize($this->inflector->tableize($role)), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             default => $this->inflector->tableize($role), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |  |