| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * PHP version 7.1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * This source file is subject to the license that is bundled with this package in the file LICENSE. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class plClassGraphElements | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     /** @var plHasDotRepresentation[] */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     private $dotElements = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     /** @var bool[] */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     private $associations = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** @var bool */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     private $createAssociations; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** @var plNodeLabelBuilder */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     private $labelBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** @var plHasNodeIdentifier[] */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     private $structure; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public function __construct(bool $createAssociations, plNodeLabelBuilder $labelBuilder) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $this->createAssociations = $createAssociations; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $this->labelBuilder = $labelBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param plHasNodeIdentifier[] $structure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @return plHasDotRepresentation[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     public function extractFrom(plPhpClass $class, array $structure): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $this->dotElements = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->associations = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->structure = $structure; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         if ($this->createAssociations) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             $this->addElementsForAttributes($class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             $this->addElementsForParameters($class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $this->dotElements[] = new plNode($class, $this->labelBuilder->labelForClass($class)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         if ($class->hasParent()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             $this->dotElements[] = plEdge::inheritance($class->extends, $class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         foreach ($class->implements as $interface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             $this->dotElements[] = plEdge::implementation($interface, $class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         return $this->dotElements; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     /** @return plHasDotRepresentation[] */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     private function addElementsForAttributes(plPhpClass $class): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         /** @var plPhpAttribute $attribute */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         foreach ($class->attributes as $attribute) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             $this->addAssociationForVariable($class, $attribute); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 68 |  |  |     /** @return plHasDotRepresentation[] */ | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |     private function addElementsForParameters(plPhpClass $class): void | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         if (!$class->hasConstructor()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |         foreach ($class->constructorParameters() as $parameter) { | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |             $this->addAssociationForVariable($class, $parameter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     private function addAssociationForVariable(plPhpClass $class, plPhpVariable $attribute): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         if ($this->needAssociation($attribute)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             $this->dotElements[] = plEdge::association($this->structure[(string)$attribute->type], $class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             $this->associations[strtolower($attribute->type)] = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     private function needAssociation(plPhpVariable $attribute): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         return $attribute->hasType() && !$attribute->isBuiltIn() && !$this->isAssociationResolved($attribute->type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     private function isAssociationResolved(string $type): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         return array_key_exists(strtolower($type), $this->associations); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 96 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |  |