|
1
|
|
|
<?php |
|
2
|
|
|
namespace Darya\Storage\Query; |
|
3
|
|
|
|
|
4
|
|
|
use Darya\Storage\Query; |
|
5
|
|
|
use Darya\Storage\Queryable; |
|
6
|
|
|
use Darya\Storage\Result; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Darya's storage query builder. |
|
10
|
|
|
* |
|
11
|
|
|
* Forwards method calls to a storage query and executes it on the given |
|
12
|
|
|
* queryable storage interface once the query has been built. |
|
13
|
|
|
* |
|
14
|
|
|
* TODO: Implement event dispatcher awareness. |
|
15
|
|
|
* |
|
16
|
|
|
* @property-read Query $query |
|
17
|
|
|
* @property-read Queryable $storage |
|
18
|
|
|
* @property-read callable $callback |
|
19
|
|
|
* |
|
20
|
|
|
* @author Chris Andrew <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
class Builder |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* The query to execute. |
|
26
|
|
|
* |
|
27
|
|
|
* @var Query |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $query; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The storage interface to query. |
|
33
|
|
|
* |
|
34
|
|
|
* @var Queryable |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $storage; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* A callback that processes results. |
|
40
|
|
|
* |
|
41
|
|
|
* @var callback |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $callback; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Query methods that should trigger query execution. |
|
47
|
|
|
* |
|
48
|
|
|
* @var array |
|
49
|
|
|
*/ |
|
50
|
|
|
protected static $executors = array( |
|
51
|
|
|
'all', 'read', 'select', 'unique', 'distinct', 'delete' |
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Instantiate a new query builder for the given query and storage. |
|
56
|
|
|
* |
|
57
|
|
|
* @param Query $query |
|
58
|
|
|
* @param Queryable $storage |
|
59
|
|
|
*/ |
|
60
|
|
|
public function __construct(Query $query, Queryable $storage) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->query = $query; |
|
63
|
|
|
$this->storage = $storage; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Dynamically invoke methods to fluently build a query. |
|
68
|
|
|
* |
|
69
|
|
|
* If the method is an execute method, the query is executed and the result |
|
70
|
|
|
* returned. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $method |
|
73
|
|
|
* @param array $arguments |
|
74
|
|
|
* @return $this|Result |
|
75
|
|
|
*/ |
|
76
|
|
|
public function __call($method, $arguments) |
|
77
|
|
|
{ |
|
78
|
|
|
call_user_func_array(array($this->query, $method), $arguments); |
|
79
|
|
|
|
|
80
|
|
|
if (in_array($method, static::$executors)) { |
|
81
|
|
|
return $this->run(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $this; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Dynamically retrieve a property. |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $property |
|
91
|
|
|
* @return mixed |
|
92
|
|
|
*/ |
|
93
|
|
|
public function __get($property) |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->$property; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Set a callback to run on results before returning them from execute |
|
100
|
|
|
* methods. |
|
101
|
|
|
* |
|
102
|
|
|
* Callbacks should accept one parameter: the query result. |
|
103
|
|
|
* |
|
104
|
|
|
* @param callback $callback |
|
105
|
|
|
*/ |
|
106
|
|
|
public function callback($callback) |
|
107
|
|
|
{ |
|
108
|
|
|
$this->callback = $callback; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Run the query through the storage interface. |
|
113
|
|
|
* |
|
114
|
|
|
* Always returns a Result object, ignoring the callback. |
|
115
|
|
|
* |
|
116
|
|
|
* @return Result |
|
117
|
|
|
*/ |
|
118
|
|
|
public function raw() |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->storage->run($this->query); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Run the query through the storage interface. |
|
125
|
|
|
* |
|
126
|
|
|
* Returns the result of the callback, if one is set. |
|
127
|
|
|
* |
|
128
|
|
|
* @return mixed |
|
129
|
|
|
*/ |
|
130
|
|
|
public function run() |
|
131
|
|
|
{ |
|
132
|
|
|
$result = $this->storage->run($this->query); |
|
133
|
|
|
|
|
134
|
|
|
if (!is_callable($this->callback)) { |
|
135
|
|
|
return $result; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return call_user_func($this->callback, $result); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Alias for the run() method. |
|
143
|
|
|
* |
|
144
|
|
|
* @return mixed |
|
145
|
|
|
*/ |
|
146
|
|
|
public function cheers() |
|
147
|
|
|
{ |
|
148
|
|
|
return $this->run(); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|