This repository was archived by the owner on Dec 23, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +44
-5
lines changed Expand file tree Collapse file tree 3 files changed +44
-5
lines changed Original file line number Diff line number Diff line change @@ -1327,7 +1327,14 @@ public function serialize() : string
1327
1327
1328
1328
public function jsonSerialize () : mixed
1329
1329
{
1330
- return $this -> code ;
1330
+ $json = [ ' $code' => $this -> code ];
1331
+
1332
+ if ( isset ( $this -> scope ) )
1333
+ {
1334
+ $json [' $scope' ] = $this -> scope ;
1335
+ }
1336
+
1337
+ return $json ;
1331
1338
}
1332
1339
1333
1340
public function unserialize (mixed $serialized ) : void
Original file line number Diff line number Diff line change
1
+ --TEST--
2
+ JsonSerializable: Javascript (with scope)
3
+ --FILE--
4
+ <?php
5
+ $ doc = [
6
+ 'foo ' => new MongoDB \BSON \Javascript ( "function foo(bar) { return bar; } " , [ 'foo ' => 42 ] )
7
+ ];
8
+
9
+ echo MongoDB \BSON \toJSON ( \MongoDB \BSON \fromPHP ( $ doc ) ), "\n" ;
10
+ $ d = json_encode ( $ doc );
11
+ echo $ d , "\n" ;
12
+
13
+ var_dump ( \MongoDB \BSON \toPHP ( \MongoDB \BSON \fromJSON ( $ d ) ) );
14
+ ?>
15
+ --EXPECTF--
16
+ { "foo" : "function foo(bar) { return bar; }" }
17
+ {"foo":{"$code":"function foo(bar) { return bar; }","$scope":{"foo":42}}}
18
+ object(stdClass)#%d (%d) {
19
+ ["foo"]=>
20
+ object(stdClass)#%d (%d) {
21
+ ["$code"]=>
22
+ string(33) "function foo(bar) { return bar; }"
23
+ ["$scope"]=>
24
+ object(stdClass)#%d (%d) {
25
+ ["foo"]=>
26
+ int(42)
27
+ }
28
+ }
29
+ }
Original file line number Diff line number Diff line change 1
1
--TEST--
2
- JsonSerializable: Javascript
2
+ JsonSerializable: Javascript (without scope)
3
3
--FILE--
4
4
<?php
5
5
$ doc = [
6
- 'foo ' => new MongoDB \BSON \Javascript ( "function foo(bar) { return bar; } " , [ ' foo ' => 42 ] )
6
+ 'foo ' => new MongoDB \BSON \Javascript ( "function foo(bar) { return bar; } " )
7
7
];
8
8
9
9
echo MongoDB \BSON \toJSON ( \MongoDB \BSON \fromPHP ( $ doc ) ), "\n" ;
@@ -14,8 +14,11 @@ var_dump( \MongoDB\BSON\toPHP( \MongoDB\BSON\fromJSON( $d ) ) );
14
14
?>
15
15
--EXPECTF--
16
16
{ "foo" : "function foo(bar) { return bar; }" }
17
- {"foo":" function foo(bar) { return bar; }"}
17
+ {"foo":{"$code":" function foo(bar) { return bar; }"} }
18
18
object(stdClass)#%d (%d) {
19
19
["foo"]=>
20
- string(33) "function foo(bar) { return bar; }"
20
+ object(stdClass)#%d (%d) {
21
+ ["$code"]=>
22
+ string(33) "function foo(bar) { return bar; }"
23
+ }
21
24
}
You can’t perform that action at this time.
0 commit comments