1
1
<?php
2
2
3
+ declare (strict_types=1 );
4
+
3
5
namespace MongoDB \Laravel ;
4
6
5
7
use Exception ;
6
8
use MongoDB \BSON \ObjectID ;
7
9
use MongoDB \Collection as MongoCollection ;
8
10
9
- /**
10
- * @mixin MongoCollection
11
- */
11
+ use function array_walk_recursive ;
12
+ use function implode ;
13
+ use function json_encode ;
14
+ use function microtime ;
15
+
16
+ /** @mixin MongoCollection */
12
17
class Collection
13
18
{
14
19
/**
@@ -25,10 +30,6 @@ class Collection
25
30
*/
26
31
protected $ collection ;
27
32
28
- /**
29
- * @param Connection $connection
30
- * @param MongoCollection $collection
31
- */
32
33
public function __construct (Connection $ connection , MongoCollection $ collection )
33
34
{
34
35
$ this ->connection = $ connection ;
@@ -38,13 +39,14 @@ public function __construct(Connection $connection, MongoCollection $collection)
38
39
/**
39
40
* Handle dynamic method calls.
40
41
*
41
- * @param string $method
42
+ * @param string $method
42
43
* @param array $parameters
44
+ *
43
45
* @return mixed
44
46
*/
45
47
public function __call (string $ method , array $ parameters )
46
48
{
47
- $ start = microtime (true );
49
+ $ start = microtime (true );
48
50
$ result = $ this ->collection ->$ method (...$ parameters );
49
51
50
52
// Once we have run the query we will calculate the time that it took to run and
@@ -56,21 +58,23 @@ public function __call(string $method, array $parameters)
56
58
57
59
// Convert the query parameters to a json string.
58
60
array_walk_recursive ($ parameters , function (&$ item , $ key ) {
59
- if ($ item instanceof ObjectID) {
60
- $ item = ( string ) $ item ;
61
+ if (! ( $ item instanceof ObjectID) ) {
62
+ return ;
61
63
}
64
+
65
+ $ item = (string ) $ item ;
62
66
});
63
67
64
68
// Convert the query parameters to a json string.
65
69
foreach ($ parameters as $ parameter ) {
66
70
try {
67
71
$ query [] = json_encode ($ parameter );
68
- } catch (Exception $ e ) {
72
+ } catch (Exception ) {
69
73
$ query [] = '{...} ' ;
70
74
}
71
75
}
72
76
73
- $ queryString = $ this ->collection ->getCollectionName (). '. ' . $ method. '( ' . implode (', ' , $ query ). ') ' ;
77
+ $ queryString = $ this ->collection ->getCollectionName () . '. ' . $ method . '( ' . implode (', ' , $ query ) . ') ' ;
74
78
75
79
$ this ->connection ->logQuery ($ queryString , [], $ time );
76
80
0 commit comments