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
|
14 |
|
public function getModelClassName($className, $context) |
|
|
|
|
26
|
|
|
{ |
27
|
14 |
|
return $className; |
28
|
|
|
} |
29
|
|
|
|
30
|
4 |
|
private function getClassFileDetails($className) |
31
|
|
|
{ |
32
|
4 |
|
$arrayed = explode('\\', $className); |
33
|
4 |
|
$class = array_pop($arrayed); |
34
|
4 |
|
if($arrayed[0] == '') { |
35
|
4 |
|
array_shift($arrayed); |
36
|
|
|
} |
37
|
4 |
|
return ['class' => $class, 'namespace' => implode('\\', $arrayed)]; |
38
|
|
|
} |
39
|
|
|
|
40
|
4 |
|
public function getJunctionClassName($classA, $classB) |
41
|
|
|
{ |
42
|
4 |
|
$classA = $this->getClassFileDetails($classA); |
43
|
4 |
|
$classB = $this->getClassFileDetails($classB); |
44
|
4 |
|
if($classA['namespace'] != $classB['namespace']) { |
45
|
|
|
throw new NibiiException( |
46
|
|
|
"Cannot automatically join two classes of different " |
47
|
|
|
. "namespaces. Please provide a model joiner or " |
48
|
|
|
. "explicitly specify your joint model." |
49
|
|
|
); |
50
|
|
|
} |
51
|
4 |
|
$classes = [$classA['class'], $classB['class']]; |
52
|
4 |
|
sort($classes); |
53
|
4 |
|
return "{$classA['namespace']}\\" . implode('', $classes); |
54
|
|
|
} |
55
|
|
|
|
56
|
36 |
|
public function getTableName($instance) |
57
|
|
|
{ |
58
|
36 |
|
$class = new \ReflectionClass($instance); |
59
|
36 |
|
$nameParts = explode("\\", $class->getName()); |
60
|
36 |
|
return \ntentan\utils\Text::deCamelize(end($nameParts)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public static function getDriverAdapterClassName() |
64
|
|
|
{ |
65
|
|
|
$driver = Config::get('ntentan:db.driver', false); |
66
|
|
|
if($driver) { |
67
|
|
|
return __NAMESPACE__ . '\adapters\\' . Text::ucamelize(Config::get('ntentan:db.driver')) . 'Adapter'; |
68
|
|
|
} |
69
|
|
|
throw new NibiiException("Please specify a driver"); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|