@@ -165,28 +165,41 @@ static PHP_METHOD(MongoDB_BSON_Document, fromPHP)
165
165
RETURN_ZVAL (& zv , 1 , 1 );
166
166
}
167
167
168
+ static bool php_phongo_document_get (php_phongo_document_t * intern , char * key , size_t key_len , zval * return_value )
169
+ {
170
+ bson_iter_t iter ;
171
+
172
+ if (!bson_iter_init (& iter , intern -> bson )) {
173
+ phongo_throw_exception (PHONGO_ERROR_RUNTIME , "Could not initialize BSON iterator" );
174
+ return false;
175
+ }
176
+
177
+ if (!bson_iter_find_w_len (& iter , key , key_len )) {
178
+ phongo_throw_exception (PHONGO_ERROR_RUNTIME , "Could not find key \"%s\" in BSON document" , key );
179
+ return false;
180
+ }
181
+
182
+ phongo_bson_value_to_zval (bson_iter_value (& iter ), return_value );
183
+
184
+ return true;
185
+ }
186
+
168
187
static PHP_METHOD (MongoDB_BSON_Document , get )
169
188
{
170
189
php_phongo_document_t * intern ;
171
190
char * key ;
172
191
size_t key_len ;
173
- bson_iter_t iter ;
174
192
175
193
PHONGO_PARSE_PARAMETERS_START (1 , 1 )
176
194
Z_PARAM_STRING (key , key_len )
177
195
PHONGO_PARSE_PARAMETERS_END ();
178
196
179
197
intern = Z_DOCUMENT_OBJ_P (getThis ());
180
- if (!bson_iter_init (& iter , intern -> bson )) {
181
- phongo_throw_exception (PHONGO_ERROR_RUNTIME , "Could not initialize BSON iterator." );
182
- }
183
198
184
- if (!bson_iter_find_w_len ( & iter , key , key_len )) {
185
- phongo_throw_exception ( PHONGO_ERROR_RUNTIME , "Could not find key \"%s\" in BSON data" , key );
199
+ if (!php_phongo_document_get ( intern , key , key_len , return_value )) {
200
+ // Exception already thrown
186
201
RETURN_NULL ();
187
202
}
188
-
189
- phongo_bson_value_to_zval (bson_iter_value (& iter ), return_value );
190
203
}
191
204
192
205
static PHP_METHOD (MongoDB_BSON_Document , getIterator )
@@ -196,23 +209,31 @@ static PHP_METHOD(MongoDB_BSON_Document, getIterator)
196
209
phongo_iterator_init (return_value , getThis ());
197
210
}
198
211
212
+ static bool php_phongo_document_has (php_phongo_document_t * intern , char * key , size_t key_len )
213
+ {
214
+ bson_iter_t iter ;
215
+
216
+ if (!bson_iter_init (& iter , intern -> bson )) {
217
+ phongo_throw_exception (PHONGO_ERROR_RUNTIME , "Could not initialize BSON iterator" );
218
+ return false;
219
+ }
220
+
221
+ return bson_iter_find_w_len (& iter , key , key_len );
222
+ }
223
+
199
224
static PHP_METHOD (MongoDB_BSON_Document , has )
200
225
{
201
226
php_phongo_document_t * intern ;
202
227
char * key ;
203
228
size_t key_len ;
204
- bson_iter_t iter ;
205
229
206
230
PHONGO_PARSE_PARAMETERS_START (1 , 1 )
207
231
Z_PARAM_STRING (key , key_len )
208
232
PHONGO_PARSE_PARAMETERS_END ();
209
233
210
234
intern = Z_DOCUMENT_OBJ_P (getThis ());
211
- if (!bson_iter_init (& iter , intern -> bson )) {
212
- phongo_throw_exception (PHONGO_ERROR_RUNTIME , "Could not initialize BSON iterator." );
213
- }
214
235
215
- RETURN_BOOL (bson_iter_find_w_len ( & iter , key , key_len ));
236
+ RETURN_BOOL (php_phongo_document_has ( intern , key , key_len ));
216
237
}
217
238
218
239
static PHP_METHOD (MongoDB_BSON_Document , toCanonicalExtendedJSON )
@@ -269,6 +290,54 @@ static PHP_METHOD(MongoDB_BSON_Document, toPHP)
269
290
RETURN_ZVAL (& state .zchild , 0 , 1 );
270
291
}
271
292
293
+ static PHP_METHOD (MongoDB_BSON_Document , offsetExists )
294
+ {
295
+ php_phongo_document_t * intern ;
296
+ zval * offset ;
297
+
298
+ PHONGO_PARSE_PARAMETERS_START (1 , 1 )
299
+ Z_PARAM_ZVAL (offset )
300
+ PHONGO_PARSE_PARAMETERS_END ();
301
+
302
+ intern = Z_DOCUMENT_OBJ_P (getThis ());
303
+
304
+ if (Z_TYPE_P (offset ) != IS_STRING ) {
305
+ RETURN_FALSE ;
306
+ }
307
+
308
+ RETURN_BOOL (php_phongo_document_has (intern , Z_STRVAL_P (offset ), Z_STRLEN_P (offset )));
309
+ }
310
+
311
+ static PHP_METHOD (MongoDB_BSON_Document , offsetGet )
312
+ {
313
+ php_phongo_document_t * intern ;
314
+ zval * offset ;
315
+
316
+ PHONGO_PARSE_PARAMETERS_START (1 , 1 )
317
+ Z_PARAM_ZVAL (offset )
318
+ PHONGO_PARSE_PARAMETERS_END ();
319
+
320
+ intern = Z_DOCUMENT_OBJ_P (getThis ());
321
+
322
+ if (Z_TYPE_P (offset ) != IS_STRING ) {
323
+ phongo_throw_exception (PHONGO_ERROR_RUNTIME , "Could not find key of type \"%s\" in BSON document" , PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P (offset ));
324
+ return ;
325
+ }
326
+
327
+ // May throw, in which case we do nothing
328
+ php_phongo_document_get (intern , Z_STRVAL_P (offset ), Z_STRLEN_P (offset ), return_value );
329
+ }
330
+
331
+ static PHP_METHOD (MongoDB_BSON_Document , offsetSet )
332
+ {
333
+ phongo_throw_exception (PHONGO_ERROR_LOGIC , "Cannot write to %s property" , ZSTR_VAL (php_phongo_document_ce -> name ));
334
+ }
335
+
336
+ static PHP_METHOD (MongoDB_BSON_Document , offsetUnset )
337
+ {
338
+ phongo_throw_exception (PHONGO_ERROR_LOGIC , "Cannot unset %s property" , ZSTR_VAL (php_phongo_document_ce -> name ));
339
+ }
340
+
272
341
static PHP_METHOD (MongoDB_BSON_Document , __toString )
273
342
{
274
343
php_phongo_document_t * intern ;
@@ -471,9 +540,94 @@ static HashTable* php_phongo_document_get_properties(phongo_compat_object_handle
471
540
return php_phongo_document_get_properties_hash (object , false, 1 );
472
541
}
473
542
543
+ zval * php_phongo_document_read_property (phongo_compat_object_handler_type * object , phongo_compat_property_accessor_name_type * member , int type , void * * cache_slot , zval * rv )
544
+ {
545
+ php_phongo_document_t * intern ;
546
+ char * key ;
547
+ size_t key_len ;
548
+
549
+ intern = Z_OBJ_DOCUMENT (PHONGO_COMPAT_GET_OBJ (object ));
550
+
551
+ PHONGO_COMPAT_PROPERTY_ACCESSOR_NAME_TO_STRING (member , key , key_len );
552
+
553
+ if (!php_phongo_document_get (intern , key , key_len , rv )) {
554
+ // Exception already thrown
555
+ return & EG (uninitialized_zval );
556
+ }
557
+
558
+ return rv ;
559
+ }
560
+
561
+ zval * php_phongo_document_write_property (phongo_compat_object_handler_type * object , phongo_compat_property_accessor_name_type * member , zval * value , void * * cache_slot )
562
+ {
563
+ phongo_throw_exception (PHONGO_ERROR_LOGIC , "Cannot write to %s property" , ZSTR_VAL (php_phongo_document_ce -> name ));
564
+ return value ;
565
+ }
566
+
567
+ int php_phongo_document_has_property (phongo_compat_object_handler_type * object , phongo_compat_property_accessor_name_type * name , int has_set_exists , void * * cache_slot )
568
+ {
569
+ php_phongo_document_t * intern ;
570
+ char * key ;
571
+ size_t key_len ;
572
+
573
+ intern = Z_OBJ_DOCUMENT (PHONGO_COMPAT_GET_OBJ (object ));
574
+
575
+ PHONGO_COMPAT_PROPERTY_ACCESSOR_NAME_TO_STRING (name , key , key_len );
576
+
577
+ return php_phongo_document_has (intern , key , key_len );
578
+ }
579
+
580
+ void php_phongo_document_unset_property (phongo_compat_object_handler_type * object , phongo_compat_property_accessor_name_type * member , void * * cache_slot )
581
+ {
582
+ phongo_throw_exception (PHONGO_ERROR_LOGIC , "Cannot unset %s property" , ZSTR_VAL (php_phongo_document_ce -> name ));
583
+ }
584
+
585
+ zval * php_phongo_document_read_dimension (phongo_compat_object_handler_type * object , zval * offset , int type , zval * rv )
586
+ {
587
+ php_phongo_document_t * intern ;
588
+
589
+ intern = Z_OBJ_DOCUMENT (PHONGO_COMPAT_GET_OBJ (object ));
590
+
591
+ if (Z_TYPE_P (offset ) != IS_STRING ) {
592
+ phongo_throw_exception (PHONGO_ERROR_RUNTIME , "Could not find key of type \"%s\" in BSON document" , PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P (offset ));
593
+ return & EG (uninitialized_zval );
594
+ }
595
+
596
+ if (!php_phongo_document_get (intern , Z_STRVAL_P (offset ), Z_STRLEN_P (offset ), rv )) {
597
+ // Exception already thrown
598
+ return & EG (uninitialized_zval );
599
+ }
600
+
601
+ return rv ;
602
+ }
603
+
604
+ void php_phongo_document_write_dimension (phongo_compat_object_handler_type * object , zval * offset , zval * value )
605
+ {
606
+ phongo_throw_exception (PHONGO_ERROR_LOGIC , "Cannot write to %s property" , ZSTR_VAL (php_phongo_document_ce -> name ));
607
+ }
608
+
609
+ int php_phongo_document_has_dimension (phongo_compat_object_handler_type * object , zval * member , int check_empty )
610
+ {
611
+ php_phongo_document_t * intern ;
612
+
613
+ intern = Z_OBJ_DOCUMENT (PHONGO_COMPAT_GET_OBJ (object ));
614
+
615
+ if (Z_TYPE_P (member ) != IS_STRING ) {
616
+ phongo_throw_exception (PHONGO_ERROR_RUNTIME , "Could not find key of type \"%s\" in BSON document" , PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P (member ));
617
+ return false;
618
+ }
619
+
620
+ return php_phongo_document_has (intern , Z_STRVAL_P (member ), Z_STRLEN_P (member ));
621
+ }
622
+
623
+ void php_phongo_document_unset_dimension (phongo_compat_object_handler_type * object , zval * offset )
624
+ {
625
+ phongo_throw_exception (PHONGO_ERROR_LOGIC , "Cannot unset %s property" , ZSTR_VAL (php_phongo_document_ce -> name ));
626
+ }
627
+
474
628
void php_phongo_document_init_ce (INIT_FUNC_ARGS )
475
629
{
476
- php_phongo_document_ce = register_class_MongoDB_BSON_Document (zend_ce_aggregate , zend_ce_serializable , php_phongo_type_ce );
630
+ php_phongo_document_ce = register_class_MongoDB_BSON_Document (zend_ce_aggregate , zend_ce_serializable , zend_ce_arrayaccess , php_phongo_type_ce );
477
631
php_phongo_document_ce -> create_object = php_phongo_document_create_object ;
478
632
479
633
#if PHP_VERSION_ID >= 80000
@@ -482,11 +636,19 @@ void php_phongo_document_init_ce(INIT_FUNC_ARGS)
482
636
483
637
memcpy (& php_phongo_handler_document , phongo_get_std_object_handlers (), sizeof (zend_object_handlers ));
484
638
PHONGO_COMPAT_SET_COMPARE_OBJECTS_HANDLER (document );
485
- php_phongo_handler_document .clone_obj = php_phongo_document_clone_object ;
486
- php_phongo_handler_document .get_debug_info = php_phongo_document_get_debug_info ;
487
- php_phongo_handler_document .get_properties = php_phongo_document_get_properties ;
488
- php_phongo_handler_document .free_obj = php_phongo_document_free_object ;
489
- php_phongo_handler_document .offset = XtOffsetOf (php_phongo_document_t , std );
639
+ php_phongo_handler_document .clone_obj = php_phongo_document_clone_object ;
640
+ php_phongo_handler_document .get_debug_info = php_phongo_document_get_debug_info ;
641
+ php_phongo_handler_document .get_properties = php_phongo_document_get_properties ;
642
+ php_phongo_handler_document .free_obj = php_phongo_document_free_object ;
643
+ php_phongo_handler_document .read_property = php_phongo_document_read_property ;
644
+ php_phongo_handler_document .write_property = php_phongo_document_write_property ;
645
+ php_phongo_handler_document .has_property = php_phongo_document_has_property ;
646
+ php_phongo_handler_document .unset_property = php_phongo_document_unset_property ;
647
+ php_phongo_handler_document .read_dimension = php_phongo_document_read_dimension ;
648
+ php_phongo_handler_document .write_dimension = php_phongo_document_write_dimension ;
649
+ php_phongo_handler_document .has_dimension = php_phongo_document_has_dimension ;
650
+ php_phongo_handler_document .unset_dimension = php_phongo_document_unset_dimension ;
651
+ php_phongo_handler_document .offset = XtOffsetOf (php_phongo_document_t , std );
490
652
}
491
653
492
654
bool phongo_document_new (zval * object , bson_t * bson , bool copy )
0 commit comments