1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Quantum PHP Framework |
5
|
|
|
* |
6
|
|
|
* An open source software development framework for PHP |
7
|
|
|
* |
8
|
|
|
* @package Quantum |
9
|
|
|
* @author Arman Ag. <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
11
|
|
|
* @link http://quantum.softberg.org/ |
12
|
|
|
* @since 2.8.0 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Database\Sleekdb\Statements; |
16
|
|
|
|
17
|
|
|
use Quantum\Libraries\Database\DbalInterface; |
18
|
|
|
use RecursiveIteratorIterator; |
19
|
|
|
use RecursiveArrayIterator; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Trait Modifier |
23
|
|
|
* @package Quantum\Libraries\Database\Idiorm\Statements |
24
|
|
|
*/ |
25
|
|
|
trait Reducer |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @inheritDoc |
30
|
|
|
*/ |
31
|
|
|
public function select(...$columns): DbalInterface |
32
|
|
|
{ |
33
|
|
|
array_walk($columns, function (&$column) { |
34
|
|
|
if (is_array($column)) { |
35
|
|
|
$column = array_flip($column); |
36
|
|
|
} |
37
|
|
|
}); |
38
|
|
|
|
39
|
|
|
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($columns)); |
40
|
|
|
$columns = iterator_to_array($iterator, true); |
41
|
|
|
|
42
|
|
|
$this->selected = $this->selectPatch($columns); |
|
|
|
|
43
|
|
|
|
44
|
|
|
return $this; |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @inheritDoc |
49
|
|
|
*/ |
50
|
|
|
public function groupBy(string $column): DbalInterface |
51
|
|
|
{ |
52
|
|
|
$this->grouped[] = $column; |
|
|
|
|
53
|
|
|
return $this; |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @inheritDoc |
58
|
|
|
*/ |
59
|
|
|
public function orderBy(string $column, string $direction): DbalInterface |
60
|
|
|
{ |
61
|
|
|
$this->ordered[$column] = $direction; |
|
|
|
|
62
|
|
|
return $this; |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritDoc |
67
|
|
|
*/ |
68
|
|
|
public function offset(int $offset): DbalInterface |
69
|
|
|
{ |
70
|
|
|
$this->offset = $offset; |
|
|
|
|
71
|
|
|
return $this; |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @inheritDoc |
76
|
|
|
*/ |
77
|
|
|
public function limit(int $limit): DbalInterface |
78
|
|
|
{ |
79
|
|
|
$this->limit = $limit; |
|
|
|
|
80
|
|
|
return $this; |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param array $columns |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
private function selectPatch(array $columns): array |
88
|
|
|
{ |
89
|
|
|
foreach ($columns as &$column) { |
90
|
|
|
$exploded = explode('.', $column); |
91
|
|
|
|
92
|
|
|
if (count($exploded) == 1) { |
93
|
|
|
$column = $exploded[0]; |
94
|
|
|
} else { |
95
|
|
|
if ($exploded[0] == $this->getTable()) { |
|
|
|
|
96
|
|
|
$column = $exploded[1]; |
97
|
|
|
} else { |
98
|
|
|
$column = $exploded[0] . '.0.' . $exploded[1]; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $columns; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
} |