1616
1717#include <php.h>
1818#include <ext/standard/base64.h>
19+ #include <Zend/zend_exceptions.h>
1920#include <Zend/zend_interfaces.h>
2021#include <Zend/zend_operators.h>
2122#include <ext/standard/php_var.h>
@@ -167,7 +168,7 @@ static PHP_METHOD(MongoDB_BSON_Document, fromPHP)
167168 RETURN_ZVAL (& zv , 1 , 1 );
168169}
169170
170- static bool php_phongo_document_get (php_phongo_document_t * intern , char * key , size_t key_len , zval * return_value )
171+ static bool php_phongo_document_get (php_phongo_document_t * intern , char * key , size_t key_len , zval * return_value , bool null_if_missing )
171172{
172173 bson_iter_t iter ;
173174
@@ -177,6 +178,11 @@ static bool php_phongo_document_get(php_phongo_document_t* intern, char* key, si
177178 }
178179
179180 if (!bson_iter_find_w_len (& iter , key , key_len )) {
181+ if (null_if_missing ) {
182+ ZVAL_NULL (return_value );
183+ return true;
184+ }
185+
180186 phongo_throw_exception (PHONGO_ERROR_RUNTIME , "Could not find key \"%s\" in BSON document" , key );
181187 return false;
182188 }
@@ -198,7 +204,7 @@ static PHP_METHOD(MongoDB_BSON_Document, get)
198204
199205 intern = Z_DOCUMENT_OBJ_P (getThis ());
200206
201- if (!php_phongo_document_get (intern , key , key_len , return_value )) {
207+ if (!php_phongo_document_get (intern , key , key_len , return_value , false )) {
202208 // Exception already thrown
203209 RETURN_NULL ();
204210 }
@@ -327,7 +333,7 @@ static PHP_METHOD(MongoDB_BSON_Document, offsetGet)
327333 }
328334
329335 // May throw, in which case we do nothing
330- php_phongo_document_get (intern , Z_STRVAL_P (offset ), Z_STRLEN_P (offset ), return_value );
336+ php_phongo_document_get (intern , Z_STRVAL_P (offset ), Z_STRLEN_P (offset ), return_value , false );
331337}
332338
333339static PHP_METHOD (MongoDB_BSON_Document , offsetSet )
@@ -552,7 +558,7 @@ zval* php_phongo_document_read_property(phongo_compat_object_handler_type* objec
552558
553559 PHONGO_COMPAT_PROPERTY_ACCESSOR_NAME_TO_STRING (member , key , key_len );
554560
555- if (!php_phongo_document_get (intern , key , key_len , rv )) {
561+ if (!php_phongo_document_get (intern , key , key_len , rv , type == BP_VAR_IS )) {
556562 // Exception already thrown
557563 return & EG (uninitialized_zval );
558564 }
@@ -591,11 +597,16 @@ zval* php_phongo_document_read_dimension(phongo_compat_object_handler_type* obje
591597 intern = Z_OBJ_DOCUMENT (PHONGO_COMPAT_GET_OBJ (object ));
592598
593599 if (Z_TYPE_P (offset ) != IS_STRING ) {
600+ if (type == BP_VAR_IS ) {
601+ ZVAL_NULL (rv );
602+ return rv ;
603+ }
604+
594605 phongo_throw_exception (PHONGO_ERROR_RUNTIME , "Could not find key of type \"%s\" in BSON document" , PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P (offset ));
595606 return & EG (uninitialized_zval );
596607 }
597608
598- if (!php_phongo_document_get (intern , Z_STRVAL_P (offset ), Z_STRLEN_P (offset ), rv )) {
609+ if (!php_phongo_document_get (intern , Z_STRVAL_P (offset ), Z_STRLEN_P (offset ), rv , type == BP_VAR_IS )) {
599610 // Exception already thrown
600611 return & EG (uninitialized_zval );
601612 }
@@ -615,7 +626,6 @@ int php_phongo_document_has_dimension(phongo_compat_object_handler_type* object,
615626 intern = Z_OBJ_DOCUMENT (PHONGO_COMPAT_GET_OBJ (object ));
616627
617628 if (Z_TYPE_P (member ) != IS_STRING ) {
618- phongo_throw_exception (PHONGO_ERROR_RUNTIME , "Could not find key of type \"%s\" in BSON document" , PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P (member ));
619629 return false;
620630 }
621631
0 commit comments