9
9
use MongoDB \WriteBatch ;
10
10
/* }}} */
11
11
12
- class Collection {
12
+ class Collection
13
+ {
13
14
const VERSION = "0.1.0 " ;
14
15
15
16
/* {{{ consts & vars */
@@ -51,7 +52,8 @@ class Collection {
51
52
* @param MongoDB\WriteConcern $wc The WriteConcern to apply to writes
52
53
* @param MongoDB\ReadPreference $rp The ReadPreferences to apply to reads
53
54
*/
54
- function __construct (Manager $ manager , $ ns , WriteConcern $ wc = null , ReadPreference $ rp = null ) {
55
+ public function __construct (Manager $ manager , $ ns , WriteConcern $ wc = null , ReadPreference $ rp = null )
56
+ {
55
57
$ this ->manager = $ manager ;
56
58
$ this ->ns = $ ns ;
57
59
$ this ->wc = $ wc ;
@@ -70,7 +72,8 @@ function __construct(Manager $manager, $ns, WriteConcern $wc = null, ReadPrefere
70
72
* @param array $options Additional options
71
73
* @return MongoDB\QueryResult
72
74
*/
73
- function find (array $ filter = array (), array $ options = array ()) {
75
+ public function find (array $ filter = array (), array $ options = array ())
76
+ {
74
77
$ options = array_merge ($ this ->getFindOptions (), $ options );
75
78
76
79
$ query = $ this ->_buildQuery ($ filter , $ options );
@@ -90,7 +93,8 @@ function find(array $filter = array(), array $options = array()) {
90
93
* @param array $options Additional options
91
94
* @return array|false The matched document, or false on failure
92
95
*/
93
- function findOne (array $ filter = array (), array $ options = array ()) {
96
+ public function findOne (array $ filter = array (), array $ options = array ())
97
+ {
94
98
$ options = array_merge ($ this ->getFindOptions (), array ("limit " => 1 ), $ options );
95
99
96
100
$ query = $ this ->_buildQuery ($ filter , $ options );
@@ -110,7 +114,8 @@ function findOne(array $filter = array(), array $options = array()) {
110
114
*
111
115
* @return array of MongoDB\Collection::find() options
112
116
*/
113
- function getFindOptions () {
117
+ public function getFindOptions ()
118
+ {
114
119
return array (
115
120
/**
116
121
* Get partial results from a mongos if some shards are down (instead of throwing an error).
@@ -214,7 +219,8 @@ function getFindOptions() {
214
219
* @return integer OP_QUERY Wire Protocol flags
215
220
* @internal
216
221
*/
217
- final protected function _opQueryFlags ($ options ) {
222
+ final protected function _opQueryFlags ($ options )
223
+ {
218
224
$ flags = 0 ;
219
225
220
226
$ flags |= $ options ["allowPartialResults " ] ? self ::QUERY_FLAG_PARTIAL : 0 ;
@@ -233,7 +239,8 @@ final protected function _opQueryFlags($options) {
233
239
* @return MongoDB\Query
234
240
* @internal
235
241
*/
236
- final protected function _buildQuery ($ filter , $ options ) {
242
+ final protected function _buildQuery ($ filter , $ options )
243
+ {
237
244
if ($ options ["comment " ]) {
238
245
$ options ["modifiers " ]['$comment ' ] = $ options ["comment " ];
239
246
}
@@ -258,7 +265,8 @@ final protected function _buildQuery($filter, $options) {
258
265
*
259
266
* @return array of available Write options
260
267
*/
261
- function getWriteOptions () {
268
+ public function getWriteOptions ()
269
+ {
262
270
return array (
263
271
"ordered " => false ,
264
272
"upsert " => false ,
@@ -271,7 +279,8 @@ function getWriteOptions() {
271
279
*
272
280
* @return array of available Bulk Write options
273
281
*/
274
- function getBulkOptions () {
282
+ public function getBulkOptions ()
283
+ {
275
284
return array (
276
285
"ordered " => false ,
277
286
);
@@ -324,18 +333,19 @@ function getBulkOptions() {
324
333
* @param array $options Additional options
325
334
* @return MongoDB\WriteResult
326
335
*/
327
- function bulkWrite (array $ bulk , array $ options = array ()) {
336
+ public function bulkWrite (array $ bulk , array $ options = array ())
337
+ {
328
338
$ options = array_merge ($ this ->getBulkOptions (), $ options );
329
339
330
340
$ batch = new WriteBatch ($ options ["ordered " ]);
331
341
332
- foreach ($ bulk as $ n => $ op ) {
333
- foreach ($ op as $ opname => $ args ) {
342
+ foreach ($ bulk as $ n => $ op ) {
343
+ foreach ($ op as $ opname => $ args ) {
334
344
if (!isset ($ args [0 ])) {
335
345
throw new \InvalidArgumentException (sprintf ("Missing argument#1 for '%s' (operation#%d) " , $ opname , $ n ));
336
346
}
337
347
338
- switch ($ opname ) {
348
+ switch ($ opname ) {
339
349
case "insertOne " :
340
350
$ batch ->insert ($ args [0 ]);
341
351
break ;
@@ -401,7 +411,8 @@ function bulkWrite(array $bulk, array $options = array()) {
401
411
* @param array $options Additional options
402
412
* @return MongoDB\InsertResult
403
413
*/
404
- function insertOne (array $ document ) {
414
+ public function insertOne (array $ document )
415
+ {
405
416
$ options = array_merge ($ this ->getWriteOptions ());
406
417
407
418
$ batch = new WriteBatch ($ options ["ordered " ]);
@@ -415,7 +426,8 @@ function insertOne(array $document) {
415
426
* Internal helper for delete one/many documents
416
427
* @internal
417
428
*/
418
- final protected function _delete ($ filter , $ limit = 1 ) {
429
+ final protected function _delete ($ filter , $ limit = 1 )
430
+ {
419
431
$ options = array_merge ($ this ->getWriteOptions (), array ("limit " => $ limit ));
420
432
421
433
$ batch = new WriteBatch ($ options ["ordered " ]);
@@ -432,7 +444,8 @@ final protected function _delete($filter, $limit = 1) {
432
444
* @param array $filter The $filter criteria to delete
433
445
* @return MongoDB\DeleteResult
434
446
*/
435
- function deleteOne (array $ filter ) {
447
+ public function deleteOne (array $ filter )
448
+ {
436
449
$ wr = $ this ->_delete ($ filter );
437
450
438
451
return new DeleteResult ($ wr );
@@ -447,7 +460,8 @@ function deleteOne(array $filter) {
447
460
* @param array $filter The $filter criteria to delete
448
461
* @return MongoDB\DeleteResult
449
462
*/
450
- function deleteMany (array $ filter ) {
463
+ public function deleteMany (array $ filter )
464
+ {
451
465
$ wr = $ this ->_delete ($ filter , 0 );
452
466
453
467
return new DeleteResult ($ wr );
@@ -457,7 +471,8 @@ function deleteMany(array $filter) {
457
471
* Internal helper for replacing/updating one/many documents
458
472
* @internal
459
473
*/
460
- protected function _update ($ filter , $ update , $ options ) {
474
+ protected function _update ($ filter , $ update , $ options )
475
+ {
461
476
$ options = array_merge ($ this ->getWriteOptions (), $ options );
462
477
463
478
$ batch = new WriteBatch ($ options ["ordered " ]);
@@ -476,7 +491,8 @@ protected function _update($filter, $update, $options) {
476
491
* @param array $options Additional options
477
492
* @return MongoDB\UpdateResult
478
493
*/
479
- function replaceOne (array $ filter , array $ update , array $ options = array ()) {
494
+ public function replaceOne (array $ filter , array $ update , array $ options = array ())
495
+ {
480
496
if (key ($ update )[0 ] == '$ ' ) {
481
497
throw new \InvalidArgumentException ("First key in \$update must NOT be a \$operator " );
482
498
}
@@ -497,7 +513,8 @@ function replaceOne(array $filter, array $update, array $options = array()) {
497
513
* @param array $options Additional options
498
514
* @return MongoDB\UpdateResult
499
515
*/
500
- function updateOne (array $ filter , array $ update , array $ options = array ()) {
516
+ public function updateOne (array $ filter , array $ update , array $ options = array ())
517
+ {
501
518
if (key ($ update )[0 ] != '$ ' ) {
502
519
throw new \InvalidArgumentException ("First key in \$update must be a \$operator " );
503
520
}
@@ -518,7 +535,8 @@ function updateOne(array $filter, array $update, array $options = array()) {
518
535
* @param array $options Additional options
519
536
* @return MongoDB\UpdateResult
520
537
*/
521
- function updateMany (array $ filter , $ update , array $ options = array ()) {
538
+ public function updateMany (array $ filter , $ update , array $ options = array ())
539
+ {
522
540
$ wr = $ this ->_update ($ filter , $ update , $ options + array ("limit " => 0 ));
523
541
524
542
return new UpdateResult ($ wr );
@@ -535,7 +553,8 @@ function updateMany(array $filter, $update, array $options = array()) {
535
553
* @param array $options Additional options
536
554
* @return integer
537
555
*/
538
- function count (array $ filter = array (), array $ options = array ()) {
556
+ public function count (array $ filter = array (), array $ options = array ())
557
+ {
539
558
$ cmd = array (
540
559
"count " => $ this ->collname ,
541
560
"query " => $ filter ,
@@ -553,7 +572,8 @@ function count(array $filter = array(), array $options = array()) {
553
572
*
554
573
* @return array of MongoDB\Collection::count() options
555
574
*/
556
- function getCountOptions () {
575
+ public function getCountOptions ()
576
+ {
557
577
return array (
558
578
/**
559
579
* The index to use.
@@ -596,7 +616,8 @@ function getCountOptions() {
596
616
* @param array $options Additional options
597
617
* @return integer
598
618
*/
599
- function distinct ($ fieldName , array $ filter = array (), array $ options = array ()) {
619
+ public function distinct ($ fieldName , array $ filter = array (), array $ options = array ())
620
+ {
600
621
$ options = array_merge ($ this ->getDistinctOptions (), $ options );
601
622
$ cmd = array (
602
623
"distinct " => $ this ->collname ,
@@ -616,7 +637,8 @@ function distinct($fieldName, array $filter = array(), array $options = array())
616
637
*
617
638
* @return array of MongoDB\Collection::distinct() options
618
639
*/
619
- function getDistinctOptions () {
640
+ public function getDistinctOptions ()
641
+ {
620
642
return array (
621
643
/**
622
644
* The maximum amount of time to allow the query to run. The default is infinite.
@@ -641,7 +663,8 @@ function getDistinctOptions() {
641
663
* @param array $options Additional options
642
664
* @return Iteratable
643
665
*/
644
- function aggregate (array $ pipeline , array $ options = array ()) {
666
+ public function aggregate (array $ pipeline , array $ options = array ())
667
+ {
645
668
$ options = array_merge ($ this ->getAggregateOptions (), $ options );
646
669
$ options = $ this ->_massageAggregateOptions ($ options );
647
670
$ cmd = array (
@@ -667,7 +690,8 @@ function aggregate(array $pipeline, array $options = array()) {
667
690
*
668
691
* @return array of MongoDB\Collection::aggregate() options
669
692
*/
670
- function getAggregateOptions () {
693
+ public function getAggregateOptions ()
694
+ {
671
695
$ opts = array (
672
696
/**
673
697
* Enables writing to temporary files. When set to true, aggregation stages
@@ -714,7 +738,8 @@ function getAggregateOptions() {
714
738
* Internal helper for massaging aggregate options
715
739
* @internal
716
740
*/
717
- protected function _massageAggregateOptions ($ options ) {
741
+ protected function _massageAggregateOptions ($ options )
742
+ {
718
743
if ($ options ["useCursor " ]) {
719
744
$ options ["cursor " ] = array ("batchSize " => $ options ["batchSize " ]);
720
745
}
@@ -733,7 +758,8 @@ protected function _massageAggregateOptions($options) {
733
758
* @param array $options Additional options
734
759
* @return array The original document
735
760
*/
736
- function findOneAndDelete (array $ filter , array $ options = array ()) {
761
+ public function findOneAndDelete (array $ filter , array $ options = array ())
762
+ {
737
763
$ options = array_merge ($ this ->getFindOneAndDeleteOptions (), $ options );
738
764
$ options = $ this ->_massageFindAndModifyOptions ($ options );
739
765
$ cmd = array (
@@ -754,7 +780,8 @@ function findOneAndDelete(array $filter, array $options = array()) {
754
780
*
755
781
* @return array of MongoDB\Collection::findOneAndDelete() options
756
782
*/
757
- function getFindOneAndDeleteOptions () {
783
+ public function getFindOneAndDeleteOptions ()
784
+ {
758
785
return array (
759
786
760
787
/**
@@ -794,7 +821,8 @@ function getFindOneAndDeleteOptions() {
794
821
* @param array $options Additional options
795
822
* @return array
796
823
*/
797
- function findOneAndReplace (array $ filter , array $ replacement , array $ options = array ()) {
824
+ public function findOneAndReplace (array $ filter , array $ replacement , array $ options = array ())
825
+ {
798
826
if (key ($ replacement )[0 ] == '$ ' ) {
799
827
throw new \InvalidArgumentException ("First key in \$replacement must NOT be a \$operator " );
800
828
}
@@ -820,7 +848,8 @@ function findOneAndReplace(array $filter, array $replacement, array $options = a
820
848
*
821
849
* @return array of MongoDB\Collection::findOneAndReplace() options
822
850
*/
823
- function getFindOneAndReplaceOptions () {
851
+ public function getFindOneAndReplaceOptions ()
852
+ {
824
853
return array (
825
854
826
855
/**
@@ -860,7 +889,6 @@ function getFindOneAndReplaceOptions() {
860
889
*/
861
890
"upsert " => false ,
862
891
);
863
-
864
892
}
865
893
866
894
/**
@@ -878,7 +906,8 @@ function getFindOneAndReplaceOptions() {
878
906
* @param array $options Additional options
879
907
* @return array
880
908
*/
881
- function findOneAndUpdate (array $ filter , array $ update , array $ options = array ()) {
909
+ public function findOneAndUpdate (array $ filter , array $ update , array $ options = array ())
910
+ {
882
911
if (key ($ update )[0 ] != '$ ' ) {
883
912
throw new \InvalidArgumentException ("First key in \$update must be a \$operator " );
884
913
}
@@ -904,7 +933,8 @@ function findOneAndUpdate(array $filter, array $update, array $options = array()
904
933
*
905
934
* @return array of MongoDB\Collection::findOneAndUpdate() options
906
935
*/
907
- function getFindOneAndUpdateOptions () {
936
+ public function getFindOneAndUpdateOptions ()
937
+ {
908
938
return array (
909
939
910
940
/**
@@ -943,14 +973,14 @@ function getFindOneAndUpdateOptions() {
943
973
*/
944
974
"upsert " => false ,
945
975
);
946
-
947
976
}
948
977
949
978
/**
950
979
* Internal helper for massaging findandmodify options
951
980
* @internal
952
981
*/
953
- final protected function _massageFindAndModifyOptions ($ options , $ update = array ()) {
982
+ final protected function _massageFindAndModifyOptions ($ options , $ update = array ())
983
+ {
954
984
$ ret = array (
955
985
"sort " => $ options ["sort " ],
956
986
"new " => isset ($ options ["returnDocument " ]) ? $ options ["returnDocument " ] == self ::FIND_ONE_AND_RETURN_AFTER : false ,
@@ -970,7 +1000,8 @@ final protected function _massageFindAndModifyOptions($options, $update = array(
970
1000
* Internal helper for throwing an exception with error message
971
1001
* @internal
972
1002
*/
973
- final protected function _generateCommandException ($ doc ) {
1003
+ final protected function _generateCommandException ($ doc )
1004
+ {
974
1005
if ($ doc ["errmsg " ]) {
975
1006
return new Exception ($ doc ["errmsg " ]);
976
1007
}
@@ -982,7 +1013,8 @@ final protected function _generateCommandException($doc) {
982
1013
* Internal helper for running a command
983
1014
* @internal
984
1015
*/
985
- final protected function _runCommand ($ dbname , array $ cmd , ReadPreference $ rp = null ) {
1016
+ final protected function _runCommand ($ dbname , array $ cmd , ReadPreference $ rp = null )
1017
+ {
986
1018
//var_dump(\BSON\toJSON(\BSON\fromArray($cmd)));
987
1019
$ command = new Command ($ cmd );
988
1020
return $ this ->manager ->executeCommand ($ dbname , $ command , $ rp );
@@ -993,7 +1025,8 @@ final protected function _runCommand($dbname, array $cmd, ReadPreference $rp = n
993
1025
*
994
1026
* @return string
995
1027
*/
996
- function getCollectionName () {
1028
+ public function getCollectionName ()
1029
+ {
997
1030
return $ this ->collname ;
998
1031
}
999
1032
@@ -1002,8 +1035,8 @@ function getCollectionName() {
1002
1035
*
1003
1036
* @return string
1004
1037
*/
1005
- function getDatabaseName () {
1038
+ public function getDatabaseName ()
1039
+ {
1006
1040
return $ this ->dbname ;
1007
1041
}
1008
1042
}
1009
-
0 commit comments