Skip to content

Commit fc5e3a4

Browse files
alcaeusjmikola
andauthored
PHPC-2222: Ensure packed arrays return PackedArray instances when converted to BSON (#1421)
* PHPC-2222: Ensure packed arrays return PackedArray instances when converted to BSON * Fix wrong macro usage to access bson object Co-authored-by: Jeremy Mikola <[email protected]> --------- Co-authored-by: Jeremy Mikola <[email protected]>
1 parent 14af5ea commit fc5e3a4

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

src/phongo_bson.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,15 +1035,20 @@ bool php_phongo_bson_to_zval_ex(const bson_t* b, php_phongo_bson_state* state)
10351035
must_dtor_state = true;
10361036
}
10371037

1038-
// Handle raw root type early to avoid creating an iterator and visiting elements
1038+
// Handle BSON root type early to avoid creating an iterator and visiting elements
10391039
if (state->map.root.type == PHONGO_TYPEMAP_BSON) {
1040-
zval obj;
1041-
php_phongo_document_t* intern;
1040+
zval obj;
1041+
bson_t** bson;
10421042

1043-
object_init_ex(&obj, php_phongo_document_ce);
1043+
if (state->is_visiting_array) {
1044+
object_init_ex(&obj, php_phongo_packedarray_ce);
1045+
bson = &Z_PACKEDARRAY_OBJ_P(&obj)->bson;
1046+
} else {
1047+
object_init_ex(&obj, php_phongo_document_ce);
1048+
bson = &Z_DOCUMENT_OBJ_P(&obj)->bson;
1049+
}
10441050

1045-
intern = Z_DOCUMENT_OBJ_P(&obj);
1046-
intern->bson = bson_copy(b);
1051+
*bson = bson_copy(b);
10471052
zval_ptr_dtor(&state->zchild);
10481053
ZVAL_COPY_VALUE(&state->zchild, &obj);
10491054

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
MongoDB\BSON\PackedArray::toPHP(): Use bson as root type
3+
--FILE--
4+
<?php
5+
6+
require_once __DIR__ . '/../utils/basic.inc';
7+
8+
$packedArray = MongoDB\BSON\PackedArray::fromPHP([1, 2, 3]);
9+
var_dump($packedArray->toPHP(['root' => 'bson']));
10+
11+
?>
12+
===DONE===
13+
<?php exit(0); ?>
14+
--EXPECTF--
15+
object(MongoDB\BSON\PackedArray)#%d (%d) {
16+
["data"]=>
17+
string(36) "GgAAABAwAAEAAAAQMQACAAAAEDIAAwAAAAA="
18+
}
19+
===DONE===

0 commit comments

Comments
 (0)