1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mattbit\Flat; |
4
|
|
|
|
5
|
|
|
use Mattbit\Flat\Query\Parser; |
6
|
|
|
use Mattbit\Flat\Query\Matcher; |
7
|
|
|
use Mattbit\Flat\Storage\DocumentStore; |
8
|
|
|
use Mattbit\Flat\Model\DocumentInterface; |
9
|
|
|
use Mattbit\Flat\Query\Expression\ExpressionInterface; |
10
|
|
|
|
11
|
|
|
class Collection |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $name; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var DocumentStore |
20
|
|
|
*/ |
21
|
|
|
protected $store; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Database |
25
|
|
|
*/ |
26
|
|
|
protected $database; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Parser |
30
|
|
|
*/ |
31
|
|
|
protected $parser; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Construct a new collection. |
35
|
|
|
* |
36
|
|
|
* @param Database $database |
37
|
|
|
* @param DocumentStore $store |
38
|
|
|
* @param string $name |
39
|
|
|
*/ |
40
|
7 |
|
public function __construct(Database $database, DocumentStore $store, $name) |
41
|
|
|
{ |
42
|
7 |
|
$this->name = $name; |
43
|
7 |
|
$this->store = $store; |
44
|
7 |
|
$this->database = $database; |
45
|
7 |
|
$this->parser = $database->getParser(); |
46
|
7 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Drop the collection. |
50
|
|
|
* |
51
|
|
|
* @return bool |
52
|
|
|
*/ |
53
|
1 |
|
public function drop() |
54
|
|
|
{ |
55
|
1 |
|
return $this->database->dropCollection($this->name); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Remove all the documents from the collection. |
60
|
|
|
* |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
1 |
|
public function truncate() |
64
|
|
|
{ |
65
|
1 |
|
$this->store->truncate(); |
66
|
1 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Insert a new document. |
70
|
|
|
* |
71
|
|
|
* @param DocumentInterface $document |
72
|
|
|
* |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
1 |
|
public function insert(DocumentInterface $document) |
76
|
|
|
{ |
77
|
1 |
|
return $this->store->insert($document); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Update existing documents. |
82
|
|
|
* |
83
|
|
|
* @param array $criteria |
84
|
|
|
* @param DocumentInterface $updated |
85
|
|
|
* @param bool $multiple |
86
|
|
|
* |
87
|
|
|
* @return int The count of the documents updated. |
88
|
|
|
*/ |
89
|
|
View Code Duplication |
public function update($criteria, DocumentInterface $updated, $multiple = false) |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
$limit = $multiple ? 1 : null; |
92
|
|
|
$documents = $this->onMatch($criteria, $limit); |
93
|
|
|
|
94
|
|
|
$count = 0; |
95
|
|
|
foreach ($documents as $document) { |
96
|
|
|
$updated->setId($document->getId()); |
97
|
|
|
$this->store->update($updated); |
98
|
|
|
$count++; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $count; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Remove documents from the collection. |
106
|
|
|
* |
107
|
|
|
* @param mixed $criteria |
108
|
|
|
* @param bool $multiple |
109
|
|
|
* |
110
|
|
|
* @return int The count of the document deleted. |
111
|
|
|
*/ |
112
|
1 |
View Code Duplication |
public function remove($criteria, $multiple = false) |
|
|
|
|
113
|
|
|
{ |
114
|
1 |
|
$limit = $multiple ? 1 : null; |
115
|
|
|
|
116
|
1 |
|
$documents = $this->onMatch($criteria, $limit); |
117
|
|
|
|
118
|
1 |
|
$count = 0; |
119
|
1 |
|
foreach ($documents as $document) { |
120
|
1 |
|
$this->store->remove($document->get('_id')); |
121
|
1 |
|
$count++; |
122
|
1 |
|
} |
123
|
|
|
|
124
|
1 |
|
return $count; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Find documents in the collection. |
129
|
|
|
* |
130
|
|
|
* @param array $criteria |
131
|
|
|
* |
132
|
|
|
* @return array The array of results. |
133
|
|
|
*/ |
134
|
2 |
|
public function find($criteria = []) |
135
|
|
|
{ |
136
|
2 |
|
return $this->onMatch($criteria); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Count documents in the collection. |
141
|
|
|
* |
142
|
|
|
* @param array $criteria |
143
|
|
|
* |
144
|
|
|
* @return int The number of documents. |
145
|
|
|
*/ |
146
|
|
|
public function count($criteria = []) |
147
|
|
|
{ |
148
|
|
|
if (empty($criteria)) { |
149
|
|
|
return $this->store->count(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return count($this->onMatch($criteria)); |
153
|
|
|
} |
154
|
|
|
|
155
|
3 |
|
protected function onMatch($criteria, $limit = null) |
156
|
|
|
{ |
157
|
3 |
|
$expression = $this->parser->parse($criteria); |
158
|
3 |
|
$matcher = $this->newMatcher($expression); |
159
|
|
|
|
160
|
3 |
|
$documents = $this->store->scan([$matcher, 'match'], $limit); |
161
|
|
|
|
162
|
3 |
|
return $documents; |
163
|
|
|
} |
164
|
|
|
|
165
|
3 |
|
protected function newMatcher(ExpressionInterface $expression) |
166
|
|
|
{ |
167
|
3 |
|
return new Matcher($expression); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.