|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rougin\Windstorm\Doctrine; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Query\QueryBuilder; |
|
6
|
|
|
use Rougin\Windstorm\OrderInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Order Query |
|
10
|
|
|
* |
|
11
|
|
|
* @package Windstorm |
|
12
|
|
|
* @author Rougin Gutib <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
class Order implements OrderInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var \Doctrine\DBAL\Query\QueryBuilder |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $builder; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $key; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $order = 'ASC'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var \Rougin\Windstorm\Doctrine\Query |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $query; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $type = ''; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Initializes the query instance. |
|
43
|
|
|
* |
|
44
|
|
|
* @param \Rougin\Windstorm\Doctrine\Query $query |
|
45
|
|
|
* @param \Doctrine\DBAL\Query\QueryBuilder $builder |
|
46
|
|
|
* @param string $key |
|
47
|
|
|
* @param string|null $initial |
|
48
|
|
|
* @param string $type |
|
49
|
|
|
*/ |
|
50
|
24 |
|
public function __construct(Query $query, QueryBuilder $builder, $key, $initial, $type = '') |
|
51
|
|
|
{ |
|
52
|
24 |
|
if ($initial && strpos($key, '.') === false) |
|
53
|
16 |
|
{ |
|
54
|
3 |
|
$key = $initial . '.' . $key; |
|
55
|
2 |
|
} |
|
56
|
|
|
|
|
57
|
24 |
|
$this->builder = $builder; |
|
58
|
|
|
|
|
59
|
24 |
|
$this->key = $key; |
|
60
|
|
|
|
|
61
|
24 |
|
$this->query = $query; |
|
62
|
|
|
|
|
63
|
24 |
|
$this->type = $type; |
|
64
|
24 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Sets the order in ascending. |
|
68
|
|
|
* |
|
69
|
|
|
* @return \Rougin\Windstorm\QueryInterface |
|
70
|
|
|
*/ |
|
71
|
6 |
|
public function ascending() |
|
72
|
|
|
{ |
|
73
|
6 |
|
$this->order = 'ASC'; |
|
74
|
|
|
|
|
75
|
6 |
|
return $this->set(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Sets the order in descending. |
|
80
|
|
|
* |
|
81
|
|
|
* @return \Rougin\Windstorm\QueryInterface |
|
82
|
|
|
*/ |
|
83
|
9 |
|
public function descending() |
|
84
|
|
|
{ |
|
85
|
9 |
|
$this->order = 'DESC'; |
|
86
|
|
|
|
|
87
|
9 |
|
return $this->set(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Calls a method from a QueryInterface instance. |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $method |
|
94
|
|
|
* @param array $parameters |
|
95
|
|
|
* @return mixed |
|
96
|
|
|
*/ |
|
97
|
12 |
|
public function __call($method, $parameters) |
|
98
|
|
|
{ |
|
99
|
12 |
|
$instance = array($this->set(), (string) $method); |
|
100
|
|
|
|
|
101
|
12 |
|
return call_user_func_array($instance, $parameters); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Calls a __toString() from a QueryInterface instance. |
|
106
|
|
|
* |
|
107
|
|
|
* @return string |
|
108
|
|
|
*/ |
|
109
|
3 |
|
public function __toString() |
|
110
|
|
|
{ |
|
111
|
3 |
|
return $this->set()->__toString(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Sets the order type and calls a method from QueryInstance. |
|
116
|
|
|
* |
|
117
|
|
|
* @return \Rougin\Windstorm\QueryInterface |
|
118
|
|
|
*/ |
|
119
|
24 |
|
protected function set() |
|
120
|
|
|
{ |
|
121
|
24 |
|
$type = strtolower($this->type) . 'OrderBy'; |
|
122
|
|
|
|
|
123
|
24 |
|
$this->builder->$type($this->key, $this->order); |
|
124
|
|
|
|
|
125
|
24 |
|
return $this->query->builder($this->builder); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|