Completed
Push — master ( dd6108...435347 )
by Patrick
02:41
created

MongoDataTable::search()   C

Complexity

Conditions 8
Paths 64

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 18
nc 64
nop 6
dl 0
loc 32
rs 5.3846
c 0
b 0
f 0
1
<?php
2
namespace Data;
3
4
class MongoDataTable extends DataTable
5
{
6
    protected $collection;
7
    protected $namespace;
8
9
    public function __construct($collection, $collection_name = false)
10
    {
11
        if($collection_name !== false)
12
        {
13
            $this->namespace = $collection.'.'.$collection_name;
14
        }
15
        else
16
        {
17
            $this->collection = $collection;
18
        }
19
    }
20
21
    public function count($filter = false)
22
    {
23
        $criteria = array();
24
        if($filter !== false)
25
        {
26
            if($filter instanceof \Data\Filter)
27
            {
28
                $criteria = $filter->to_mongo_filter();
29
            }
30
            else
31
            {
32
                $criteria = $filter;
33
            }
34
        }
35
        return $this->collection->count($criteria);
36
    }
37
38
    private function getCriteriaFromFilter($filter)
39
    {
40
        if($filter === false)
41
        {
42
            return array();
43
        }
44
        if(is_array($filter))
45
        {
46
            return $filter;
47
        }
48
        return $filter->to_mongo_filter();
49
    }
50
51
    public function read($filter = false, $select = false, $count = false, $skip = false, $sort = false, $params = false)
52
    {
53
        $fields   = array();
54
        $criteria = $this->getCriteriaFromFilter($filter);
55
        if($select !== false)
56
        {
57
            $fields = array_fill_keys($select, 1);
58
        }
59
        $cursor = $this->collection->find($criteria, $fields);
60
        if($params !== false && isset($params['fields']))
61
        {
62
            $cursor->fields($params['fields']);
63
        }
64
        if($sort !== false)
65
        {
66
            $cursor->sort($sort);
67
        }
68
        if($skip !== false)
69
        {
70
            $cursor->skip($skip);
71
        }
72
        if($count !== false)
73
        {
74
            $cursor->limit($count);
75
        }
76
        $ret = array();
77
        foreach($cursor as $doc)
78
        {
79
            array_push($ret, $doc);
80
        }
81
        return $ret;
82
    }
83
84
    public function create($data)
85
    {
86
        $res = $this->collection->insert($data);
87
        if($res === false || $res['err'] !== null)
88
        {
89
            return false;
90
        }
91
        return $data['_id'];
92
    }
93
94
    public function update($filter, $data)
95
    {
96
        $criteria = $this->getCriteriaFromFilter($filter);
97
        if(isset($data['_id']))
98
        {
99
            unset($data['_id']);
100
        }
101
        $res = $this->collection->update($criteria, array('$set' => $data));
102
        if($res === false || $res['err'] !== null)
103
        {
104
            return false;
105
        }
106
        return true;
107
    }
108
109
    public function delete($filter)
110
    {
111
        $criteria = $this->getCriteriaFromFilter($filter);
112
        $res = $this->collection->remove($criteria);
113
        if($res === false || $res['err'] !== null)
114
        {
115
            return false;
116
        }
117
        return true;
118
    }
119
}
120