This repository has been archived by the owner on Sep 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathResult.php
239 lines (229 loc) · 6.38 KB
/
Result.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
////////////////////////////////////////////////////////////////////////////////
// __________ __ ________ __________
// \______ \ |__ ______ / _____/ ____ _____ ______\______ \ _______ ___
// | ___/ | \\____ \/ \ ____/ __ \\__ \\_ __ \ | _// _ \ \/ /
// | | | Y \ |_> > \_\ \ ___/ / __ \| | \/ | ( <_> > <
// |____| |___| / __/ \______ /\___ >____ /__| |______ /\____/__/\_ \
// \/|__| \/ \/ \/ \/ \/
// -----------------------------------------------------------------------------
// Designed and Developed by Brad Jones <brad @="bjc.id.au" />
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
namespace Gears\NoSqLite;
/**
* Class: \Gears\NoSqLite\Result
* =============================================================================
* This class represents a result set, returned by the read functionality.
*/
class Result extends \ArrayObject
{
// TODO: Find out if this is really needed...
private $total_size;
/**
* Method: __construct
* =========================================================================
* This only seems to be here for the extra count functionality I seem to
* have added. However I can not for the life of me remember why I added
* this seemingly redundant code. That will teach for not documenting
* this class.
*
* Parameters:
* -------------------------------------------------------------------------
* $input - An array
*
* Returns:
* -------------------------------------------------------------------------
* void
*/
public function __construct($input)
{
$this->total_size = count($input);
parent::__construct($input);
}
// TODO: Find out if this is really needed...
public function count()
{
return $this->total_size;
}
// TODO: Find out if this is really needed...
public function size()
{
return parent::count();
}
/**
* Method: skip
* =========================================================================
* This skips over records in the result set.
*
* Parameters:
* -------------------------------------------------------------------------
* $value - How many records to skip.
*
* Returns:
* -------------------------------------------------------------------------
* self
*/
public function skip($value)
{
if ($value > 0)
{
$counter = 0; $results = array();
foreach ($this->getArrayCopy() as $key => $result)
{
if ($counter >= $value)
{
$results[$key] = $result;
}
else
{
$counter++;
}
}
$this->exchangeArray($results);
}
return $this;
}
/**
* Method: limit
* =========================================================================
* This limits how many records we return in the result set.
*
* Parameters:
* -------------------------------------------------------------------------
* $value - How many records to return.
*
* Returns:
* -------------------------------------------------------------------------
* self
*/
public function limit($value)
{
if ($value > 0)
{
$counter = 0; $results = array();
foreach ($this->getArrayCopy() as $key => $result)
{
if ($counter < $value)
{
$results[$key] = $result;
}
else
{
break;
}
$counter++;
}
$this->exchangeArray($results);
}
return $this;
}
/**
* Method: distinct
* =========================================================================
* Finds the distinct values for a specified field across a single
* collection. distinct returns a document that contains an array
* of the distinct values.
*
* Parameters:
* -------------------------------------------------------------------------
* $distinct_value - The field to collect distinct values from.
*
* Returns:
* -------------------------------------------------------------------------
* The distinct values
*/
public function distinct($distinct_value)
{
$distinct_values = array();
foreach ($this->getArrayCopy() as $result)
{
if (isset($result[$distinct_value]) && !in_array($result[$distinct_value], $distinct_values))
{
$distinct_values[] = $result[$distinct_value];
}
elseif (strpos($distinct_value, '.'))
{
$dot_value = $result;
$dot_keys = explode('.', $distinct_value);
foreach ($dot_keys as $dot_key)
{
if (isset($dot_value[$dot_key]))
{
$dot_value = $dot_value[$dot_key];
}
else
{
$dot_value = null; break;
}
}
if (!empty($dot_value) && !in_array($dot_value, $distinct_values))
{
$distinct_values[] = $dot_value;
}
}
}
return $distinct_values;
}
/**
* Method: sort
* =========================================================================
* Controls the order that the query returns matching documents.
* For each field in the sort document, if the field’s corresponding
* value is positive, then sort() returns query results in ascending order
* for that attribute. If the field’s corresponding value is negative,
* then sort() returns query results in descending order.
*
* Parameters:
* -------------------------------------------------------------------------
* $value - The sort query
*
* Returns:
* -------------------------------------------------------------------------
* self
*/
public function sort($value)
{
$args = array();
$data = $this->getArrayCopy();
foreach ($value as $sort_key => $sort_order)
{
$tmp = array();
foreach ($data as $key => $row)
{
if (isset($row[$sort_key]))
{
$tmp[$key] = $row[$sort_key];
}
elseif (strpos($sort_key, '.'))
{
$dot_value = $row;
$dot_keys = explode('.', $sort_key);
foreach ($dot_keys as $dot_key)
{
if (isset($dot_value[$dot_key]))
{
$dot_value = $dot_value[$dot_key];
}
else
{
$dot_value = null; break;
}
}
$tmp[$key] = $dot_value;
}
else
{
$tmp[$key] = null;
}
}
$args[] = $tmp;
if ($sort_order == 1) $args[] = SORT_ASC;
elseif ($sort_order == -1) $args[] = SORT_DESC;
}
$args[] = &$data;
call_user_func_array('array_multisort', $args);
$this->exchangeArray(array_pop($args));
return $this;
}
}