File tree Expand file tree Collapse file tree 4 files changed +125
-1
lines changed Expand file tree Collapse file tree 4 files changed +125
-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
+ /** @param mixed $value */
17
+ public function canDecode ($ value ): bool
18
+ {
19
+ return $ value instanceof Document;
20
+ }
21
+
22
+ /** @param mixed $value */
23
+ public function canEncode ($ value ): bool
24
+ {
25
+ return $ value instanceof Document;
26
+ }
27
+
28
+ /** @param mixed $value */
29
+ public function decode ($ value ): Document
30
+ {
31
+ if (! $ value instanceof Document) {
32
+ throw UnsupportedValueException::invalidDecodableValue ($ value );
33
+ }
34
+
35
+ return $ value ;
36
+ }
37
+
38
+ /** @param mixed $value */
39
+ public function encode ($ value ): Document
40
+ {
41
+ if (! $ value instanceof Document) {
42
+ throw UnsupportedValueException::invalidEncodableValue ($ value );
43
+ }
44
+
45
+ return $ value ;
46
+ }
47
+ }
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
+ use function is_object ;
12
+
13
+ final class ToObjectCodec implements DocumentCodec
14
+ {
15
+ use DecodeIfSupported;
16
+ use EncodeIfSupported;
17
+
18
+ /** @param mixed $value */
19
+ public function canDecode ($ value ): bool
20
+ {
21
+ return $ value instanceof Document;
22
+ }
23
+
24
+ /** @param mixed $value */
25
+ public function canEncode ($ value ): bool
26
+ {
27
+ return is_object ($ value );
28
+ }
29
+
30
+ /** @param mixed $value */
31
+ public function decode ($ value ): object
32
+ {
33
+ if (! $ value instanceof Document) {
34
+ throw UnsupportedValueException::invalidDecodableValue ($ value );
35
+ }
36
+
37
+ return $ value ->toPHP (['root ' => 'stdClass ' , 'array ' => 'array ' , 'document ' => 'stdClass ' ]);
38
+ }
39
+
40
+ /** @param mixed $value */
41
+ public function encode ($ value ): Document
42
+ {
43
+ if (! is_object ($ value )) {
44
+ throw UnsupportedValueException::invalidEncodableValue ($ value );
45
+ }
46
+
47
+ return Document::fromPHP ($ value );
48
+ }
49
+ }
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 ;
@@ -40,7 +42,7 @@ public function provideParams(): Generator
40
42
{
41
43
yield 'Driver default typemap ' => [
42
44
'codec ' => null ,
43
- 'typeMap ' => [] ,
45
+ 'typeMap ' => null ,
44
46
'accessor ' => 'object ' ,
45
47
];
46
48
@@ -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 object) ' => [
78
+ 'codec ' => new ToObjectCodec (),
79
+ 'typeMap ' => null ,
80
+ 'accessor ' => 'object ' ,
81
+ ];
68
82
}
69
83
70
84
#[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 object) ' => [
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