Skip to content

Commit b0cea97

Browse files
committed
PHPC-2244: Return Int64 instances when converting BSON objects to PHP
1 parent 3f2e5d6 commit b0cea97

File tree

8 files changed

+103
-3
lines changed

8 files changed

+103
-3
lines changed

src/BSON/Document.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ static PHP_METHOD(MongoDB_BSON_Document, toPHP)
256256

257257
intern = Z_DOCUMENT_OBJ_P(getThis());
258258

259+
state.map.int64_as_object = true;
260+
259261
if (!php_phongo_bson_to_zval_ex(intern->bson, &state)) {
260262
zval_ptr_dtor(&state.zchild);
261263
php_phongo_bson_typemap_dtor(&state.map);

src/BSON/PackedArray.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ static PHP_METHOD(MongoDB_BSON_PackedArray, toPHP)
184184

185185
intern = Z_PACKEDARRAY_OBJ_P(getThis());
186186

187-
state.is_visiting_array = true;
187+
state.is_visiting_array = true;
188+
state.map.int64_as_object = true;
188189

189190
if (!php_phongo_bson_to_zval_ex(intern->bson, &state)) {
190191
zval_ptr_dtor(&state.zchild);

src/phongo_bson.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,9 +579,17 @@ static bool php_phongo_bson_visit_int64(const bson_iter_t* iter ARG_UNUSED, cons
579579
php_phongo_field_path_write_item_at_current_level(state->field_path, key);
580580

581581
if (state->is_visiting_array) {
582-
ADD_NEXT_INDEX_INT64(retval, v_int64);
582+
if (state->map.int64_as_object) {
583+
ADD_NEXT_INDEX_INT64_OBJ(retval, v_int64);
584+
} else {
585+
ADD_NEXT_INDEX_INT64(retval, v_int64);
586+
}
583587
} else {
584-
ADD_ASSOC_INT64(retval, key, v_int64);
588+
if (state->map.int64_as_object) {
589+
ADD_ASSOC_INT64_OBJ(retval, key, v_int64);
590+
} else {
591+
ADD_ASSOC_INT64(retval, key, v_int64);
592+
}
585593
}
586594

587595
return false;

src/phongo_bson.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ typedef struct {
6262
php_phongo_bson_typemap_element document;
6363
php_phongo_bson_typemap_element array;
6464
php_phongo_bson_typemap_element root;
65+
bool int64_as_object;
6566
struct {
6667
php_phongo_field_path_map_element** map;
6768
size_t allocated_size;

src/phongo_compat.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,18 @@
121121
} \
122122
}
123123

124+
#define ADD_NEXT_INDEX_INT64_OBJ(_zv, _value) \
125+
do { \
126+
zval zchild; \
127+
phongo_int64_new(&zchild, (_value)); \
128+
add_next_index_zval((_zv), &zchild); \
129+
} while (0);
130+
#define ADD_ASSOC_INT64_OBJ(_zv, _key, _value) \
131+
do { \
132+
zval zchild; \
133+
phongo_int64_new(&zchild, (_value)); \
134+
add_assoc_zval((_zv), (_key), &zchild); \
135+
} while (0);
124136
#if SIZEOF_ZEND_LONG == 8
125137
#define ADD_INDEX_INT64(_zv, _index, _value) add_index_long((_zv), (_index), (_value))
126138
#define ADD_NEXT_INDEX_INT64(_zv, _value) add_next_index_long((_zv), (_value))
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
MongoDB\BSON\Document::toPHP(): int64 values are returned as Int64 instances
3+
--FILE--
4+
<?php
5+
6+
$bson = MongoDB\BSON\Document::fromPHP([
7+
'int32' => 123,
8+
'int64' => new MongoDB\BSON\Int64(123),
9+
]);
10+
11+
var_dump($bson->toPHP());
12+
13+
?>
14+
===DONE===
15+
<?php exit(0); ?>
16+
--EXPECTF--
17+
object(stdClass)#%d (%d) {
18+
["int32"]=>
19+
int(123)
20+
["int64"]=>
21+
object(MongoDB\BSON\Int64)#%d (1) {
22+
["integer"]=>
23+
string(3) "123"
24+
}
25+
}
26+
===DONE===
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
MongoDB\BSON\PackedArray::toPHP(): int64 values are returned as Int64 instances
3+
--FILE--
4+
<?php
5+
6+
$bson = MongoDB\BSON\PackedArray::fromPHP([
7+
123,
8+
new MongoDB\BSON\Int64(123),
9+
]);
10+
11+
var_dump($bson->toPHP());
12+
13+
?>
14+
===DONE===
15+
<?php exit(0); ?>
16+
--EXPECTF--
17+
array(2) {
18+
[0]=>
19+
int(123)
20+
[1]=>
21+
object(MongoDB\BSON\Int64)#%d (1) {
22+
["integer"]=>
23+
string(3) "123"
24+
}
25+
}
26+
===DONE===

tests/bson/bson-toPHP-017.phpt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
MongoDB\BSON\toPHP(): int64 values are returned as int scalars
3+
--FILE--
4+
<?php
5+
6+
$bson = MongoDB\BSON\fromPHP([
7+
'int32' => 123,
8+
// Using a 32-bit value to simplify testing
9+
'int64' => new MongoDB\BSON\Int64(123),
10+
]);
11+
12+
var_dump(MongoDB\BSON\toPHP($bson));
13+
14+
?>
15+
===DONE===
16+
<?php exit(0); ?>
17+
--EXPECTF--
18+
object(stdClass)#%d (%d) {
19+
["int32"]=>
20+
int(123)
21+
["int64"]=>
22+
int(123)
23+
}
24+
===DONE===

0 commit comments

Comments
 (0)