|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Phossa Project |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5.4 |
|
6
|
|
|
* |
|
7
|
|
|
* @category Library |
|
8
|
|
|
* @package Phossa2\Query |
|
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
|
10
|
|
|
* @license http://mit-license.org/ MIT License |
|
11
|
|
|
* @link http://www.phossa.com/ |
|
12
|
|
|
*/ |
|
13
|
|
|
/*# declare(strict_types=1); */ |
|
14
|
|
|
|
|
15
|
|
|
namespace Phossa2\Query\Traits\Clause; |
|
16
|
|
|
|
|
17
|
|
|
use Phossa2\Query\Interfaces\Clause\FromInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* FromTrait |
|
21
|
|
|
* |
|
22
|
|
|
* Implementation of FromInterface |
|
23
|
|
|
* |
|
24
|
|
|
* @package Phossa2\Query |
|
25
|
|
|
* @author Hong Zhang <[email protected]> |
|
26
|
|
|
* @see FromInterface |
|
27
|
|
|
* @version 2.0.0 |
|
28
|
|
|
* @since 2.0.0 added |
|
29
|
|
|
*/ |
|
30
|
|
|
trait FromTrait |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* {@inheritDoc} |
|
34
|
|
|
*/ |
|
35
|
|
|
public function from($table, /*# string */ $alias = '') |
|
36
|
|
|
{ |
|
37
|
|
|
if (is_array($table)) { |
|
38
|
|
|
$this->multipleFrom($table); |
|
39
|
|
|
} else { |
|
40
|
|
|
$clause = &$this->getClause('FROM'); |
|
41
|
|
|
if (empty($alias)) { |
|
42
|
|
|
$clause[] = $table; |
|
43
|
|
|
} else { |
|
44
|
|
|
$clause[(string) $alias] = $table; |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
return $this; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritDoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
public function table($table, /*# string */ $alias = '') |
|
54
|
|
|
{ |
|
55
|
|
|
$clause = &$this->getClause('FROM'); |
|
56
|
|
|
$clause = []; |
|
57
|
|
|
return $this->from($table, $alias); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* from multiple tables |
|
62
|
|
|
* |
|
63
|
|
|
* @param array $tables |
|
64
|
|
|
* @access protected |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function multipleFrom(array $tables) |
|
67
|
|
|
{ |
|
68
|
|
|
foreach ($tables as $key => $val) { |
|
69
|
|
|
if (is_int($key)) { |
|
70
|
|
|
$this->from($val); |
|
71
|
|
|
} else { |
|
72
|
|
|
$this->from($key, $val); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Build FROM |
|
79
|
|
|
* |
|
80
|
|
|
* @return array |
|
81
|
|
|
* @access protected |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function buildFrom(array $settings)/*# : string */ |
|
84
|
|
|
{ |
|
85
|
|
|
$result = []; |
|
86
|
|
|
$clause = &$this->getClause('FROM'); |
|
87
|
|
View Code Duplication |
foreach ($clause as $as => $tbl) { |
|
|
|
|
|
|
88
|
|
|
$alias = $this->quoteAlias($as); |
|
89
|
|
|
$table = $this->quoteItem($tbl); |
|
90
|
|
|
$result[] = $table . $alias; |
|
91
|
|
|
} |
|
92
|
|
|
return $this->joinClause('FROM', ',', $result, $settings); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
abstract protected function quoteAlias($alias)/*# : string */; |
|
96
|
|
|
abstract protected function quoteItem($item, /*# bool */ $rawMode = false)/*# : string */; |
|
97
|
|
|
abstract protected function &getClause(/*# string */ $clauseName)/*# : array */; |
|
98
|
|
|
abstract protected function joinClause( |
|
99
|
|
|
/*# : string */ $prefix, |
|
100
|
|
|
/*# : string */ $seperator, |
|
101
|
|
|
array $clause, |
|
102
|
|
|
array $settings |
|
103
|
|
|
)/*# : string */; |
|
104
|
|
|
} |
|
105
|
|
|
|
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.