File tree Expand file tree Collapse file tree 4 files changed +120
-1
lines changed Expand file tree Collapse file tree 4 files changed +120
-1
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace MongoDB \Benchmark \Fixtures ;
4
+
5
+ use MongoDB \BSON \Document ;
6
+ use MongoDB \Codec \DecodeIfSupported ;
7
+ use MongoDB \Codec \DocumentCodec ;
8
+ use MongoDB \Codec \EncodeIfSupported ;
9
+ use MongoDB \Exception \UnsupportedValueException ;
10
+
11
+ final class PassThruCodec implements DocumentCodec
12
+ {
13
+ use DecodeIfSupported;
14
+ use EncodeIfSupported;
15
+
16
+ public function canDecode ($ value ): bool
17
+ {
18
+ return $ value instanceof Document;
19
+ }
20
+
21
+ public function canEncode ($ value ): bool
22
+ {
23
+ return $ value instanceof Document;
24
+ }
25
+
26
+ public function decode ($ value ): object
27
+ {
28
+ if (! $ value instanceof Document) {
29
+ throw UnsupportedValueException::invalidDecodableValue ($ value );
30
+ }
31
+
32
+ return $ value ;
33
+ }
34
+
35
+ public function encode ($ value ): Document
36
+ {
37
+ if (! $ value instanceof Document) {
38
+ throw UnsupportedValueException::invalidEncodableValue ($ value );
39
+ }
40
+
41
+ return $ value ;
42
+ }
43
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace MongoDB \Benchmark \Fixtures ;
4
+
5
+ use MongoDB \BSON \Document ;
6
+ use MongoDB \Codec \DecodeIfSupported ;
7
+ use MongoDB \Codec \DocumentCodec ;
8
+ use MongoDB \Codec \EncodeIfSupported ;
9
+ use MongoDB \Exception \UnsupportedValueException ;
10
+ use function is_object ;
11
+
12
+ final class ToObjectCodec implements DocumentCodec
13
+ {
14
+ use DecodeIfSupported;
15
+ use EncodeIfSupported;
16
+
17
+ public function canDecode ($ value ): bool
18
+ {
19
+ return $ value instanceof Document;
20
+ }
21
+
22
+ public function canEncode ($ value ): bool
23
+ {
24
+ return is_object ($ value );
25
+ }
26
+
27
+ public function decode ($ value ): object
28
+ {
29
+ if (! $ value instanceof Document) {
30
+ throw UnsupportedValueException::invalidDecodableValue ($ value );
31
+ }
32
+
33
+ return $ value ->toPHP (['root ' => 'stdClass ' , 'array ' => 'array ' , 'document ' => 'stdClass ' ]);
34
+ }
35
+
36
+ public function encode ($ value ): Document
37
+ {
38
+ if (! is_object ($ value )) {
39
+ throw UnsupportedValueException::invalidEncodableValue ($ value );
40
+ }
41
+
42
+ return Document::fromPHP ($ value );
43
+ }
44
+ }
Original file line number Diff line number Diff line change 3
3
namespace MongoDB \Benchmark ;
4
4
5
5
use Generator ;
6
+ use MongoDB \Benchmark \Fixtures \PassThruCodec ;
7
+ use MongoDB \Benchmark \Fixtures \ToObjectCodec ;
6
8
use MongoDB \BSON \Document ;
9
+ use MongoDB \Codec \DecodeIfSupported ;
10
+ use MongoDB \Codec \DocumentCodec ;
11
+ use MongoDB \Codec \EncodeIfSupported ;
7
12
use MongoDB \Exception \InvalidArgumentException ;
13
+ use MongoDB \Exception \UnsupportedValueException ;
8
14
use MongoDB \Model \BSONArray ;
9
15
use MongoDB \Model \BSONDocument ;
10
16
use PhpBench \Attributes \BeforeClassMethods ;
@@ -40,7 +46,7 @@ public function provideParams(): Generator
40
46
{
41
47
yield 'Driver default typemap ' => [
42
48
'codec ' => null ,
43
- 'typeMap ' => [] ,
49
+ 'typeMap ' => null ,
44
50
'accessor ' => 'object ' ,
45
51
];
46
52
@@ -65,6 +71,18 @@ public function provideParams(): Generator
65
71
'typeMap ' => ['root ' => 'bson ' ],
66
72
'accessor ' => 'bson ' ,
67
73
];
74
+
75
+ yield 'Codec (pass thru) ' => [
76
+ 'codec ' => new PassThruCodec (),
77
+ 'typeMap ' => null ,
78
+ 'accessor ' => 'bson ' ,
79
+ ];
80
+
81
+ yield 'Codec (to array) ' => [
82
+ 'codec ' => new ToObjectCodec (),
83
+ 'typeMap ' => null ,
84
+ 'accessor ' => 'object ' ,
85
+ ];
68
86
}
69
87
70
88
#[ParamProviders('provideParams ' )]
Original file line number Diff line number Diff line change 3
3
namespace MongoDB \Benchmark ;
4
4
5
5
use Generator ;
6
+ use MongoDB \Benchmark \Fixtures \PassThruCodec ;
7
+ use MongoDB \Benchmark \Fixtures \ToObjectCodec ;
6
8
use MongoDB \BSON \Document ;
7
9
use MongoDB \Exception \InvalidArgumentException ;
8
10
use MongoDB \Model \BSONArray ;
@@ -65,6 +67,18 @@ public function provideParams(): Generator
65
67
'typeMap ' => ['root ' => 'bson ' ],
66
68
'accessor ' => 'bson ' ,
67
69
];
70
+
71
+ yield 'Codec (pass thru) ' => [
72
+ 'codec ' => new PassThruCodec (),
73
+ 'typeMap ' => null ,
74
+ 'accessor ' => 'bson ' ,
75
+ ];
76
+
77
+ yield 'Codec (to array) ' => [
78
+ 'codec ' => new ToObjectCodec (),
79
+ 'typeMap ' => null ,
80
+ 'accessor ' => 'object ' ,
81
+ ];
68
82
}
69
83
70
84
#[ParamProviders('provideParams ' )]
You can’t perform that action at this time.
0 commit comments