Skip to content

Commit 2551570

Browse files
committed
PHPC-329: __pclass must be both instantiatable and Persistable
1 parent a2b71a6 commit 2551570

File tree

2 files changed

+104
-2
lines changed

2 files changed

+104
-2
lines changed

src/bson.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,12 @@ bool php_phongo_bson_visit_binary(const bson_iter_t *iter ARG_UNUSED, const char
215215
zval *zchild = NULL;
216216
TSRMLS_FETCH();
217217

218-
if (v_subtype == 0x80 && strcmp(key, PHONGO_ODM_FIELD_NAME) ==0) {
219-
((php_phongo_bson_state *)data)->odm = zend_fetch_class((char *)v_binary, v_binary_len, ZEND_FETCH_CLASS_AUTO|ZEND_FETCH_CLASS_SILENT TSRMLS_CC);
218+
if (v_subtype == 0x80 && strcmp(key, PHONGO_ODM_FIELD_NAME) == 0) {
219+
zend_class_entry *found_ce = zend_fetch_class((char *)v_binary, v_binary_len, ZEND_FETCH_CLASS_AUTO|ZEND_FETCH_CLASS_SILENT TSRMLS_CC);
220+
221+
if (found_ce && PHONGO_IS_CLASS_INSTANTIATABLE(found_ce) && instanceof_function(found_ce, php_phongo_persistable_ce TSRMLS_CC)) {
222+
((php_phongo_bson_state *)data)->odm = found_ce;
223+
}
220224
}
221225

222226
MAKE_STD_ZVAL(zchild);

tests/bson/bson-toPHP-001.phpt

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
--TEST--
2+
BSON\toPHP(): __pclass must be both instantiatable and Persistable
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
use MongoDB\BSON as BSON;
8+
9+
require_once __DIR__ . "/../utils/basic.inc";
10+
11+
abstract class MyAbstractDocument implements BSON\Persistable
12+
{
13+
}
14+
15+
class MyDocument implements BSON\Unserializable
16+
{
17+
public function bsonUnserialize(array $data)
18+
{
19+
$this->unserialized = true;
20+
}
21+
}
22+
23+
// Create base64-encoded class names for __pclass field's binary data
24+
$bMyAbstractDocument = base64_encode('MyAbstractDocument');
25+
$bMyDocument = base64_encode('MyDocument');
26+
$bUnserializable = base64_encode(BSON_NAMESPACE . '\Unserializable');
27+
$bPersistable = base64_encode(BSON_NAMESPACE . '\Persistable');
28+
29+
$tests = array(
30+
'{ "foo": "yes", "__pclass": { "$type" : "80", "$binary" : "' . $bMyAbstractDocument . '" } }',
31+
'{ "foo": "yes", "__pclass": { "$type" : "80", "$binary" : "' . $bMyDocument . '" } }',
32+
'{ "foo": "yes", "__pclass": { "$type" : "80", "$binary" : "' . $bUnserializable . '" } }',
33+
'{ "foo": "yes", "__pclass": { "$type" : "44", "$binary" : "' . $bPersistable . '" } }',
34+
);
35+
36+
foreach ($tests as $test) {
37+
echo $test, "\n";
38+
var_dump(toPHP(fromJSON($test)));
39+
echo "\n";
40+
}
41+
42+
?>
43+
===DONE===
44+
<?php exit(0); ?>
45+
--EXPECTF--
46+
{ "foo": "yes", "__pclass": { "$type" : "80", "$binary" : "TXlBYnN0cmFjdERvY3VtZW50" } }
47+
object(stdClass)#%d (2) {
48+
["foo"]=>
49+
string(3) "yes"
50+
["__pclass"]=>
51+
object(MongoDB\BSON\Binary)#%d (2) {
52+
["data"]=>
53+
string(18) "MyAbstractDocument"
54+
["subtype"]=>
55+
int(128)
56+
}
57+
}
58+
59+
{ "foo": "yes", "__pclass": { "$type" : "80", "$binary" : "TXlEb2N1bWVudA==" } }
60+
object(stdClass)#%d (2) {
61+
["foo"]=>
62+
string(3) "yes"
63+
["__pclass"]=>
64+
object(MongoDB\BSON\Binary)#%d (2) {
65+
["data"]=>
66+
string(10) "MyDocument"
67+
["subtype"]=>
68+
int(128)
69+
}
70+
}
71+
72+
{ "foo": "yes", "__pclass": { "$type" : "80", "$binary" : "TW9uZ29EQlxCU09OXFVuc2VyaWFsaXphYmxl" } }
73+
object(stdClass)#%d (2) {
74+
["foo"]=>
75+
string(3) "yes"
76+
["__pclass"]=>
77+
object(MongoDB\BSON\Binary)#%d (2) {
78+
["data"]=>
79+
string(27) "MongoDB\BSON\Unserializable"
80+
["subtype"]=>
81+
int(128)
82+
}
83+
}
84+
85+
{ "foo": "yes", "__pclass": { "$type" : "44", "$binary" : "TW9uZ29EQlxCU09OXFBlcnNpc3RhYmxl" } }
86+
object(stdClass)#%d (2) {
87+
["foo"]=>
88+
string(3) "yes"
89+
["__pclass"]=>
90+
object(MongoDB\BSON\Binary)#%d (2) {
91+
["data"]=>
92+
string(24) "MongoDB\BSON\Persistable"
93+
["subtype"]=>
94+
int(68)
95+
}
96+
}
97+
98+
===DONE===

0 commit comments

Comments
 (0)