Completed
Push — master ( 7f10bf...447c8c )
by James Ekow Abaka
05:19
created

Resolver::getModelClassName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
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\ModelClassResolverInterface;
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 Resolver implements ModelClassResolverInterface, ModelJoinerInterface,
0 ignored issues
show
Coding Style introduced by
The first item in a multi-line implements list must be on the line following the implements keyword
Loading history...
Coding Style introduced by
Only one interface may be specified per line in a multi-line implements declaration
Loading history...
23
    TableNameResolverInterface
24
{
25
    public function getModelClassName($className, $context)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

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.

Loading history...
26
    {
27
        return $className;
28
    }
29
    
30
    private function getClassFileDetails($className)
31
    {
32
        $arrayed = explode('\\', $className);
33
        $class = array_pop($arrayed);
34
        if($arrayed[0] == '') {
35
            array_shift($arrayed);
36
        }
37
        return ['class' => $class, 'namespace' => implode('\\', $arrayed)];
38
    }
39
40
    public function getJunctionClassName($classA, $classB)
41
    {
42
        $classA = $this->getClassFileDetails($classA);
43
        $classB = $this->getClassFileDetails($classB);
44
        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
        $classes = [$classA['class'], $classB['class']];
52
        sort($classes);
53
        return "{$classA['namespace']}\\" . implode('', $classes);        
54
    }
55
56
    public function getTableName($instance)
57
    {
58
        $class = new \ReflectionClass($instance);
59
        $nameParts = explode("\\", $class->getName());
60
        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