1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
--TEST--
BSON encoding: Encoding objects into BSON representation
--FILE--
<?php
require_once __DIR__ . '/../utils/tools.php';
class MyClass implements MongoDB\BSON\Persistable {
function bsonSerialize() {
return array(
"random" => "class",
"data"
);
}
function bsonUnserialize(array $data) {
$this->props = $data;
}
}
class MyClass2 implements MongoDB\BSON\Persistable {
function bsonSerialize() {
return array(
1, 2, 3,
);
}
function bsonUnserialize(array $data) {
$this->props = $data;
}
}
$tests = array(
array("stuff" => new MyClass),
array("stuff" => new MyClass2),
array("stuff" => array(new MyClass, new MyClass2)),
);
foreach($tests as $n => $test) {
$s = fromPHP($test);
echo "Test#{$n} ", toJSON($s), "\n";
hex_dump($s);
$ret = toPHP($s);
var_dump($ret);
}
?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
Test#0 { "stuff" : { "__pclass" : { "$binary" : "TXlDbGFzcw==", "$type" : "80" }, "random" : "class", "0" : "data" } }
0 : 45 00 00 00 03 73 74 75 66 66 00 39 00 00 00 05 [E....stuff.9....]
10 : 5f 5f 70 63 6c 61 73 73 00 07 00 00 00 80 4d 79 [__pclass......My]
20 : 43 6c 61 73 73 02 72 61 6e 64 6f 6d 00 06 00 00 [Class.random....]
30 : 00 63 6c 61 73 73 00 02 30 00 05 00 00 00 64 61 [.class..0.....da]
40 : 74 61 00 00 00 [ta...]
object(stdClass)#%d (1) {
["stuff"]=>
object(MyClass)#%d (1) {
["props"]=>
array(3) {
["__pclass"]=>
object(MongoDB\BSON\Binary)#%d (2) {
["data"]=>
string(7) "MyClass"
["type"]=>
int(128)
}
["random"]=>
string(5) "class"
[0]=>
string(4) "data"
}
}
}
Test#1 { "stuff" : { "__pclass" : { "$binary" : "TXlDbGFzczI=", "$type" : "80" }, "0" : 1, "1" : 2, "2" : 3 } }
0 : 3d 00 00 00 03 73 74 75 66 66 00 31 00 00 00 05 [=....stuff.1....]
10 : 5f 5f 70 63 6c 61 73 73 00 08 00 00 00 80 4d 79 [__pclass......My]
20 : 43 6c 61 73 73 32 10 30 00 01 00 00 00 10 31 00 [Class2.0......1.]
30 : 02 00 00 00 10 32 00 03 00 00 00 00 00 [.....2.......]
object(stdClass)#%d (1) {
["stuff"]=>
object(MyClass2)#%d (1) {
["props"]=>
array(4) {
["__pclass"]=>
object(MongoDB\BSON\Binary)#%d (2) {
["data"]=>
string(8) "MyClass2"
["type"]=>
int(128)
}
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
}
}
Test#2 { "stuff" : [ { "__pclass" : { "$binary" : "TXlDbGFzcw==", "$type" : "80" }, "random" : "class", "0" : "data" }, { "__pclass" : { "$binary" : "TXlDbGFzczI=", "$type" : "80" }, "0" : 1, "1" : 2, "2" : 3 } ] }
0 : 81 00 00 00 04 73 74 75 66 66 00 75 00 00 00 03 [.....stuff.u....]
10 : 30 00 39 00 00 00 05 5f 5f 70 63 6c 61 73 73 00 [0.9....__pclass.]
20 : 07 00 00 00 80 4d 79 43 6c 61 73 73 02 72 61 6e [.....MyClass.ran]
30 : 64 6f 6d 00 06 00 00 00 63 6c 61 73 73 00 02 30 [dom.....class..0]
40 : 00 05 00 00 00 64 61 74 61 00 00 03 31 00 31 00 [.....data...1.1.]
50 : 00 00 05 5f 5f 70 63 6c 61 73 73 00 08 00 00 00 [...__pclass.....]
60 : 80 4d 79 43 6c 61 73 73 32 10 30 00 01 00 00 00 [.MyClass2.0.....]
70 : 10 31 00 02 00 00 00 10 32 00 03 00 00 00 00 00 [.1......2.......]
80 : 00 [.]
object(stdClass)#%d (1) {
["stuff"]=>
array(2) {
[0]=>
object(MyClass)#%d (1) {
["props"]=>
array(3) {
["__pclass"]=>
object(MongoDB\BSON\Binary)#%d (2) {
["data"]=>
string(7) "MyClass"
["type"]=>
int(128)
}
["random"]=>
string(5) "class"
[0]=>
string(4) "data"
}
}
[1]=>
object(MyClass2)#%d (1) {
["props"]=>
array(4) {
["__pclass"]=>
object(MongoDB\BSON\Binary)#%d (2) {
["data"]=>
string(8) "MyClass2"
["type"]=>
int(128)
}
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
}
}
}
===DONE===
|