|
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
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Description of DefaultClassResolver |
|
17
|
|
|
* |
|
18
|
|
|
* @author ekow |
|
19
|
|
|
*/ |
|
20
|
|
|
class ClassNameResolver implements ClassResolverInterface, ModelJoinerInterface, |
|
|
|
|
|
|
21
|
|
|
TableNameResolverInterface |
|
22
|
|
|
{ |
|
23
|
|
|
public function getModelClassName($default, $context) |
|
|
|
|
|
|
24
|
|
|
{ |
|
25
|
|
|
if(self::$classResolver !== null && $model[0] !== "\\") { |
|
26
|
|
|
$resolver = self::$classResolver; |
|
27
|
|
|
$className = $resolver($model, $context); |
|
|
|
|
|
|
28
|
|
|
} else { |
|
29
|
|
|
$className = $model; |
|
30
|
|
|
} |
|
31
|
|
|
return $className; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
private function getClassFileDetails($className) |
|
35
|
|
|
{ |
|
36
|
|
|
$arrayed = explode('\\', $className); |
|
37
|
|
|
$class = array_pop($arrayed); |
|
38
|
|
|
if($arrayed[0] == '') { |
|
39
|
|
|
array_shift($arrayed); |
|
40
|
|
|
} |
|
41
|
|
|
return ['class' => $class, 'namespace' => implode('\\', $arrayed)]; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function getJunctionClassName($classA, $classB) |
|
45
|
|
|
{ |
|
46
|
|
|
$classA = $this->getClassFileDetails($classA); |
|
47
|
|
|
$classB = $this->getClassFileDetails($classB); |
|
48
|
|
|
if($classA['namespace'] != $classB['namespace']) { |
|
49
|
|
|
throw new NibiiException( |
|
50
|
|
|
"Cannot automatically join two classes of different " |
|
51
|
|
|
. "namespaces. Please provide a model joiner or " |
|
52
|
|
|
. "explicitly specify your joint model." |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
$classes = [$classA['class'], $classB['class']]; |
|
56
|
|
|
sort($classes); |
|
57
|
|
|
return "{$classA['namespace']}\\" . implode('', $classes); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function getTableName($instance) |
|
61
|
|
|
{ |
|
62
|
|
|
$class = new \ReflectionClass($instance); |
|
63
|
|
|
$nameParts = explode("\\", $class->getName()); |
|
64
|
|
|
return \ntentan\utils\Text::deCamelize(end($nameParts)); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|