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\Misc\Template; |
18
|
|
|
use Phossa2\Query\Interfaces\Clause\OrderInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* OrderTrait |
22
|
|
|
* |
23
|
|
|
* Implementation of OrderInterface |
24
|
|
|
* |
25
|
|
|
* @package Phossa2\Query |
26
|
|
|
* @author Hong Zhang <[email protected]> |
27
|
|
|
* @see OrderInterface |
28
|
|
|
* @version 2.0.0 |
29
|
|
|
* @since 2.0.0 added |
30
|
|
|
*/ |
31
|
|
View Code Duplication |
trait OrderTrait |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
use AbstractTrait; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritDoc} |
37
|
|
|
*/ |
38
|
|
|
public function order($col) |
39
|
|
|
{ |
40
|
|
|
if (func_num_args() > 1) { |
41
|
|
|
$col = func_get_args(); |
42
|
|
|
} |
43
|
|
|
return $this->realOrder($col, 'ASC'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritDoc} |
48
|
|
|
*/ |
49
|
|
|
public function orderDesc($col) |
50
|
|
|
{ |
51
|
|
|
if (func_num_args() > 1) { |
52
|
|
|
$col = func_get_args(); |
53
|
|
|
} |
54
|
|
|
return $this->realOrder($col, 'DESC'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritDoc} |
59
|
|
|
*/ |
60
|
|
|
public function orderTpl(/*# string */ $template, $col, array $params = []) |
61
|
|
|
{ |
62
|
|
|
$template = $this->positionedParam($template, $params); |
63
|
|
|
return $this->realOrder(new Template($template, $col), '', true); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritDoc} |
68
|
|
|
*/ |
69
|
|
|
public function orderRaw(/*# string */ $rawString, array $params = []) |
70
|
|
|
{ |
71
|
|
|
$rawString = $this->positionedParam($rawString, $params); |
72
|
|
|
return $this->realOrder($rawString, '', true); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Real orderby |
77
|
|
|
* |
78
|
|
|
* @param string|string[]|Template $col |
79
|
|
|
* @param string $suffix 'ASC' or 'DESC' |
80
|
|
|
* @param bool $rawMode |
81
|
|
|
* @return $this |
82
|
|
|
* @access protected |
83
|
|
|
*/ |
84
|
|
|
protected function realOrder( |
85
|
|
|
$col, |
86
|
|
|
/*# sting */ $suffix = 'ASC', |
87
|
|
|
/*# bool */ $rawMode = false |
88
|
|
|
) { |
89
|
|
|
if (is_array($col)) { |
90
|
|
|
$this->multipleOrder($col, $suffix); |
91
|
|
|
} else { |
92
|
|
|
$clause = &$this->getClause('ORDER BY'); |
93
|
|
|
$part = [$col, $this->isRaw($col, $rawMode)]; |
94
|
|
|
if (!empty($suffix)) { |
95
|
|
|
$part[] = $suffix; |
96
|
|
|
} |
97
|
|
|
$clause[] = $part; |
98
|
|
|
} |
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Multitple orderbys |
104
|
|
|
* |
105
|
|
|
* @param array $cols |
106
|
|
|
* @param string $suffix 'ASC' or 'DESC' |
107
|
|
|
* @access protected |
108
|
|
|
*/ |
109
|
|
|
protected function multipleOrder(array $cols, /*# sting */ $suffix) |
110
|
|
|
{ |
111
|
|
|
foreach ($cols as $col) { |
112
|
|
|
$this->realOrder($col, $suffix); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Build ORDER BY |
118
|
|
|
* |
119
|
|
|
* @param string $prefix |
120
|
|
|
* @param array $settings |
121
|
|
|
* @return string |
122
|
|
|
* @access protected |
123
|
|
|
*/ |
124
|
|
|
protected function buildOrder( |
125
|
|
|
/*# string */ $prefix, |
126
|
|
|
array $settings |
127
|
|
|
)/*# : string */ { |
128
|
|
|
return $this->buildClause('ORDER BY', $prefix, $settings); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
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.