1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace cvweiss\projectbase; |
4
|
|
|
|
5
|
|
|
use MongoDB\Driver\BulkWrite; |
6
|
|
|
|
7
|
|
|
class MongoDoc |
8
|
|
|
{ |
9
|
|
|
private $collection = null; |
10
|
|
|
private $data = null; |
11
|
|
|
private $updates = []; |
12
|
|
|
|
13
|
|
|
public function __construct(string $collection, array $row = null) |
14
|
|
|
{ |
15
|
|
|
$this->collection = $collection; |
16
|
|
|
$this->data = $row === null ? [] : $row; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function get(string $field) |
20
|
|
|
{ |
21
|
|
|
return $this->data[$field] ?? null; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function getAll() |
25
|
|
|
{ |
26
|
|
|
return $this->data; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function set(string $field, $value):MongoDoc |
30
|
|
|
{ |
31
|
|
|
$this->data[$field] = $value; |
32
|
|
|
$this->updates[$field] = $value; |
33
|
|
|
return $this; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function setAll(array $params):MongoDoc |
37
|
|
|
{ |
38
|
|
|
foreach ($params as $key=>$value) { |
39
|
|
|
$this->set($key, $value); |
40
|
|
|
} |
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function save(BulkWrite $bulk = null):bool |
45
|
|
|
{ |
46
|
|
|
$return = isset($this->data['_id']) ? $this->update($bulk) : $this->insert($bulk); |
47
|
|
|
$this->updates = []; |
48
|
|
|
|
49
|
|
|
return $return; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function getBulkWriter(BulkWrite $bulk = null):BulkWrite |
53
|
|
|
{ |
54
|
|
|
return $bulk ?? new BulkWrite(['ordered' => true]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function doCommit(BulkWrite $bulk, bool $doCommit):bool |
58
|
|
|
{ |
59
|
|
|
if ($doCommit !== false) { |
60
|
|
|
$return = Mongo::get()->executeBulkWrite($this->collection, $bulk); |
61
|
|
|
return (count($return->getWriteErrors()) == 0); |
62
|
|
|
} |
63
|
|
|
return false; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function insert(BulkWrite $bulk = null):bool |
67
|
|
|
{ |
68
|
|
|
$commit = $bulk === null; |
69
|
|
|
$bulk = $this->getBulkWriter($bulk); |
70
|
|
|
|
71
|
|
|
$id = $bulk->insert($this->data); |
72
|
|
|
$this->data['_id'] = $id; |
73
|
|
|
|
74
|
|
|
return $this->doCommit($bulk, $commit); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function update(BulkWrite $bulk = null):bool |
78
|
|
|
{ |
79
|
|
|
// If we have nothing to update then move along |
80
|
|
|
if (sizeof($this->updates) == 0) return true; |
81
|
|
|
|
82
|
|
|
$commit = $bulk === null; |
83
|
|
|
$bulk = $this->getBulkWriter($bulk); |
84
|
|
|
|
85
|
|
|
$bulk->update(['_id' => $this->data['_id']], ['$set' => $this->updates]); |
86
|
|
|
|
87
|
|
|
return $this->doCommit($bulk, $commit); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function delete(BulkWrite $bulk = null):bool |
91
|
|
|
{ |
92
|
|
|
$commit = $bulk === null; |
93
|
|
|
$bulk = $this->getBulkWriter($bulk); |
94
|
|
|
|
95
|
|
|
$bulk->delete(['_id' => $this->data['_id']]); |
96
|
|
|
|
97
|
|
|
return $this->doCommit($bulk, $commit); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function __set($foo, $bar) |
101
|
|
|
{ |
102
|
|
|
throw new \Exception("unable to set $foo to $bar; use set(\$field, \$value)"); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|