|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace JamesMoss\Flywheel; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Query |
|
7
|
|
|
* |
|
8
|
|
|
* Builds an executes a query whichs searches and sorts documents from a |
|
9
|
|
|
* repository. |
|
10
|
|
|
* |
|
11
|
|
|
* @todo turn the limit and order by arrays into value objects |
|
12
|
|
|
*/ |
|
13
|
|
|
class Query |
|
14
|
|
|
{ |
|
15
|
|
|
protected $repo; |
|
16
|
|
|
protected $predicate; |
|
17
|
|
|
protected $limit = array(); |
|
18
|
|
|
protected $orderBy = array(); |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Constructor |
|
22
|
|
|
* |
|
23
|
|
|
* @param Repository $repository The repo this query will run against. |
|
24
|
|
|
*/ |
|
25
|
4 |
|
public function __construct(Repository $repository) |
|
26
|
|
|
{ |
|
27
|
4 |
|
$this->repo = $repository; |
|
28
|
4 |
|
$this->predicate = new Predicate(); |
|
29
|
4 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Set a limit on the number of documents returned. An offset from 0 can |
|
33
|
|
|
* also be specified. |
|
34
|
|
|
* |
|
35
|
|
|
* @param int $count The number of documents to return. |
|
36
|
|
|
* @param int $offset The offset from which to return. |
|
37
|
|
|
* |
|
38
|
|
|
* @return Query The same instance of this class. |
|
39
|
|
|
*/ |
|
40
|
1 |
|
public function limit($count, $offset = 0) |
|
41
|
|
|
{ |
|
42
|
1 |
|
$this->limit = array((int) $count, (int) $offset); |
|
43
|
|
|
|
|
44
|
1 |
|
return $this; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Sets the fields to order the results by. They should be in the |
|
49
|
|
|
* the format 'fieldname ASC|DESC'. e.g 'dateAdded DESC'. |
|
50
|
|
|
* |
|
51
|
|
|
* @param mixed $fields An array comprising strings in the above format |
|
52
|
|
|
* (or a single string) |
|
53
|
|
|
* |
|
54
|
|
|
* @return Query The same instance of this class. |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public function orderBy($fields) |
|
57
|
|
|
{ |
|
58
|
1 |
|
$this->orderBy = (array) $fields; |
|
59
|
|
|
|
|
60
|
1 |
|
return $this; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @see Query::andWhere |
|
65
|
|
|
* |
|
66
|
|
|
* @return Query The same instance of this class. |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function where($field, $operator = null, $value = null) |
|
69
|
|
|
{ |
|
70
|
1 |
|
return $this->andWhere($field, $operator, $value); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Adds a boolean AND predicate for this query, |
|
75
|
|
|
* |
|
76
|
|
|
* @param string|Closure $field The name of the field to match or an anonymous |
|
77
|
|
|
* function that will define sub predicates. |
|
78
|
|
|
* @param string $operator An operator from the allowed list. |
|
79
|
|
|
* @param string $value The value to compare against. |
|
80
|
|
|
* |
|
81
|
|
|
* @return Query The same instance of this class. |
|
82
|
|
|
*/ |
|
83
|
1 |
|
public function andWhere($field, $operator = null, $value = null) |
|
84
|
|
|
{ |
|
85
|
1 |
|
$this->predicate->andWhere($field, $operator, $value); |
|
86
|
|
|
|
|
87
|
1 |
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Adds a boolean OR predicate for this query, |
|
92
|
|
|
* |
|
93
|
|
|
* @param string|Closure $field The name of the field to match or an anonymous |
|
94
|
|
|
* function that will define sub predicates. |
|
95
|
|
|
* @param string $operator An operator from the allowed list. |
|
96
|
|
|
* @param string $value The value to compare against. |
|
97
|
|
|
* |
|
98
|
|
|
* @return Query The same instance of this class. |
|
99
|
|
|
*/ |
|
100
|
|
|
public function orWhere($field, $operator = null, $value = null) |
|
101
|
|
|
{ |
|
102
|
|
|
$this->predicate->orWhere($field, $operator, $value); |
|
103
|
|
|
|
|
104
|
|
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Runs the query. |
|
109
|
|
|
* |
|
110
|
|
|
* @return Result The documents returned from this query. |
|
111
|
|
|
*/ |
|
112
|
|
|
public function execute() |
|
113
|
|
|
{ |
|
114
|
|
|
$qe = new QueryExecuter($this->repo, $this->predicate, $this->limit, $this->orderBy); |
|
115
|
|
|
|
|
116
|
|
|
return $qe->run(); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|