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\OrderByInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* OrderByTrait |
21
|
|
|
* |
22
|
|
|
* Implementation of OrderByInterface |
23
|
|
|
* |
24
|
|
|
* @package Phossa2\Query |
25
|
|
|
* @author Hong Zhang <[email protected]> |
26
|
|
|
* @see OrderByInterface |
27
|
|
|
* @version 2.0.0 |
28
|
|
|
* @since 2.0.0 added |
29
|
|
|
*/ |
30
|
|
|
trait OrderByTrait |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* {@inheritDoc} |
34
|
|
|
*/ |
35
|
|
|
public function orderBy($col) |
36
|
|
|
{ |
37
|
|
|
return $this->realOrderBy($col, 'ASC'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritDoc} |
42
|
|
|
*/ |
43
|
|
|
public function orderByDesc($col) |
44
|
|
|
{ |
45
|
|
|
return $this->realOrderBy($col, 'DESC'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritDoc} |
50
|
|
|
*/ |
51
|
|
|
public function orderByTpl(/*# string */ $template, $col) |
52
|
|
|
{ |
53
|
|
|
return $this->realOrderBy($this->clauseTpl($template, $col), '', true); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritDoc} |
58
|
|
|
*/ |
59
|
|
|
public function orderByRaw(/*# string */ $rawString) |
60
|
|
|
{ |
61
|
|
|
return $this->realOrderBy($rawString, '', true); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string|string[] $col |
66
|
|
|
* @param string $suffix 'ASC' or 'DESC' |
67
|
|
|
* @param bool $rawMode |
68
|
|
|
* @return $this |
69
|
|
|
* @access protected |
70
|
|
|
*/ |
71
|
|
View Code Duplication |
protected function realOrderBy( |
|
|
|
|
72
|
|
|
$col, |
73
|
|
|
/*# sting */ $suffix = 'ASC', |
74
|
|
|
/*# bool */ $rawMode = false |
75
|
|
|
) { |
76
|
|
|
if (is_array($col)) { |
77
|
|
|
$this->multipleOrderBy($col); |
|
|
|
|
78
|
|
|
} else { |
79
|
|
|
$clause = &$this->getClause('ORDER BY'); |
80
|
|
|
$clause[] = [$col, $suffix, $this->isRaw($col, $rawMode)]; |
81
|
|
|
} |
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Build ORDER BY |
87
|
|
|
* |
88
|
|
|
* @param array $settings |
89
|
|
|
* @return string |
90
|
|
|
* @access protected |
91
|
|
|
*/ |
92
|
|
|
protected function buildOrderby(array $settings)/*# : string */ |
93
|
|
|
{ |
94
|
|
|
$result = []; |
95
|
|
|
$clause = &$this->getClause('ORDER BY'); |
96
|
|
|
foreach ($clause as $ord) { |
97
|
|
|
$item = $this->quoteItem($ord[0], $ord[2]); |
98
|
|
|
$ordr = $ord[1] ? (' ' . $ord[1]) : ''; |
99
|
|
|
$result[] = $item . $ordr; |
100
|
|
|
} |
101
|
|
|
return $this->joinClause('ORDRE BY', ',', $result, $settings); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
abstract protected function isRaw($str, /*# bool */ $rawMode)/*# : bool */; |
105
|
|
|
abstract protected function clauseTpl(/*# string */ $template, $col)/*# : string */; |
106
|
|
|
abstract protected function &getClause(/*# string */ $clauseName)/*# : array */; |
107
|
|
|
abstract protected function quoteItem($item, /*# bool */ $rawMode = false)/*# : string */; |
108
|
|
|
abstract protected function joinClause( |
109
|
|
|
/*# : string */ $prefix, |
110
|
|
|
/*# : string */ $seperator, |
111
|
|
|
array $clause, |
112
|
|
|
array $settings |
113
|
|
|
)/*# : string */; |
114
|
|
|
} |
115
|
|
|
|
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.