1: <?php
2: namespace Hook;
3:
4: class Collection {
5:
6: 7: 8: 9:
10: protected $name;
11:
12: 13: 14: 15:
16: protected $segments;
17:
18: 19: 20: 21:
22: protected $client;
23:
24: 25: 26: 27:
28: protected $wheres;
29:
30: 31: 32: 33:
34: protected $options;
35:
36: 37: 38: 39:
40: protected $ordering;
41:
42: 43: 44: 45:
46: protected $group;
47:
48: 49: 50: 51:
52: protected $limit;
53:
54: 55: 56: 57:
58: protected $offset;
59:
60: public function __construct(array $options) {
61: $this->name = $options['name'];
62: $this->client = isset($options['client']) ? $options['client'] : Client::getInstance();
63: $this->segments = 'collection/' . $this->name;
64: $this->reset();
65: }
66:
67: protected function reset() {
68: $this->wheres = array();
69: $this->options = array();
70: $this->ordering = array();
71: $this->group = array();
72: $this->limit = null;
73: $this->offset = null;
74: return $this;
75: }
76:
77: public function create(array $data) {
78: return $this->client->post($this->segments, $data);
79: }
80:
81: public function where($field, $_operation = null, $_value = null, $operation = 'and') {
82: $operation = (is_null($_value)) ? "=" : $_operation;
83: $value = (is_null($_value)) ? $_operation : $_value;
84:
85: if (is_array($field)) {
86: foreach($field as $field => $value) {
87: if (is_array($value)) {
88: $operation = $value[0];
89: $value = $value[1];
90: }
91: $this->addWhere($field, $operation, $value, $operation);
92: }
93: } else {
94: $this->addWhere($field, $operation, $value, $operation);
95: }
96:
97: return $this;
98: }
99:
100: public function orWhere($field, $_operation = null, $_value = null) {
101: return $this->where($field, $_operation, $_value, 'or');
102: }
103:
104: public function get() {
105: return $this->client->get($this->segments, $this->buildQuery());
106: }
107:
108: public function find($_id) {
109: return $this->client->get($this->segments . '/' . $_id, $this->buildQuery());
110: }
111:
112: public function select() {
113: $this->options['select'] = func_get_args();
114: return $this;
115: }
116:
117: public function with() {
118: $this->options['with'] = func_get_args();
119: return $this;
120: }
121:
122: public function group() {
123: $this->group = func_get_args();
124: return $this;
125: }
126:
127: public function count($field = '*') {
128: $this->options['aggregation'] = array('method' => 'count', 'field' => $field);
129: return $this->get();
130: }
131:
132: public function max($field) {
133: $this->options['aggregation'] = array('method' => 'max', 'field' => $field);
134: return $this->get();
135: }
136:
137: public function min($field) {
138: $this->options['aggregation'] = array('method' => 'min', 'field' => $field);
139: return $this->get();
140: }
141:
142: public function avg($field) {
143: $this->options['aggregation'] = array('method' => 'avg', 'field' => $field);
144: return $this->get();
145: }
146:
147: public function sum($field) {
148: $this->options['aggregation'] = array('method' => 'sum', 'field' => $field);
149: return $this->get();
150: }
151:
152: public function first() {
153: $this->options['first'] = 1;
154: return $this->get();
155: }
156:
157: public function firstOrCreate($data) {
158: $this->options['first'] = 1;
159: $this->options['data'] = $data;
160: return $this->client->post($this->segments, $this->buildQuery());
161: }
162:
163: public function sort($field, $direction = null) {
164: if (is_null($direction)) {
165: $direction = 'asc';
166: } else if (is_int($direction)) {
167: $direction = (intval($direction) === -1) ? 'desc' : 'asc';
168: }
169: $this->ordering[] = array($field, $direction);
170: return $this;
171: }
172:
173: public function limit($int) {
174: $this->limit = $int;
175: return $this;
176: }
177:
178: public function offset($int) {
179: $this->offset = $int;
180: return $this;
181: }
182:
183: public function channel($options) {
184: throw Exception("Not implemented.");
185: }
186:
187: public function remove($_id = null) {
188: $path = $this->segments;
189: if (!is_null($_id)) {
190: $path .= '/' . $_id;
191: }
192: return $this->client->remove($path, $this->buildQuery());
193: }
194:
195: public function update($_id, array $data = null) {
196: return $this->client->post($this->segments . '/' . $_id, $data);
197: }
198:
199: public function increment($field, $value) {
200: $this->options['operation'] = array('method' => 'increment', 'field' => $field, 'value' => $value);
201: return $this->client->put($this->segments, $this->buildQuery());
202: }
203:
204: public function decrement($field, $value) {
205: $this->options['operation'] = array('method' => 'decrement', 'field' => $field, 'value' => $value);
206: return $this->client->put($this->segments, $this->buildQuery());
207: }
208:
209: public function updateAll(array $data) {
210: $this->options['data'] = $data;
211: return $this->client->put($this->segments, $this->buildQuery());
212: }
213:
214: protected function addWhere($field, $operation, $value) {
215: $this->wheres[] = array($field, strtolower($operation), $value);
216: return $this;
217: }
218:
219: protected function buildQuery() {
220: $query = array();
221:
222:
223: if ($this->limit !== null) { $query['limit'] = $this->limit; }
224: if ($this->offset !== null) { $query['offset'] = $this->offset; }
225:
226:
227: if (count($this->wheres) > 0) {
228: $query['q'] = $this->wheres;
229: }
230:
231:
232: if (count($this->ordering) > 0) {
233: $query['s'] = $this->ordering;
234: }
235:
236:
237: if (count($this->group) > 0) {
238: $query['g'] = $this->group;
239: }
240:
241: $shortnames = array(
242: 'paginate' => 'p',
243: 'first' => 'f',
244: 'aggregation' => 'aggr',
245: 'operation' => 'op',
246: 'data' => 'data',
247: 'with' => 'with',
248: 'select' => 'select'
249: );
250:
251: foreach($shortnames as $field => $shortname) {
252: if (isset($this->options[$field])) {
253: $query[$shortnames[$field]] = $this->options[$field];
254: }
255: }
256:
257:
258: $this->reset();
259:
260: return $query;
261: }
262: }
263: