| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * To change this license header, choose License Headers in Project Properties. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * To change this template file, choose Tools | Templates | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * and open the template in the editor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace ntentan\nibii; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use ntentan\nibii\interfaces\ClassResolverInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use ntentan\nibii\interfaces\ModelJoinerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use ntentan\nibii\interfaces\TableNameResolverInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use ntentan\config\Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use ntentan\utils\Text; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * Description of DefaultClassResolver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @author ekow | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | class ClassNameResolver implements ClassResolverInterface, ModelJoinerInterface, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     TableNameResolverInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public function getModelClassName($default, $context) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         if(self::$classResolver !== null && $model[0] !== "\\") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             $resolver = self::$classResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             $className = $resolver($model, $context); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             $className = $model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         return $className; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     private function getClassFileDetails($className) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $arrayed = explode('\\', $className); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $class = array_pop($arrayed); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         if($arrayed[0] == '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             array_shift($arrayed); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         return ['class' => $class, 'namespace' => implode('\\', $arrayed)]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     public function getJunctionClassName($classA, $classB) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $classA = $this->getClassFileDetails($classA); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $classB = $this->getClassFileDetails($classB); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         if($classA['namespace'] != $classB['namespace']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             throw new NibiiException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 "Cannot automatically join two classes of different " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                     . "namespaces. Please provide a model joiner or " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                     . "explicitly specify your joint model." | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $classes = [$classA['class'], $classB['class']]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         sort($classes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         return "{$classA['namespace']}\\" . implode('', $classes);         | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     public function getTableName($instance) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $class = new \ReflectionClass($instance); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $nameParts = explode("\\", $class->getName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         return \ntentan\utils\Text::deCamelize(end($nameParts));            | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 68 |  |  |      | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |     public static function getDriverAdapterClassName() | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |         return __NAMESPACE__ . '\adapters\\' . Text::ucamelize(Config::get('ntentan:db.driver')) . 'Adapter'; | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 73 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |  |