Skip to content

Commit 8ab4acc

Browse files
committed
PHPC-410: object_to_bson() should encode non-stdClass objects
1 parent 57a3617 commit 8ab4acc

File tree

4 files changed

+84
-14
lines changed

4 files changed

+84
-14
lines changed

src/bson.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -599,15 +599,7 @@ void object_to_bson(zval *object, php_phongo_bson_flags_t flags, const char *key
599599
{
600600
bson_t child;
601601

602-
if (Z_TYPE_P(object) != IS_OBJECT || instanceof_function(Z_OBJCE_P(object), zend_standard_class_def TSRMLS_CC)) {
603-
mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, "encoding as-if was stdclass");
604-
bson_append_document_begin(bson, key, key_len, &child);
605-
zval_to_bson(object, flags, &child, NULL TSRMLS_CC);
606-
bson_append_document_end(bson, &child);
607-
return;
608-
}
609-
610-
if (instanceof_function(Z_OBJCE_P(object), php_phongo_type_ce TSRMLS_CC)) {
602+
if (Z_TYPE_P(object) == IS_OBJECT && instanceof_function(Z_OBJCE_P(object), php_phongo_type_ce TSRMLS_CC)) {
611603
if (instanceof_function(Z_OBJCE_P(object), php_phongo_serializable_ce TSRMLS_CC)) {
612604
zval *obj_data = NULL;
613605
bson_t child;
@@ -718,9 +710,9 @@ void object_to_bson(zval *object, php_phongo_bson_flags_t flags, const char *key
718710
}
719711
}
720712

721-
/* Even if we don't know how to encode the object, ensure that we at least
722-
* create an empty BSON document. */
713+
mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, "encoding document");
723714
bson_append_document_begin(bson, key, key_len, &child);
715+
zval_to_bson(object, flags, &child, NULL TSRMLS_CC);
724716
bson_append_document_end(bson, &child);
725717
}
726718
void phongo_bson_append(bson_t *bson, php_phongo_bson_flags_t flags, const char *key, long key_len, int entry_type, zval *entry TSRMLS_DC)

tests/bson/bson-fromPHP-002.phpt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
BSON\fromPHP(): Encoding non-Persistable objects as a document
3+
--INI--
4+
date.timezone=America/Los_Angeles
5+
--SKIPIF--
6+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
7+
--FILE--
8+
<?php
9+
use MongoDB\BSON as BSON;
10+
11+
require_once __DIR__ . "/../utils/basic.inc";
12+
13+
class MyDocument {
14+
private $foo = 1;
15+
protected $bar = 2;
16+
public $baz = 3;
17+
}
18+
19+
$s = fromPHP(new MyDocument);
20+
echo "Test ", toJSON($s), "\n";
21+
hex_dump($s);
22+
23+
?>
24+
===DONE===
25+
<?php exit(0); ?>
26+
--EXPECT--
27+
Test { "foo" : 1, "bar" : 2, "baz" : 3 }
28+
0 : 20 00 00 00 10 66 6f 6f 00 01 00 00 00 10 62 61 [ ....foo......ba]
29+
10 : 72 00 02 00 00 00 10 62 61 7a 00 03 00 00 00 00 [r......baz......]
30+
===DONE===

tests/bson/bson-fromPHP-003.phpt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
--TEST--
2+
BSON\fromPHP(): Encoding non-Persistable objects as a document field value
3+
--INI--
4+
date.timezone=America/Los_Angeles
5+
--SKIPIF--
6+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
7+
--FILE--
8+
<?php
9+
use MongoDB\BSON as BSON;
10+
11+
require_once __DIR__ . "/../utils/basic.inc";
12+
13+
class MyDocument {
14+
private $foo = 1;
15+
protected $bar = 2;
16+
public $baz = 3;
17+
}
18+
19+
$tests = array(
20+
array(new BSON\UTCDateTime('1416445411987')),
21+
array('x' => new BSON\UTCDateTime('1416445411987')),
22+
array(new MyDocument),
23+
array('x' => new MyDocument),
24+
);
25+
26+
foreach ($tests as $document) {
27+
$s = fromPHP($document);
28+
echo "Test ", toJSON($s), "\n";
29+
hex_dump($s);
30+
}
31+
32+
?>
33+
===DONE===
34+
<?php exit(0); ?>
35+
--EXPECT--
36+
Test { "0" : { "$date" : 1416445411987 } }
37+
0 : 10 00 00 00 09 30 00 93 c2 b9 ca 49 01 00 00 00 [.....0.....I....]
38+
Test { "x" : { "$date" : 1416445411987 } }
39+
0 : 10 00 00 00 09 78 00 93 c2 b9 ca 49 01 00 00 00 [.....x.....I....]
40+
Test { "0" : { "foo" : 1, "bar" : 2, "baz" : 3 } }
41+
0 : 28 00 00 00 03 30 00 20 00 00 00 10 66 6f 6f 00 [(....0. ....foo.]
42+
10 : 01 00 00 00 10 62 61 72 00 02 00 00 00 10 62 61 [.....bar......ba]
43+
20 : 7a 00 03 00 00 00 00 00 [z.......]
44+
Test { "x" : { "foo" : 1, "bar" : 2, "baz" : 3 } }
45+
0 : 28 00 00 00 03 78 00 20 00 00 00 10 66 6f 6f 00 [(....x. ....foo.]
46+
10 : 01 00 00 00 10 62 61 72 00 02 00 00 00 10 62 61 [.....bar......ba]
47+
20 : 7a 00 03 00 00 00 00 00 [z.......]
48+
===DONE===

tests/bson/bson-utcdatetime-001.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ Test#1 { "0" : { "$date" : 1416445411987 } }
5151
string(37) "{ "0" : { "$date" : 1416445411987 } }"
5252
string(37) "{ "0" : { "$date" : 1416445411987 } }"
5353
bool(true)
54-
Test#2 { "0" : { } }
55-
string(14) "{ "0" : { } }"
56-
string(14) "{ "0" : { } }"
54+
Test#2 { "0" : { "date" : "2014-11-20 01:03:31.987%r(0+)%r", "timezone_type" : 1, "timezone" : "+00:00" } }
55+
string(98) "{ "0" : { "date" : "2014-11-20 01:03:31.987%r(0+)%r", "timezone_type" : 1, "timezone" : "+00:00" } }"
56+
string(98) "{ "0" : { "date" : "2014-11-20 01:03:31.987%r(0+)%r", "timezone_type" : 1, "timezone" : "+00:00" } }"
5757
bool(false)
5858
===DONE===

0 commit comments

Comments
 (0)