Skip to content

Commit 81de2ca

Browse files
committed
PHPC-210 & PHPC-209 & PHPC-207 Fix BSON ODM/ODS
1 parent 86d3a8f commit 81de2ca

File tree

4 files changed

+25
-26
lines changed

4 files changed

+25
-26
lines changed

src/bson.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ bool php_phongo_bson_visit_document(const bson_iter_t *iter ARG_UNUSED, const ch
470470
array_init(state.zchild);
471471

472472
if (!bson_iter_visit_all(&child, &php_bson_visitors, &state)) {
473-
if (state.map.document || state.odm) {
473+
if ((state.map.document || state.odm) && instanceof_function(state.odm ? state.odm : state.map.document, php_phongo_unserializable_ce TSRMLS_CC)) {
474474
zval *obj = NULL;
475475

476476
MAKE_STD_ZVAL(obj);
@@ -506,11 +506,11 @@ bool php_phongo_bson_visit_array(const bson_iter_t *iter ARG_UNUSED, const char
506506

507507
if (!bson_iter_visit_all(&child, &php_bson_visitors, &state)) {
508508

509-
if (state.map.array || state.odm) {
509+
if (state.map.array && instanceof_function(state.map.array, php_phongo_unserializable_ce TSRMLS_CC)) {
510510
zval *obj = NULL;
511511

512512
MAKE_STD_ZVAL(obj);
513-
object_init_ex(obj, state.odm ? state.odm : state.map.array);
513+
object_init_ex(obj, state.map.array);
514514
zend_call_method_with_1_params(&obj, NULL, NULL, BSON_UNSERIALIZE_FUNC_NAME, NULL, state.zchild);
515515
add_assoc_zval(retval, key, obj);
516516
zval_ptr_dtor(&state.zchild);
@@ -595,12 +595,12 @@ void object_to_bson(zval *object, const char *key, long key_len, bson_t *bson TS
595595
tmp_ht->nApplyCount++;
596596
}
597597

598-
bson_append_array_begin(bson, key, key_len, &child);
598+
bson_append_document_begin(bson, key, key_len, &child);
599599
if (instanceof_function(Z_OBJCE_P(object), php_phongo_persistable_ce TSRMLS_CC)) {
600600
bson_append_binary(&child, PHONGO_ODM_FIELD_NAME, -1, 0x80, (const uint8_t *)Z_OBJCE_P(object)->name, strlen(Z_OBJCE_P(object)->name));
601601
}
602602
zval_to_bson(retval, PHONGO_BSON_NONE, &child, NULL TSRMLS_CC);
603-
bson_append_array_end(bson, &child);
603+
bson_append_document_end(bson, &child);
604604

605605
if (tmp_ht) {
606606
tmp_ht->nApplyCount--;
@@ -842,7 +842,7 @@ int bson_to_zval(const unsigned char *data, int data_len, php_phongo_bson_state
842842

843843
array_init(state->zchild);
844844
bson_iter_visit_all(&iter, &php_bson_visitors, state);
845-
if (state->map.array || state->odm) {
845+
if ((state->map.array || state->odm) && instanceof_function(state->odm ? state->odm : state->map.array, php_phongo_unserializable_ce TSRMLS_CC)) {
846846
zval *obj = NULL;
847847

848848
MAKE_STD_ZVAL(obj);

tests/bson/bson-encode-002.phpt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@ foreach($tests as $n => $test) {
3737
$s = BSON\fromArray($test);
3838
echo "Test#{$n} ", BSON\toJSON($s), "\n";
3939
hex_dump($s);
40-
BSON\toArray($s, array("array" => get_class($test["stuff"])));
40+
$obj = BSON\toArray($s, array("document" => get_class($test["stuff"])));
41+
var_dump($obj);
4142
}
4243
?>
4344
===DONE===
4445
<?php exit(0); ?>
4546
--EXPECTF--
46-
Test#0 { "stuff" : [ "class", "data" ] }
47-
0 : 2f 00 00 00 04 73 74 75 66 66 00 23 00 00 00 02 [/....stuff.#....]
47+
Test#0 { "stuff" : { "random" : "class", "0" : "data" } }
48+
0 : 2f 00 00 00 03 73 74 75 66 66 00 23 00 00 00 02 [/....stuff.#....]
4849
10 : 72 61 6e 64 6f 6d 00 06 00 00 00 63 6c 61 73 73 [random.....class]
4950
20 : 00 02 30 00 05 00 00 00 64 61 74 61 00 00 00 [..0.....data...]
5051
string(24) "MyClass::bsonUnserialize"
@@ -54,14 +55,13 @@ array(2) {
5455
[0]=>
5556
string(4) "data"
5657
}
57-
string(24) "MyClass::bsonUnserialize"
5858
array(1) {
5959
["stuff"]=>
6060
object(MyClass)#%d (0) {
6161
}
6262
}
63-
Test#1 { "stuff" : [ 1, 2, 3 ] }
64-
0 : 26 00 00 00 04 73 74 75 66 66 00 1a 00 00 00 10 [&....stuff......]
63+
Test#1 { "stuff" : { "0" : 1, "1" : 2, "2" : 3 } }
64+
0 : 26 00 00 00 03 73 74 75 66 66 00 1a 00 00 00 10 [&....stuff......]
6565
10 : 30 00 01 00 00 00 10 31 00 02 00 00 00 10 32 00 [0......1......2.]
6666
20 : 03 00 00 00 00 00 [......]
6767
string(25) "MyClass2::bsonUnserialize"
@@ -73,7 +73,6 @@ array(3) {
7373
[2]=>
7474
int(3)
7575
}
76-
string(25) "MyClass2::bsonUnserialize"
7776
array(1) {
7877
["stuff"]=>
7978
object(MyClass2)#%d (0) {

tests/bson/bson-encode-003.phpt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ foreach($tests as $n => $test) {
4545
===DONE===
4646
<?php exit(0); ?>
4747
--EXPECT--
48-
Test#0 { "stuff" : [ { "$type" : "80", "$binary" : "TXlDbGFzcw==" }, "class", "data" ] }
49-
0 : 45 00 00 00 04 73 74 75 66 66 00 39 00 00 00 05 [E....stuff.9....]
48+
Test#0 { "stuff" : { "__pclass" : { "$type" : "80", "$binary" : "TXlDbGFzcw==" }, "random" : "class", "0" : "data" } }
49+
0 : 45 00 00 00 03 73 74 75 66 66 00 39 00 00 00 05 [E....stuff.9....]
5050
10 : 5f 5f 70 63 6c 61 73 73 00 07 00 00 00 80 4d 79 [__pclass......My]
5151
20 : 43 6c 61 73 73 02 72 61 6e 64 6f 6d 00 06 00 00 [Class.random....]
5252
30 : 00 63 6c 61 73 73 00 02 30 00 05 00 00 00 64 61 [.class..0.....da]
@@ -63,8 +63,8 @@ array(1) {
6363
}
6464
}
6565
}
66-
Test#1 { "stuff" : [ { "$type" : "80", "$binary" : "TXlDbGFzczI=" }, 1, 2, 3 ] }
67-
0 : 3d 00 00 00 04 73 74 75 66 66 00 31 00 00 00 05 [=....stuff.1....]
66+
Test#1 { "stuff" : { "__pclass" : { "$type" : "80", "$binary" : "TXlDbGFzczI=" }, "0" : 1, "1" : 2, "2" : 3 } }
67+
0 : 3d 00 00 00 03 73 74 75 66 66 00 31 00 00 00 05 [=....stuff.1....]
6868
10 : 5f 5f 70 63 6c 61 73 73 00 08 00 00 00 80 4d 79 [__pclass......My]
6969
20 : 43 6c 61 73 73 32 10 30 00 01 00 00 00 10 31 00 [Class2.0......1.]
7070
30 : 02 00 00 00 10 32 00 03 00 00 00 00 00 [.....2.......]
@@ -82,12 +82,12 @@ array(1) {
8282
}
8383
}
8484
}
85-
Test#2 { "stuff" : [ [ { "$type" : "80", "$binary" : "TXlDbGFzcw==" }, "class", "data" ], [ { "$type" : "80", "$binary" : "TXlDbGFzczI=" }, 1, 2, 3 ] ] }
86-
0 : 81 00 00 00 04 73 74 75 66 66 00 75 00 00 00 04 [.....stuff.u....]
85+
Test#2 { "stuff" : [ { "__pclass" : { "$type" : "80", "$binary" : "TXlDbGFzcw==" }, "random" : "class", "0" : "data" }, { "__pclass" : { "$type" : "80", "$binary" : "TXlDbGFzczI=" }, "0" : 1, "1" : 2, "2" : 3 } ] }
86+
0 : 81 00 00 00 04 73 74 75 66 66 00 75 00 00 00 03 [.....stuff.u....]
8787
10 : 30 00 39 00 00 00 05 5f 5f 70 63 6c 61 73 73 00 [0.9....__pclass.]
8888
20 : 07 00 00 00 80 4d 79 43 6c 61 73 73 02 72 61 6e [.....MyClass.ran]
8989
30 : 64 6f 6d 00 06 00 00 00 63 6c 61 73 73 00 02 30 [dom.....class..0]
90-
40 : 00 05 00 00 00 64 61 74 61 00 00 04 31 00 31 00 [.....data...1.1.]
90+
40 : 00 05 00 00 00 64 61 74 61 00 00 03 31 00 31 00 [.....data...1.1.]
9191
50 : 00 00 05 5f 5f 70 63 6c 61 73 73 00 08 00 00 00 [...__pclass.....]
9292
60 : 80 4d 79 43 6c 61 73 73 32 10 30 00 01 00 00 00 [.MyClass2.0.....]
9393
70 : 10 31 00 02 00 00 00 10 32 00 03 00 00 00 00 00 [.1......2.......]

tests/bson/bson-encode-004.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,28 +72,28 @@ object(Person)#%d (5) {
7272
["secret":protected]=>
7373
string(24) "Hannes confidential info"
7474
}
75-
Test { "0" : [ { "$type" : "80", "$binary" : "UGVyc29u" }, "Hannes", 42, [ [ { "$type" : "80", "$binary" : "QWRkcmVzcw==" }, 94086, "USA" ], [ { "$type" : "80", "$binary" : "QWRkcmVzcw==" }, 200, "Iceland" ] ], [ [ { "$type" : "80", "$binary" : "UGVyc29u" }, "Jeremy", 21, { }, { } ] ] ] }
76-
0 : 1f 01 00 00 04 30 00 17 01 00 00 05 5f 5f 70 63 [.....0......__pc]
75+
Test { "0" : { "__pclass" : { "$type" : "80", "$binary" : "UGVyc29u" }, "name" : "Hannes", "age" : 42, "address" : [ { "__pclass" : { "$type" : "80", "$binary" : "QWRkcmVzcw==" }, "zip" : 94086, "country" : "USA" }, { "__pclass" : { "$type" : "80", "$binary" : "QWRkcmVzcw==" }, "zip" : 200, "country" : "Iceland" } ], "friends" : [ { "__pclass" : { "$type" : "80", "$binary" : "UGVyc29u" }, "name" : "Jeremy", "age" : 21, "address" : { }, "friends" : { } } ] } }
76+
0 : 1f 01 00 00 03 30 00 17 01 00 00 05 5f 5f 70 63 [.....0......__pc]
7777
10 : 6c 61 73 73 00 06 00 00 00 80 50 65 72 73 6f 6e [lass......Person]
7878
20 : 02 6e 61 6d 65 00 07 00 00 00 48 61 6e 6e 65 73 [.name.....Hannes]
7979
30 : 00 10 61 67 65 00 2a 00 00 00 04 61 64 64 72 65 [..age.*....addre]
80-
40 : 73 73 00 79 00 00 00 04 30 00 35 00 00 00 05 5f [ss.y....0.5...._]
80+
40 : 73 73 00 79 00 00 00 03 30 00 35 00 00 00 05 5f [ss.y....0.5...._]
8181
50 : 5f 70 63 6c 61 73 73 00 07 00 00 00 80 41 64 64 [_pclass......Add]
8282
60 : 72 65 73 73 10 7a 69 70 00 86 6f 01 00 02 63 6f [ress.zip..o...co]
83-
70 : 75 6e 74 72 79 00 04 00 00 00 55 53 41 00 00 04 [untry.....USA...]
83+
70 : 75 6e 74 72 79 00 04 00 00 00 55 53 41 00 00 03 [untry.....USA...]
8484
80 : 31 00 39 00 00 00 05 5f 5f 70 63 6c 61 73 73 00 [1.9....__pclass.]
8585
90 : 07 00 00 00 80 41 64 64 72 65 73 73 10 7a 69 70 [.....Address.zip]
8686
A0 : 00 c8 00 00 00 02 63 6f 75 6e 74 72 79 00 08 00 [......country...]
8787
B0 : 00 00 49 63 65 6c 61 6e 64 00 00 00 04 66 72 69 [..Iceland....fri]
88-
C0 : 65 6e 64 73 00 58 00 00 00 04 30 00 50 00 00 00 [ends.X....0.P...]
88+
C0 : 65 6e 64 73 00 58 00 00 00 03 30 00 50 00 00 00 [ends.X....0.P...]
8989
D0 : 05 5f 5f 70 63 6c 61 73 73 00 06 00 00 00 80 50 [.__pclass......P]
9090
E0 : 65 72 73 6f 6e 02 6e 61 6d 65 00 07 00 00 00 4a [erson.name.....J]
9191
F0 : 65 72 65 6d 79 00 10 61 67 65 00 15 00 00 00 03 [eremy..age......]
9292
100 : 61 64 64 72 65 73 73 00 05 00 00 00 00 03 66 72 [address.......fr]
9393
110 : 69 65 6e 64 73 00 05 00 00 00 00 00 00 00 00 [iends..........]
9494
array(1) {
9595
[0]=>
96-
object(Person)#%d2 (5) {
96+
object(Person)#%d (5) {
9797
["name":protected]=>
9898
string(6) "Hannes"
9999
["age":protected]=>

0 commit comments

Comments
 (0)