22
33namespace Tequila \MongoDB ;
44
5+ use MongoDB \Driver \Manager ;
6+ use MongoDB \Driver \ReadConcern ;
7+ use MongoDB \Driver \ReadPreference ;
8+ use MongoDB \Driver \WriteConcern ;
9+ use Tequila \MongoDB \BSON \BSONArray ;
10+ use Tequila \MongoDB \Command \CreateIndexes ;
11+ use Tequila \MongoDB \Command \DropCollection ;
12+ use Tequila \MongoDB \Command \DropIndexes ;
13+ use Tequila \MongoDB \Command \ListIndexes ;
514use Tequila \MongoDB \Operation \Find ;
15+ use Tequila \MongoDB \Options \DatabaseAndCollectionOptions ;
616use Tequila \MongoDB \Write \Bulk \BulkWrite ;
717use Tequila \MongoDB \Write \Bulk \BulkWriteOptions ;
818use Tequila \MongoDB \Write \Model \DeleteMany ;
1929
2030class Collection
2131{
22- use Traits \ReadPreferenceAndConcernsTrait;
23-
2432 /**
25- * @var Connection
33+ * @var Manager
2634 */
27- private $ connection ;
35+ private $ manager ;
2836
2937 /**
3038 * @var string
@@ -34,18 +42,45 @@ class Collection
3442 /**
3543 * @var string
3644 */
37- private $ name ;
45+ private $ collectionName ;
46+
47+ /**
48+ * @var ReadConcern|null
49+ */
50+ private $ readConcern ;
51+
52+ /**
53+ * @var ReadPreference|null
54+ */
55+ private $ readPreference ;
56+
57+ /**
58+ * @var WriteConcern|null
59+ */
60+ private $ writeConcern ;
61+
62+ /**
63+ * @var array
64+ */
65+ private $ typeMap ;
3866
3967 /**
40- * @param Connection $connection
68+ * @param Manager $manager
4169 * @param string $databaseName
4270 * @param string $collectionName
71+ * @param array $options
4372 */
44- public function __construct (Connection $ connection , $ databaseName , $ collectionName )
73+ public function __construct (Manager $ manager , $ databaseName , $ collectionName, array $ options = [] )
4574 {
46- $ this ->connection = $ connection ;
47- $ this ->databaseName = $ databaseName ;
48- $ this ->name = $ collectionName ;
75+ $ this ->manager = $ manager ;
76+ $ this ->databaseName = (string )$ databaseName ;
77+ $ this ->collectionName = (string )$ collectionName ;
78+
79+ $ options = DatabaseAndCollectionOptions::resolve ($ options , $ manager );
80+ $ this ->readConcern = $ options ['readConcern ' ];
81+ $ this ->readPreference = $ options ['readPreference ' ];
82+ $ this ->writeConcern = $ options ['writeConcern ' ];
83+ $ this ->typeMap = $ options ['typeMap ' ];
4984 }
5085
5186 /**
@@ -59,17 +94,52 @@ public function getDatabaseName()
5994 /**
6095 * @return string
6196 */
62- public function getName ()
97+ public function getCollectionName ()
6398 {
64- return $ this ->name ;
99+ return $ this ->collectionName ;
65100 }
66101
67102 /**
103+ * @param array $options
68104 * @return array
69105 */
70- public function drop ()
106+ public function drop (array $ options = [])
107+ {
108+ $ command = new DropCollection ($ this ->databaseName , $ this ->collectionName , $ options );
109+ $ cursor = $ command ->execute ($ this ->manager );
110+
111+ return current ($ cursor ->toArray ());
112+ }
113+
114+ /**
115+ * @param array $options
116+ * @return array|object
117+ */
118+ public function dropIndexes (array $ options = [])
119+ {
120+ $ command = new DropIndexes ($ this ->databaseName , $ this ->collectionName , '* ' , $ options );
121+ $ cursor = $ command ->execute ($ this ->manager );
122+
123+ return current ($ cursor ->toArray ());
124+ }
125+
126+ /**
127+ * @param string $indexName
128+ * @param array $options
129+ * @return array|object
130+ */
131+ public function dropIndex ($ indexName , array $ options = [])
71132 {
72- return $ this ->connection ->dropCollection ($ this ->databaseName , $ this ->name );
133+ $ command = new DropIndexes (
134+ $ this ->databaseName ,
135+ $ this ->collectionName ,
136+ $ indexName ,
137+ $ options
138+ );
139+
140+ $ cursor = $ command ->execute ($ this ->manager );
141+
142+ return current ($ cursor ->toArray ());
73143 }
74144
75145 /**
@@ -79,10 +149,36 @@ public function drop()
79149 */
80150 public function bulkWrite (array $ requests , array $ options = [])
81151 {
82- $ options = $ options + ['writeConcern ' => $ this ->getWriteConcern () ];
152+ $ options = $ options + ['writeConcern ' => $ this ->writeConcern ];
83153 $ bulk = new BulkWrite ($ requests , $ options );
84154
85- return $ bulk ->execute ($ this ->connection , $ this ->databaseName , $ this ->name );
155+ return $ bulk ->execute ($ this ->manager , $ this ->databaseName , $ this ->collectionName );
156+ }
157+
158+ /**
159+ * @param Index[] $indexes
160+ * @return string[]
161+ */
162+ public function createIndexes (array $ indexes )
163+ {
164+ $ command = new CreateIndexes ($ this ->databaseName , $ this ->collectionName , $ indexes );
165+ $ command ->execute ($ this ->manager );
166+
167+ return array_map (function (Index $ index ) {
168+ return $ index ->getName ();
169+ }, $ indexes );
170+ }
171+
172+ /**
173+ * @param array $key
174+ * @param array $options
175+ * @return string
176+ */
177+ public function createIndex (array $ key , array $ options = [])
178+ {
179+ $ index = new Index ($ key , $ options );
180+
181+ return current ($ this ->createIndexes ([$ index ]));
86182 }
87183
88184 /**
@@ -94,7 +190,7 @@ public function find($filter = [], array $options = [])
94190 {
95191 $ operation = new Find ($ filter , $ options );
96192
97- return $ operation ->execute ($ this ->connection , $ this ->databaseName , $ this ->name );
193+ return $ operation ->execute ($ this ->manager , $ this ->databaseName , $ this ->collectionName );
98194 }
99195
100196 /**
@@ -104,7 +200,6 @@ public function find($filter = [], array $options = [])
104200 */
105201 public function insertOne ($ document , array $ options = [])
106202 {
107-
108203 $ model = new InsertOne ($ document );
109204 $ bulkWriteResult = $ this ->bulkWrite ([$ model ], $ options );
110205
@@ -157,6 +252,17 @@ public function deleteMany($filter, array $options = [])
157252 return new DeleteResult ($ bulkWriteResult );
158253 }
159254
255+ /**
256+ * @return array|BSONArray
257+ */
258+ public function listIndexes ()
259+ {
260+ $ command = new ListIndexes ($ this ->databaseName , $ this ->collectionName );
261+ $ cursor = $ command ->execute ($ this ->manager );
262+
263+ return $ cursor ->toArray ();
264+ }
265+
160266 /**
161267 * @param array|object $filter
162268 * @param $update
0 commit comments