1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ntentan\nibii; |
4
|
|
|
|
5
|
|
|
use ntentan\nibii\interfaces\ModelClassResolverInterface; |
6
|
|
|
use ntentan\nibii\interfaces\ModelJoinerInterface; |
7
|
|
|
use ntentan\nibii\interfaces\TableNameResolverInterface; |
8
|
|
|
use ntentan\config\Config; |
9
|
|
|
use ntentan\utils\Text; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Description of DefaultClassResolver |
13
|
|
|
* |
14
|
|
|
* @author ekow |
15
|
|
|
*/ |
16
|
|
|
class Resolver implements ModelClassResolverInterface, ModelJoinerInterface, TableNameResolverInterface |
17
|
|
|
{ |
18
|
14 |
|
public function getModelClassName($className, $context) { |
|
|
|
|
19
|
14 |
|
return $className; |
20
|
|
|
} |
21
|
|
|
|
22
|
4 |
|
private function getClassFileDetails($className) { |
23
|
4 |
|
$arrayed = explode('\\', $className); |
24
|
4 |
|
$class = array_pop($arrayed); |
25
|
4 |
|
if ($arrayed[0] == '') { |
26
|
4 |
|
array_shift($arrayed); |
27
|
|
|
} |
28
|
4 |
|
return ['class' => $class, 'namespace' => implode('\\', $arrayed)]; |
29
|
|
|
} |
30
|
|
|
|
31
|
4 |
|
public function getJunctionClassName($classA, $classB) { |
32
|
4 |
|
$classA = $this->getClassFileDetails($classA); |
33
|
4 |
|
$classB = $this->getClassFileDetails($classB); |
34
|
4 |
|
if ($classA['namespace'] != $classB['namespace']) { |
35
|
|
|
throw new NibiiException( |
36
|
|
|
"Cannot automatically join two classes of different " |
37
|
|
|
. "namespaces. Please provide a model joiner or " |
38
|
|
|
. "explicitly specify your joint model." |
39
|
|
|
); |
40
|
|
|
} |
41
|
4 |
|
$classes = [$classA['class'], $classB['class']]; |
42
|
4 |
|
sort($classes); |
43
|
4 |
|
return "{$classA['namespace']}\\" . implode('', $classes); |
44
|
|
|
} |
45
|
|
|
|
46
|
36 |
|
public function getTableName($instance) { |
47
|
36 |
|
$class = new \ReflectionClass($instance); |
48
|
36 |
|
$nameParts = explode("\\", $class->getName()); |
49
|
36 |
|
return \ntentan\utils\Text::deCamelize(end($nameParts)); |
50
|
|
|
} |
51
|
|
|
|
52
|
36 |
|
public static function getDriverAdapterClassName($driver = false) { |
53
|
36 |
|
if ($driver) { |
54
|
36 |
|
return __NAMESPACE__ . '\adapters\\' . Text::ucamelize($driver) . 'Adapter'; |
|
|
|
|
55
|
|
|
} |
56
|
|
|
throw new NibiiException("Please specify a driver"); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.