1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BfwSql\Queries\Parts; |
4
|
|
|
|
5
|
|
|
use \BfwSql\Queries\AbstractQuery; |
6
|
|
|
|
7
|
|
|
class ColumnList extends AbstractList |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var \BfwSql\Queries\Parts\Table $table The table where are columns |
11
|
|
|
*/ |
12
|
|
|
protected $table; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* {@inheritdoc} |
16
|
|
|
*/ |
17
|
|
|
protected $separator = ','; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
* |
22
|
|
|
* @param \BfwSql\Queries\AbstractQuery $querySystem |
23
|
|
|
* @param \BfwSql\Queries\Parts\Table $table The table where are column |
24
|
|
|
*/ |
25
|
|
|
public function __construct(AbstractQuery $querySystem, Table $table) |
26
|
|
|
{ |
27
|
|
|
parent::__construct($querySystem); |
28
|
|
|
$this->table = $table; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Getter accessor to property table |
33
|
|
|
* |
34
|
|
|
* @return \BfwSql\Queries\Parts\Table |
35
|
|
|
*/ |
36
|
|
|
public function getTable(): Table |
37
|
|
|
{ |
38
|
|
|
return $this->table; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Magic method __invoke, used when the user call object like a function |
43
|
|
|
* @link http://php.net/manual/en/language.oop5.magic.php#object.invoke |
44
|
|
|
* |
45
|
|
|
* @param array $columns The list of columns to declare |
46
|
|
|
* The key into the array is the shortcut of the column. |
47
|
|
|
* The value is the column name. |
48
|
|
|
* If no key is defined (so integer), the column will not have shortcut. |
49
|
|
|
* |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
|
public function __invoke(array $columns) |
53
|
|
|
{ |
54
|
|
|
foreach ($columns as $shortcut => $name) { |
55
|
|
|
if (is_int($shortcut)) { |
56
|
|
|
$column = new Column($this->table, $name); |
57
|
|
|
} else { |
58
|
|
|
$column = new Column($this->table, $name, $shortcut); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->list[] = $column; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
public function generate(): string |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$sqlPart = ''; |
71
|
|
|
|
72
|
|
|
foreach ($this->list as $index => $column) { |
73
|
|
|
if ($index > 0) { |
74
|
|
|
$sqlPart .= $this->separator; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$sqlPart .= $column->generate(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $sqlPart; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.