|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Phuria SQL Builder 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\QueryBuilder; |
|
13
|
|
|
|
|
14
|
|
|
use Phuria\QueryBuilder\Table\AbstractTable; |
|
15
|
|
|
use Phuria\QueryBuilder\Table\SubQueryTable; |
|
16
|
|
|
use Phuria\QueryBuilder\Table\UnknownTable; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @author Beniamin Jonatan Šimko <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class TableFactory |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var TableRegistry $registry |
|
25
|
|
|
*/ |
|
26
|
|
|
private $registry; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var TableRecognizer $tableRecognizer |
|
30
|
|
|
*/ |
|
31
|
|
|
private $tableRecognizer; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param TableRegistry $registry |
|
35
|
|
|
*/ |
|
36
|
26 |
|
public function __construct(TableRegistry $registry = null) |
|
37
|
|
|
{ |
|
38
|
26 |
|
$this->registry = $registry ?: new TableRegistry(); |
|
39
|
26 |
|
$this->tableRecognizer = new TableRecognizer(); |
|
40
|
26 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param mixed $table |
|
44
|
|
|
* @param QueryBuilder $qb |
|
45
|
|
|
* |
|
46
|
|
|
* @return AbstractTable |
|
47
|
|
|
*/ |
|
48
|
24 |
|
public function createNewTable($table, QueryBuilder $qb) |
|
49
|
|
|
{ |
|
50
|
24 |
|
$tableType = $this->tableRecognizer->recognizeType($table); |
|
51
|
|
|
|
|
52
|
24 |
|
$tableClass = null; |
|
53
|
|
|
|
|
54
|
|
|
switch ($tableType) { |
|
55
|
24 |
|
case TableRecognizer::TYPE_CLOSURE: |
|
56
|
|
|
$tableClass = $this->tableRecognizer->extractClassName($table); |
|
57
|
|
|
break; |
|
58
|
24 |
|
case TableRecognizer::TYPE_CLASS_NAME: |
|
59
|
|
|
$tableClass = $table; |
|
60
|
|
|
break; |
|
61
|
24 |
|
case TableRecognizer::TYPE_TABLE_NAME: |
|
62
|
24 |
|
$tableClass = $this->registry->getTableClass($table); |
|
63
|
24 |
|
break; |
|
64
|
1 |
|
case TableRecognizer::TYPE_SUB_QUERY: |
|
65
|
1 |
|
return $this->createSubQueryTable($table, $qb); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
24 |
|
return $this->doCreate($table, $tableClass, $qb); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param string $requestedTable |
|
73
|
|
|
* @param string $tableClass |
|
74
|
|
|
* @param QueryBuilder $qb |
|
75
|
|
|
* |
|
76
|
|
|
* @return mixed |
|
77
|
|
|
*/ |
|
78
|
24 |
|
private function doCreate($requestedTable, $tableClass, QueryBuilder $qb) |
|
79
|
|
|
{ |
|
80
|
24 |
|
$tableObject = new $tableClass($qb); |
|
81
|
|
|
|
|
82
|
24 |
|
if ($tableObject instanceof UnknownTable) { |
|
83
|
9 |
|
$tableObject->setTableName($requestedTable); |
|
84
|
9 |
|
} |
|
85
|
|
|
|
|
86
|
24 |
|
return $tableObject; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param QueryBuilder $subQb |
|
91
|
|
|
* @param QueryBuilder $qb |
|
92
|
|
|
* |
|
93
|
|
|
* @return SubQueryTable |
|
94
|
|
|
*/ |
|
95
|
1 |
|
public function createSubQueryTable(QueryBuilder $subQb, QueryBuilder $qb) |
|
96
|
|
|
{ |
|
97
|
1 |
|
return new SubQueryTable($subQb, $qb); |
|
98
|
|
|
} |
|
99
|
|
|
} |