1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
4
|
|
|
* Date: 6/3/14 |
5
|
|
|
* Time: 12:07 AM. |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace NilPortugues\Sql\QueryBuilder\Syntax; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class SyntaxFactory. |
15
|
|
|
*/ |
16
|
|
|
final class SyntaxFactory |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Creates a collection of Column objects. |
20
|
|
|
* |
21
|
|
|
* @param array $arguments |
22
|
|
|
* @param Table|null $table |
23
|
|
|
* |
24
|
|
|
* @return array |
25
|
|
|
*/ |
26
|
|
|
public static function createColumns(array &$arguments, $table = null) |
27
|
|
|
{ |
28
|
|
|
$createdColumns = []; |
29
|
|
|
|
30
|
|
|
foreach ($arguments as $index => $column) { |
31
|
|
|
if (!is_object($column)) { |
32
|
|
|
$newColumn = array($column); |
33
|
|
|
$column = self::createColumn($newColumn, $table); |
34
|
|
|
if (!is_numeric($index)) { |
35
|
|
|
$column->setAlias($index); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$createdColumns[] = $column; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return \array_filter($createdColumns); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Creates a Column object. |
47
|
|
|
* |
48
|
|
|
* @param array $argument |
49
|
|
|
* @param null|Table $table |
50
|
|
|
* |
51
|
|
|
* @return Column |
52
|
|
|
*/ |
53
|
|
|
public static function createColumn(array &$argument, $table = null) |
54
|
|
|
{ |
55
|
|
|
$columnName = \array_values($argument); |
56
|
|
|
$columnName = $columnName[0]; |
57
|
|
|
|
58
|
|
|
$columnAlias = \array_keys($argument); |
59
|
|
|
$columnAlias = $columnAlias[0]; |
60
|
|
|
|
61
|
|
|
if (\is_numeric($columnAlias) || \strpos($columnName, '*') !== false) { |
62
|
|
|
$columnAlias = null; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return new Column($columnName, (string) $table, $columnAlias); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Creates a Table object. |
70
|
|
|
* |
71
|
|
|
* @param string[] $table |
72
|
|
|
* |
73
|
|
|
* @return Table |
74
|
|
|
*/ |
75
|
|
|
public static function createTable($table) |
76
|
|
|
{ |
77
|
|
|
if (\is_array($table)) { |
78
|
|
|
$tableName = \current($table); |
79
|
|
|
$tableAlias = \key($table); |
80
|
|
|
} else { |
81
|
|
|
$tableName = $table; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$newTable = new Table($tableName); |
85
|
|
|
|
86
|
|
|
if (isset($tableAlias) && !is_numeric($tableAlias)) { |
87
|
|
|
$newTable->setAlias($tableAlias); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $newTable; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|