1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mattbit\Flat; |
4
|
|
|
|
5
|
|
|
use Mattbit\Flat\Document\Document; |
6
|
|
|
use Mattbit\Flat\Document\Identifiable; |
7
|
|
|
use Mattbit\Flat\Query\Expression\ExpressionInterface; |
8
|
|
|
use Mattbit\Flat\Query\Matcher; |
9
|
|
|
use Mattbit\Flat\Storage\DocumentStore; |
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
|
|
|
* Construct a new collection. |
30
|
|
|
* |
31
|
|
|
* @param Database $database |
32
|
|
|
* @param DocumentStore $store |
33
|
|
|
* @param string $name |
34
|
|
|
*/ |
35
|
|
|
public function __construct(Database $database, DocumentStore $store, $name) |
36
|
|
|
{ |
37
|
|
|
$this->name = $name; |
38
|
|
|
$this->store = $store; |
39
|
|
|
$this->database = $database; |
40
|
|
|
$this->parser = $database->getParser(); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Drop the collection. |
45
|
|
|
* |
46
|
|
|
* @return bool |
47
|
|
|
*/ |
48
|
|
|
public function drop() |
49
|
|
|
{ |
50
|
|
|
return $this->database->dropCollection($this); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Remove all the documents from the collection. |
55
|
|
|
* |
56
|
|
|
* @return bool |
57
|
|
|
*/ |
58
|
|
|
public function truncate() |
59
|
|
|
{ |
60
|
|
|
$this->store->truncate(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Insert a new document. |
65
|
|
|
* |
66
|
|
|
* @param Identifiable $document |
67
|
|
|
* |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
public function insert(Identifiable $document) |
71
|
|
|
{ |
72
|
|
|
return $this->store->insertDocument($document); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Update existing documents. |
77
|
|
|
* |
78
|
|
|
* @param array $criteria |
79
|
|
|
* @param Identifiable $updated |
80
|
|
|
* @param bool $multiple |
81
|
|
|
* |
82
|
|
|
* @return int The count of the documents updated. |
83
|
|
|
*/ |
84
|
|
View Code Duplication |
public function update($criteria, Identifiable $updated, $multiple = false) |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
$limit = $multiple ? 1 : null; |
87
|
|
|
$documents = $this->onMatch($criteria, $limit); |
88
|
|
|
|
89
|
|
|
foreach ($documents as $document) { |
90
|
|
|
$this->store->updateDocument($updated, $multiple); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Remove documents from the collection. |
96
|
|
|
* |
97
|
|
|
* @param mixed $criteria |
98
|
|
|
* @param bool $multiple |
99
|
|
|
* |
100
|
|
|
* @return int The count of the document deleted. |
101
|
|
|
*/ |
102
|
|
View Code Duplication |
public function remove($criteria, $multiple = false) |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
$limit = $multiple ? 1 : null; |
105
|
|
|
|
106
|
|
|
$documents = $this->onMatch($criteria, $limit); |
107
|
|
|
|
108
|
|
|
foreach ($documents as $document) { |
109
|
|
|
$this->store->removeDocument($document->getId()); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return true; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Find documents in the collection. |
117
|
|
|
* |
118
|
|
|
* @param array $criteria |
119
|
|
|
* |
120
|
|
|
* @return array The array of results. |
121
|
|
|
*/ |
122
|
|
|
public function find($criteria) |
123
|
|
|
{ |
124
|
|
|
return $this->onMatch($criteria); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
protected function onMatch($criteria, $limit = null) |
128
|
|
|
{ |
129
|
|
|
$expression = $this->parser->parse($criteria); |
130
|
|
|
$matcher = $this->newMatcher($expression); |
131
|
|
|
|
132
|
|
|
$documents = $this->store->scanDocuments([$matcher, 'match'], $limit); |
133
|
|
|
|
134
|
|
|
return $documents; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
protected function newMatcher(ExpressionInterface $expression) |
138
|
|
|
{ |
139
|
|
|
return new Matcher($expression); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: