| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace EaselDrawing\Templates; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use EaselDrawing\ElementInterface; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 7 |  |  | class ElementFactory | 
            
                                                                        
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  |     private $builders = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |     public function register(string $type, string $builderClass) | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |         $type = $this->castType($type); | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |         $implements = class_implements($builderClass); | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |         if (! in_array(ElementBuilderInterface::class, $implements)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |             throw new \InvalidArgumentException( | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |                 "Cannot register the class $builderClass since it does not implements " . ElementBuilderInterface::class | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |         $this->builders[$type] = $builderClass; | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     public function builderClass(string $type): string | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |         $type = $this->castType($type); | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |         if (! isset($this->builders[$type])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |             throw new \InvalidArgumentException("There are no builder class for type $type"); | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         return $this->builders[$type]; | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     public function has(string $type): bool | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         $type = $this->castType($type); | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         return (isset($this->builders[$type])); | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     public function builder(string $type) : ElementBuilderInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         $builderClass = $this->builderClass($type); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         /* @var ElementBuilderInterface $builder */ | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         $builder = call_user_func("$builderClass::create"); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         return $builder; | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     public function element(string $type, $data, Template $template): ElementInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $builder = $this->builder($type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $builder->configure($data, $template); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 51 |  |  |         return $builder->build(); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     private function castType(string $type) : string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 56 |  |  |         return strtolower($type); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 58 |  |  | } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 59 |  |  |  |