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
|
|
|
use Traversable; |
11
|
|
|
|
12
|
|
|
class Collection implements \IteratorAggregate |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $name; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var DocumentStore |
21
|
|
|
*/ |
22
|
|
|
protected $store; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Database |
26
|
|
|
*/ |
27
|
|
|
protected $database; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Parser |
31
|
|
|
*/ |
32
|
|
|
protected $parser; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Construct a new collection. |
36
|
|
|
* |
37
|
|
|
* @param Database $database |
38
|
|
|
* @param DocumentStore $store |
39
|
|
|
* @param string $name |
40
|
|
|
*/ |
41
|
6 |
|
public function __construct(Database $database, DocumentStore $store, $name) |
42
|
|
|
{ |
43
|
6 |
|
$this->name = $name; |
44
|
6 |
|
$this->store = $store; |
45
|
6 |
|
$this->database = $database; |
46
|
6 |
|
$this->parser = $database->getParser(); |
47
|
6 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Drop the collection. |
51
|
|
|
* |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
1 |
|
public function drop() |
55
|
|
|
{ |
56
|
1 |
|
return $this->database->dropCollection($this->name); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Remove all the documents from the collection. |
61
|
|
|
* |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
1 |
|
public function truncate() |
65
|
|
|
{ |
66
|
1 |
|
$this->store->truncate(); |
67
|
1 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Insert a new document. |
71
|
|
|
* |
72
|
|
|
* @param DocumentInterface $document |
73
|
|
|
* |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
1 |
|
public function insert(DocumentInterface $document) |
77
|
|
|
{ |
78
|
1 |
|
return $this->store->insert($document); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Update existing documents. |
83
|
|
|
* |
84
|
|
|
* @param array $criteria |
85
|
|
|
* @param DocumentInterface $updated |
86
|
|
|
* @param bool $multiple |
87
|
|
|
* |
88
|
|
|
* @return int The count of the documents updated. |
89
|
|
|
*/ |
90
|
|
View Code Duplication |
public function update($criteria, DocumentInterface $updated, $multiple = false) |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$limit = $multiple ? 1 : null; |
93
|
|
|
$documents = $this->onMatch($criteria, $limit); |
94
|
|
|
|
95
|
|
|
foreach ($documents as $document) { |
96
|
|
|
$this->store->update($updated); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Remove documents from the collection. |
102
|
|
|
* |
103
|
|
|
* @param mixed $criteria |
104
|
|
|
* @param bool $multiple |
105
|
|
|
* |
106
|
|
|
* @return int The count of the document deleted. |
107
|
|
|
*/ |
108
|
1 |
View Code Duplication |
public function remove($criteria, $multiple = false) |
|
|
|
|
109
|
|
|
{ |
110
|
1 |
|
$limit = $multiple ? 1 : null; |
111
|
|
|
|
112
|
1 |
|
$documents = $this->onMatch($criteria, $limit); |
113
|
|
|
|
114
|
1 |
|
foreach ($documents as $document) { |
115
|
1 |
|
$this->store->remove($document->get('_id')); |
116
|
1 |
|
} |
117
|
|
|
|
118
|
1 |
|
return true; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Find documents in the collection. |
123
|
|
|
* |
124
|
|
|
* @param array $criteria |
125
|
|
|
* |
126
|
|
|
* @return array The array of results. |
127
|
|
|
*/ |
128
|
1 |
|
public function find($criteria) |
129
|
|
|
{ |
130
|
1 |
|
return $this->onMatch($criteria); |
131
|
|
|
} |
132
|
|
|
|
133
|
2 |
|
protected function onMatch($criteria, $limit = null) |
134
|
|
|
{ |
135
|
2 |
|
$expression = $this->parser->parse($criteria); |
136
|
2 |
|
$matcher = $this->newMatcher($expression); |
137
|
|
|
|
138
|
2 |
|
$documents = $this->store->scan([$matcher, 'match'], $limit); |
139
|
|
|
|
140
|
2 |
|
return $documents; |
141
|
|
|
} |
142
|
|
|
|
143
|
2 |
|
protected function newMatcher(ExpressionInterface $expression) |
144
|
|
|
{ |
145
|
2 |
|
return new Matcher($expression); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function getIterator() |
149
|
|
|
{ |
150
|
|
|
return $this->store->getIterator(); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
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.