16
16
17
17
#include <php.h>
18
18
#include <ext/standard/base64.h>
19
+ #include <Zend/zend_exceptions.h>
19
20
#include <Zend/zend_interfaces.h>
20
21
#include <Zend/zend_operators.h>
21
22
#include <ext/standard/php_var.h>
@@ -167,7 +168,7 @@ static PHP_METHOD(MongoDB_BSON_Document, fromPHP)
167
168
RETURN_ZVAL (& zv , 1 , 1 );
168
169
}
169
170
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 )
171
172
{
172
173
bson_iter_t iter ;
173
174
@@ -177,6 +178,11 @@ static bool php_phongo_document_get(php_phongo_document_t* intern, char* key, si
177
178
}
178
179
179
180
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
+
180
186
phongo_throw_exception (PHONGO_ERROR_RUNTIME , "Could not find key \"%s\" in BSON document" , key );
181
187
return false;
182
188
}
@@ -198,7 +204,7 @@ static PHP_METHOD(MongoDB_BSON_Document, get)
198
204
199
205
intern = Z_DOCUMENT_OBJ_P (getThis ());
200
206
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 )) {
202
208
// Exception already thrown
203
209
RETURN_NULL ();
204
210
}
@@ -327,7 +333,7 @@ static PHP_METHOD(MongoDB_BSON_Document, offsetGet)
327
333
}
328
334
329
335
// 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 );
331
337
}
332
338
333
339
static PHP_METHOD (MongoDB_BSON_Document , offsetSet )
@@ -552,7 +558,7 @@ zval* php_phongo_document_read_property(phongo_compat_object_handler_type* objec
552
558
553
559
PHONGO_COMPAT_PROPERTY_ACCESSOR_NAME_TO_STRING (member , key , key_len );
554
560
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 )) {
556
562
// Exception already thrown
557
563
return & EG (uninitialized_zval );
558
564
}
@@ -591,11 +597,16 @@ zval* php_phongo_document_read_dimension(phongo_compat_object_handler_type* obje
591
597
intern = Z_OBJ_DOCUMENT (PHONGO_COMPAT_GET_OBJ (object ));
592
598
593
599
if (Z_TYPE_P (offset ) != IS_STRING ) {
600
+ if (type == BP_VAR_IS ) {
601
+ ZVAL_NULL (rv );
602
+ return rv ;
603
+ }
604
+
594
605
phongo_throw_exception (PHONGO_ERROR_RUNTIME , "Could not find key of type \"%s\" in BSON document" , PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P (offset ));
595
606
return & EG (uninitialized_zval );
596
607
}
597
608
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 )) {
599
610
// Exception already thrown
600
611
return & EG (uninitialized_zval );
601
612
}
@@ -615,7 +626,6 @@ int php_phongo_document_has_dimension(phongo_compat_object_handler_type* object,
615
626
intern = Z_OBJ_DOCUMENT (PHONGO_COMPAT_GET_OBJ (object ));
616
627
617
628
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 ));
619
629
return false;
620
630
}
621
631
0 commit comments