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\SQLBuilder\TableFactory; |
13
|
|
|
|
14
|
|
|
use Phuria\SQLBuilder\QueryBuilder\AbstractBuilder; |
15
|
|
|
use Phuria\SQLBuilder\Table\SubQueryTable; |
16
|
|
|
use Phuria\SQLBuilder\Table\UnknownTable; |
17
|
|
|
use Phuria\SQLBuilder\TableRecognizer; |
18
|
|
|
use Phuria\SQLBuilder\TableRegistry; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Beniamin Jonatan Šimko <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class TableFactory implements TableFactoryInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var TableRegistry $registry |
27
|
|
|
*/ |
28
|
|
|
private $registry; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var TableRecognizer $tableRecognizer |
32
|
|
|
*/ |
33
|
|
|
private $tableRecognizer; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param TableRegistry $registry |
|
|
|
|
37
|
|
|
*/ |
38
|
30 |
|
public function __construct(TableRegistry $registry = null) |
39
|
|
|
{ |
40
|
30 |
|
$this->registry = $registry ?: new TableRegistry(); |
41
|
30 |
|
$this->tableRecognizer = new TableRecognizer(); |
42
|
30 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritdoc |
46
|
|
|
*/ |
47
|
28 |
|
public function createNewTable($table, AbstractBuilder $qb) |
48
|
|
|
{ |
49
|
28 |
|
$tableType = $this->tableRecognizer->recognizeType($table); |
50
|
|
|
|
51
|
28 |
|
$tableClass = null; |
52
|
|
|
|
53
|
|
|
switch ($tableType) { |
54
|
28 |
|
case TableRecognizer::TYPE_CLOSURE: |
55
|
|
|
$tableClass = $this->tableRecognizer->extractClassName($table); |
56
|
|
|
break; |
57
|
28 |
|
case TableRecognizer::TYPE_CLASS_NAME: |
58
|
|
|
$tableClass = $table; |
59
|
|
|
break; |
60
|
28 |
|
case TableRecognizer::TYPE_TABLE_NAME: |
61
|
28 |
|
$tableClass = $this->registry->getTableClass($table); |
62
|
28 |
|
break; |
63
|
1 |
|
case TableRecognizer::TYPE_SUB_QUERY: |
64
|
1 |
|
return $this->createSubQueryTable($table, $qb); |
65
|
|
|
} |
66
|
|
|
|
67
|
28 |
|
return $this->doCreate($table, $tableClass, $qb); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $requestedTable |
72
|
|
|
* @param string $tableClass |
73
|
|
|
* @param AbstractBuilder $qb |
74
|
|
|
* |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
28 |
|
private function doCreate($requestedTable, $tableClass, AbstractBuilder $qb) |
78
|
|
|
{ |
79
|
28 |
|
$tableObject = new $tableClass($qb); |
80
|
|
|
|
81
|
28 |
|
if ($tableObject instanceof UnknownTable) { |
82
|
27 |
|
$tableObject->setTableName($requestedTable); |
83
|
27 |
|
} |
84
|
|
|
|
85
|
28 |
|
return $tableObject; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param AbstractBuilder $subQb |
90
|
|
|
* @param AbstractBuilder $qb |
91
|
|
|
* |
92
|
|
|
* @return SubQueryTable |
93
|
|
|
*/ |
94
|
1 |
|
private function createSubQueryTable(AbstractBuilder $subQb, AbstractBuilder $qb) |
95
|
|
|
{ |
96
|
1 |
|
return new SubQueryTable($subQb, $qb); |
97
|
|
|
} |
98
|
|
|
} |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.