|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of UnderQuery package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2016 Beniamin Jonatan Šimko |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Phuria\UnderQuery\TableFactory; |
|
13
|
|
|
|
|
14
|
|
|
use Phuria\UnderQuery\QueryBuilder\BuilderInterface; |
|
15
|
|
|
use Phuria\UnderQuery\Table\SubQueryTable; |
|
16
|
|
|
use Phuria\UnderQuery\Table\DefaultTable; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @author Beniamin Jonatan Šimko <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class TableFactory implements TableFactoryInterface |
|
22
|
|
|
{ |
|
23
|
|
|
const TYPE_CLOSURE = 1; |
|
24
|
|
|
const TYPE_CLASS_NAME = 2; |
|
25
|
|
|
const TYPE_TABLE_NAME = 3; |
|
26
|
|
|
const TYPE_SUB_QUERY = 4; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param mixed $stuff |
|
30
|
|
|
* |
|
31
|
|
|
* @return int |
|
32
|
|
|
*/ |
|
33
|
4 |
|
public function recognizeType($stuff) |
|
34
|
|
|
{ |
|
35
|
4 |
|
if ($stuff instanceof \Closure) { |
|
36
|
2 |
|
return static::TYPE_CLOSURE; |
|
37
|
4 |
|
} else if ($stuff instanceof BuilderInterface) { |
|
38
|
2 |
|
return static::TYPE_SUB_QUERY; |
|
39
|
3 |
|
} else if (false !== strpos($stuff, '\\')) { |
|
40
|
2 |
|
return static::TYPE_CLASS_NAME; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
2 |
|
return static::TYPE_TABLE_NAME; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @inheritdoc |
|
48
|
|
|
*/ |
|
49
|
3 |
|
public function createNewTable($table, BuilderInterface $qb) |
|
50
|
|
|
{ |
|
51
|
3 |
|
$tableType = $this->recognizeType($table); |
|
52
|
|
|
|
|
53
|
|
|
switch ($tableType) { |
|
54
|
3 |
|
case static::TYPE_CLOSURE: |
|
55
|
1 |
|
$tableClass = $this->extractClassName($table); |
|
56
|
1 |
|
return new $tableClass($qb); |
|
57
|
3 |
|
case static::TYPE_CLASS_NAME: |
|
58
|
1 |
|
return new $table($qb); |
|
59
|
2 |
|
case static::TYPE_SUB_QUERY: |
|
60
|
1 |
|
return new SubQueryTable($table, $qb); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
$tableObject = new DefaultTable($qb); |
|
64
|
1 |
|
$tableObject->setTableName($table); |
|
65
|
|
|
|
|
66
|
1 |
|
return $tableObject; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param \Closure $closure |
|
71
|
|
|
* |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
2 |
|
public function extractClassName(\Closure $closure) |
|
75
|
|
|
{ |
|
76
|
2 |
|
$ref = new \ReflectionFunction($closure); |
|
77
|
2 |
|
$firstParameter = $ref->getParameters()[0]; |
|
78
|
|
|
|
|
79
|
2 |
|
return $firstParameter->getClass()->getName(); |
|
80
|
|
|
} |
|
81
|
|
|
} |