@@ -192,6 +192,36 @@ static bool php_phongo_document_get(php_phongo_document_t* intern, char* key, si
192
192
return true;
193
193
}
194
194
195
+ static bool php_phongo_document_get_by_zval (php_phongo_document_t * intern , zval * key , zval * return_value , bool null_if_missing )
196
+ {
197
+ if (Z_TYPE_P (key ) != IS_STRING && Z_TYPE_P (key ) != IS_LONG ) {
198
+ if (null_if_missing ) {
199
+ ZVAL_NULL (return_value );
200
+ return true;
201
+ }
202
+
203
+ phongo_throw_exception (PHONGO_ERROR_RUNTIME , "Could not find key of type \"%s\" in BSON document" , zend_zval_type_name (key ));
204
+ return false;
205
+ }
206
+
207
+ zend_string * tmp_str ;
208
+ zend_string * str = zval_try_get_tmp_string (key , & tmp_str );
209
+
210
+ if (!str ) {
211
+ // Exception already thrown
212
+ return false;
213
+ }
214
+
215
+ if (!php_phongo_document_get (intern , ZSTR_VAL (str ), ZSTR_LEN (str ), return_value , null_if_missing )) {
216
+ // Exception already thrown
217
+ zend_tmp_string_release (tmp_str );
218
+ return false;
219
+ }
220
+
221
+ zend_tmp_string_release (tmp_str );
222
+ return true;
223
+ }
224
+
195
225
static PHP_METHOD (MongoDB_BSON_Document , get )
196
226
{
197
227
php_phongo_document_t * intern ;
@@ -229,6 +259,30 @@ static bool php_phongo_document_has(php_phongo_document_t* intern, char* key, si
229
259
return bson_iter_find_w_len (& iter , key , key_len );
230
260
}
231
261
262
+ static bool php_phongo_document_has_by_zval (php_phongo_document_t * intern , zval * key )
263
+ {
264
+ if (Z_TYPE_P (key ) != IS_STRING && Z_TYPE_P (key ) != IS_LONG ) {
265
+ return false;
266
+ }
267
+
268
+ zend_string * tmp_str ;
269
+ zend_string * str = zval_try_get_tmp_string (key , & tmp_str );
270
+
271
+ if (!str ) {
272
+ // Exception already thrown
273
+ return false;
274
+ }
275
+
276
+ if (!php_phongo_document_has (intern , ZSTR_VAL (str ), ZSTR_LEN (str ))) {
277
+ // Exception may be thrown if BSON iterator could not be initialized
278
+ zend_tmp_string_release (tmp_str );
279
+ return false;
280
+ }
281
+
282
+ zend_tmp_string_release (tmp_str );
283
+ return true;
284
+ }
285
+
232
286
static PHP_METHOD (MongoDB_BSON_Document , has )
233
287
{
234
288
php_phongo_document_t * intern ;
@@ -309,11 +363,7 @@ static PHP_METHOD(MongoDB_BSON_Document, offsetExists)
309
363
310
364
intern = Z_DOCUMENT_OBJ_P (getThis ());
311
365
312
- if (Z_TYPE_P (offset ) != IS_STRING ) {
313
- RETURN_FALSE ;
314
- }
315
-
316
- RETURN_BOOL (php_phongo_document_has (intern , Z_STRVAL_P (offset ), Z_STRLEN_P (offset )));
366
+ RETURN_BOOL (php_phongo_document_has_by_zval (intern , offset ));
317
367
}
318
368
319
369
static PHP_METHOD (MongoDB_BSON_Document , offsetGet )
@@ -327,13 +377,8 @@ static PHP_METHOD(MongoDB_BSON_Document, offsetGet)
327
377
328
378
intern = Z_DOCUMENT_OBJ_P (getThis ());
329
379
330
- if (Z_TYPE_P (offset ) != IS_STRING ) {
331
- phongo_throw_exception (PHONGO_ERROR_RUNTIME , "Could not find key of type \"%s\" in BSON document" , zend_zval_type_name (offset ));
332
- return ;
333
- }
334
-
335
380
// May throw, in which case we do nothing
336
- php_phongo_document_get (intern , Z_STRVAL_P ( offset ), Z_STRLEN_P ( offset ) , return_value , false);
381
+ php_phongo_document_get_by_zval (intern , offset , return_value , false);
337
382
}
338
383
339
384
static PHP_METHOD (MongoDB_BSON_Document , offsetSet )
@@ -588,21 +633,9 @@ void php_phongo_document_unset_property(zend_object* object, zend_string* member
588
633
589
634
zval * php_phongo_document_read_dimension (zend_object * object , zval * offset , int type , zval * rv )
590
635
{
591
- php_phongo_document_t * intern ;
592
-
593
- intern = Z_OBJ_DOCUMENT (object );
594
-
595
- if (Z_TYPE_P (offset ) != IS_STRING ) {
596
- if (type == BP_VAR_IS ) {
597
- ZVAL_NULL (rv );
598
- return rv ;
599
- }
600
-
601
- phongo_throw_exception (PHONGO_ERROR_RUNTIME , "Could not find key of type \"%s\" in BSON document" , zend_zval_type_name (offset ));
602
- return & EG (uninitialized_zval );
603
- }
636
+ php_phongo_document_t * intern = Z_OBJ_DOCUMENT (object );
604
637
605
- if (!php_phongo_document_get (intern , Z_STRVAL_P ( offset ), Z_STRLEN_P ( offset ) , rv , type == BP_VAR_IS )) {
638
+ if (!php_phongo_document_get_by_zval (intern , offset , rv , type == BP_VAR_IS )) {
606
639
// Exception already thrown
607
640
return & EG (uninitialized_zval );
608
641
}
@@ -617,15 +650,9 @@ void php_phongo_document_write_dimension(zend_object* object, zval* offset, zval
617
650
618
651
int php_phongo_document_has_dimension (zend_object * object , zval * member , int check_empty )
619
652
{
620
- php_phongo_document_t * intern ;
621
-
622
- intern = Z_OBJ_DOCUMENT (object );
623
-
624
- if (Z_TYPE_P (member ) != IS_STRING ) {
625
- return false;
626
- }
653
+ php_phongo_document_t * intern = Z_OBJ_DOCUMENT (object );
627
654
628
- return php_phongo_document_has (intern , Z_STRVAL_P ( member ), Z_STRLEN_P ( member ) );
655
+ return php_phongo_document_has_by_zval (intern , member );
629
656
}
630
657
631
658
void php_phongo_document_unset_dimension (zend_object * object , zval * offset )
0 commit comments