@@ -189,6 +189,36 @@ static bool php_phongo_document_get(php_phongo_document_t* intern, char* key, si
189189 return true;
190190}
191191
192+ static bool php_phongo_document_get_by_zval (php_phongo_document_t * intern , zval * key , zval * return_value , bool null_if_missing )
193+ {
194+ if (Z_TYPE_P (key ) != IS_STRING && Z_TYPE_P (key ) != IS_LONG ) {
195+ if (null_if_missing ) {
196+ ZVAL_NULL (return_value );
197+ return true;
198+ }
199+
200+ phongo_throw_exception (PHONGO_ERROR_RUNTIME , "Could not find key of type \"%s\" in BSON document" , zend_zval_type_name (key ));
201+ return false;
202+ }
203+
204+ zend_string * tmp_str ;
205+ zend_string * str = zval_try_get_tmp_string (key , & tmp_str );
206+
207+ if (!str ) {
208+ // Exception already thrown
209+ return false;
210+ }
211+
212+ if (!php_phongo_document_get (intern , ZSTR_VAL (str ), ZSTR_LEN (str ), return_value , null_if_missing )) {
213+ // Exception already thrown
214+ zend_tmp_string_release (tmp_str );
215+ return false;
216+ }
217+
218+ zend_tmp_string_release (tmp_str );
219+ return true;
220+ }
221+
192222static PHP_METHOD (MongoDB_BSON_Document , get )
193223{
194224 php_phongo_document_t * intern ;
@@ -201,10 +231,8 @@ static PHP_METHOD(MongoDB_BSON_Document, get)
201231
202232 intern = Z_DOCUMENT_OBJ_P (getThis ());
203233
204- if (!php_phongo_document_get (intern , key , key_len , return_value , false)) {
205- // Exception already thrown
206- RETURN_NULL ();
207- }
234+ // May throw, in which case we do nothing
235+ php_phongo_document_get (intern , key , key_len , return_value , false);
208236}
209237
210238static PHP_METHOD (MongoDB_BSON_Document , getIterator )
@@ -226,6 +254,30 @@ static bool php_phongo_document_has(php_phongo_document_t* intern, char* key, si
226254 return bson_iter_find_w_len (& iter , key , key_len );
227255}
228256
257+ static bool php_phongo_document_has_by_zval (php_phongo_document_t * intern , zval * key )
258+ {
259+ if (Z_TYPE_P (key ) != IS_STRING && Z_TYPE_P (key ) != IS_LONG ) {
260+ return false;
261+ }
262+
263+ zend_string * tmp_str ;
264+ zend_string * str = zval_try_get_tmp_string (key , & tmp_str );
265+
266+ if (!str ) {
267+ // Exception already thrown
268+ return false;
269+ }
270+
271+ if (!php_phongo_document_has (intern , ZSTR_VAL (str ), ZSTR_LEN (str ))) {
272+ // Exception may be thrown if BSON iterator could not be initialized
273+ zend_tmp_string_release (tmp_str );
274+ return false;
275+ }
276+
277+ zend_tmp_string_release (tmp_str );
278+ return true;
279+ }
280+
229281static PHP_METHOD (MongoDB_BSON_Document , has )
230282{
231283 php_phongo_document_t * intern ;
@@ -306,11 +358,7 @@ static PHP_METHOD(MongoDB_BSON_Document, offsetExists)
306358
307359 intern = Z_DOCUMENT_OBJ_P (getThis ());
308360
309- if (Z_TYPE_P (offset ) != IS_STRING ) {
310- RETURN_FALSE ;
311- }
312-
313- RETURN_BOOL (php_phongo_document_has (intern , Z_STRVAL_P (offset ), Z_STRLEN_P (offset )));
361+ RETURN_BOOL (php_phongo_document_has_by_zval (intern , offset ));
314362}
315363
316364static PHP_METHOD (MongoDB_BSON_Document , offsetGet )
@@ -324,13 +372,8 @@ static PHP_METHOD(MongoDB_BSON_Document, offsetGet)
324372
325373 intern = Z_DOCUMENT_OBJ_P (getThis ());
326374
327- if (Z_TYPE_P (offset ) != IS_STRING ) {
328- phongo_throw_exception (PHONGO_ERROR_RUNTIME , "Could not find key of type \"%s\" in BSON document" , zend_zval_type_name (offset ));
329- return ;
330- }
331-
332375 // May throw, in which case we do nothing
333- php_phongo_document_get (intern , Z_STRVAL_P ( offset ), Z_STRLEN_P ( offset ) , return_value , false);
376+ php_phongo_document_get_by_zval (intern , offset , return_value , false);
334377}
335378
336379static PHP_METHOD (MongoDB_BSON_Document , offsetSet )
@@ -491,13 +534,9 @@ static HashTable* php_phongo_document_get_properties(zend_object* object)
491534
492535zval * php_phongo_document_read_property (zend_object * object , zend_string * member , int type , void * * cache_slot , zval * rv )
493536{
494- php_phongo_document_t * intern ;
495- char * key = ZSTR_VAL (member );
496- size_t key_len = ZSTR_LEN (member );
497-
498- intern = Z_OBJ_DOCUMENT (object );
537+ php_phongo_document_t * intern = Z_OBJ_DOCUMENT (object );
499538
500- if (!php_phongo_document_get (intern , key , key_len , rv , type == BP_VAR_IS )) {
539+ if (!php_phongo_document_get (intern , ZSTR_VAL ( member ), ZSTR_LEN ( member ) , rv , type == BP_VAR_IS )) {
501540 // Exception already thrown
502541 return & EG (uninitialized_zval );
503542 }
@@ -511,15 +550,11 @@ zval* php_phongo_document_write_property(zend_object* object, zend_string* membe
511550 return value ;
512551}
513552
514- int php_phongo_document_has_property (zend_object * object , zend_string * name , int has_set_exists , void * * cache_slot )
553+ int php_phongo_document_has_property (zend_object * object , zend_string * member , int has_set_exists , void * * cache_slot )
515554{
516- php_phongo_document_t * intern ;
517- char * key = ZSTR_VAL (name );
518- size_t key_len = ZSTR_LEN (name );
519-
520- intern = Z_OBJ_DOCUMENT (object );
555+ php_phongo_document_t * intern = Z_OBJ_DOCUMENT (object );
521556
522- return php_phongo_document_has (intern , key , key_len );
557+ return php_phongo_document_has (intern , ZSTR_VAL ( member ), ZSTR_LEN ( member ) );
523558}
524559
525560void php_phongo_document_unset_property (zend_object * object , zend_string * member , void * * cache_slot )
@@ -529,21 +564,9 @@ void php_phongo_document_unset_property(zend_object* object, zend_string* member
529564
530565zval * php_phongo_document_read_dimension (zend_object * object , zval * offset , int type , zval * rv )
531566{
532- php_phongo_document_t * intern ;
533-
534- intern = Z_OBJ_DOCUMENT (object );
535-
536- if (Z_TYPE_P (offset ) != IS_STRING ) {
537- if (type == BP_VAR_IS ) {
538- ZVAL_NULL (rv );
539- return rv ;
540- }
541-
542- phongo_throw_exception (PHONGO_ERROR_RUNTIME , "Could not find key of type \"%s\" in BSON document" , zend_zval_type_name (offset ));
543- return & EG (uninitialized_zval );
544- }
567+ php_phongo_document_t * intern = Z_OBJ_DOCUMENT (object );
545568
546- if (!php_phongo_document_get (intern , Z_STRVAL_P ( offset ), Z_STRLEN_P ( offset ) , rv , type == BP_VAR_IS )) {
569+ if (!php_phongo_document_get_by_zval (intern , offset , rv , type == BP_VAR_IS )) {
547570 // Exception already thrown
548571 return & EG (uninitialized_zval );
549572 }
@@ -558,15 +581,9 @@ void php_phongo_document_write_dimension(zend_object* object, zval* offset, zval
558581
559582int php_phongo_document_has_dimension (zend_object * object , zval * member , int check_empty )
560583{
561- php_phongo_document_t * intern ;
562-
563- intern = Z_OBJ_DOCUMENT (object );
564-
565- if (Z_TYPE_P (member ) != IS_STRING ) {
566- return false;
567- }
584+ php_phongo_document_t * intern = Z_OBJ_DOCUMENT (object );
568585
569- return php_phongo_document_has (intern , Z_STRVAL_P ( member ), Z_STRLEN_P ( member ) );
586+ return php_phongo_document_has_by_zval (intern , member );
570587}
571588
572589void php_phongo_document_unset_dimension (zend_object * object , zval * offset )
0 commit comments