This repository has been archived by the owner on Mar 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEMongoQueryBuilder.php
128 lines (116 loc) · 3.64 KB
/
EMongoQueryBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/**
* EMongoQueryBuilder
*
* Represents the Yii edition to the MongoCursor and allows for lazy loading of objects.
*
* This class does not support eager loading by default, in order to use eager loading you should look into using this
* classes reponse with iterator_to_array().
*
* I did try originally to make this into a active data provider and use this for two fold operations but the cactivedataprovider would extend
* a lot for the cursor and the two took quite different constructors.
*/
class EMongoQueryBuilder
{
/**
* @var EMongoDocument
*/
public $model;
/**
* @var array|EMongoCriteria|MongoCursor|string
*/
private $query = '';
/**
* @var array
*/
private $options = [];
/**
* The cursor constructor
* @param string|EMongoDocument $modelClass - The class name for the active record
* @param array|MongoCursor|EMongoCriteria $criteria - Either a condition array (without sort,limit and skip) or a MongoCursor Object
* @param array $fields
*/
public function __construct($model, $criteria = [], $fields = [])
{
if (is_string($model)) {
$this->model = EMongoDocument::model($this->modelClass);
} elseif ($model instanceof EMongoDocument) {
$this->model = $model;
}
$this->options = [];
if ($criteria instanceof EMongoCriteria) {
$this->options['projection'] = $criteria->project;
if ($criteria->skip > 0) {
$this->options['skip'] = $criteria->skip;
}
if ($criteria->limit > 0) {
$this->options['limit'] = intval($criteria->limit);
}
if ($criteria->sort) {
$this->options['sort'] = $criteria->sort;
}
$this->query = $criteria->condition;
} else {
// Then we are doing an active query
$this->options['projection'] = $fields;
$this->query = $criteria;
}
}
/**
* Execute query
* @param bool $ac If set true will be returned array of EMongoDocuments if not hen array
* of CBSONDocument
* @return array
*/
public function queryAll($ac = false) : array
{
$finded = $this->model->getCollection()->find($this->query, $this->options);
$finded = iterator_to_array($finded);
if ($ac === true) {
return $this->model->populateRecords($finded, true, null, false);
}
return $finded;
}
/**
* Counts the records returned by the criteria. By default this will not take skip and limit into account
* you can add inject true as the first and only parameter to enable MongoDB to take those offsets into
* consideration.
*
* @param bool $takeSkip
* @return int
*/
public function count($takeSkip = false /* Was true originally but it was to change the way the driver worked which seemed wrong */)
{
return $this->model->getCollection()->count($this->query, $this->options);
}
/**
* Set sort fields
* @param array $fields
* @return EMongoQueryBuilder
*/
public function sort(array $fields)
{
$this->options['sort'] = $fields;
return $this;
}
/**
* Set skip
* @param int $num
* @return EMongoQueryBuilder
*/
public function skip($num = 0)
{
$this->options['skip'] = $num;
return $this;
}
/**
* Set limit
* @param int $num
* @return EMongoQueryBuilder
*/
public function limit($num = 0)
{
$this->options['limit'] = intval($num);
return $this;
}
}