1: <?php
2:
3: namespace PHPixie\Database\Driver\Mongo\Query;
4:
5: class Runner
6: {
7: protected $chain = array();
8: public function chainProperty($property)
9: {
10: $this->chain[] = array(
11: 'type' => 'property',
12: 'name' => $property
13: );
14: }
15:
16: public function chainMethod($method, $args = array())
17: {
18: $this->chain[] = array(
19: 'type' => 'method',
20: 'name' => $method,
21: 'args' => $args
22: );
23: }
24:
25: public function getChain()
26: {
27: return $this->chain;
28: }
29:
30: public function run($connection)
31: {
32: $current = $connection->database();
33:
34: foreach ($this->chain as $step) {
35: switch ($step['type']) {
36: case 'property':
37: $property = $step['name'];
38: $current = $current->$property;
39: break;
40: case 'method':
41: $args = $step['args'];
42: $current = call_user_func_array(array($current, $step['name']), $args);
43:
44: if ($step['name'] === 'insert')
45: $connection->setInsertId((string) $args[0]['_id']);
46:
47: if ($step['name'] === 'batchInsert') {
48: $last = end($args[0]);
49: $connection->setInsertId((string) $last['_id']);
50: }
51:
52: break;
53: }
54: }
55:
56: return $current;
57: }
58: }
59: