Completed
Push — master ( bef41f...9e2ad4 )
by Patrick
02:56
created

MongoDataTable::getCriteriaFromFilter()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 12
rs 9.4285
c 1
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 search($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
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
121