@@ -401,12 +401,70 @@ static HashTable* phongo_writeconcern_get_properties(zend_object* object)
401401 return phongo_writeconcern_get_properties_hash (object , false, false, false);
402402}
403403
404+ /**
405+ * Note: only equality comparison is supported for objects.
406+ * Inequality will always return -1, making sorting arbitrary.
407+ */
408+ static int phongo_writeconcern_compare_objects (zval * o1 , zval * o2 )
409+ {
410+ ZEND_COMPARE_OBJECTS_FALLBACK (o1 , o2 );
411+
412+ phongo_writeconcern_t * intern1 ;
413+ phongo_writeconcern_t * intern2 ;
414+
415+ const mongoc_write_concern_t * wc1 = NULL ;
416+ const mongoc_write_concern_t * wc2 = NULL ;
417+
418+ intern1 = Z_WRITECONCERN_OBJ_P (o1 );
419+ intern2 = Z_WRITECONCERN_OBJ_P (o2 );
420+
421+ if (intern1 && intern1 -> write_concern ) {
422+ wc1 = intern1 -> write_concern ;
423+ }
424+
425+ if (intern2 && intern2 -> write_concern ) {
426+ wc2 = intern2 -> write_concern ;
427+ }
428+
429+ if (wc1 == wc2 ) {
430+ return 0 ;
431+ }
432+
433+ if (wc1 == NULL || wc2 == NULL ) {
434+ return -1 ;
435+ }
436+
437+ if (mongoc_write_concern_get_w (wc1 ) != mongoc_write_concern_get_w (wc2 )) {
438+ return -1 ;
439+ }
440+
441+ const char * wtag1 = NULL ;
442+ const char * wtag2 = NULL ;
443+
444+ wtag1 = mongoc_write_concern_get_wtag (wc1 );
445+ wtag2 = mongoc_write_concern_get_wtag (wc2 );
446+
447+ if (wtag1 == wtag2 ) {
448+ } else if (wtag1 == NULL || wtag2 == NULL ) {
449+ return -1 ;
450+ } else if (strcmp (wtag1 , wtag2 ) != 0 ) {
451+ return -1 ;
452+ }
453+
454+ if (mongoc_write_concern_get_journal (wc1 ) != mongoc_write_concern_get_journal (wc2 )) {
455+ return -1 ;
456+ }
457+
458+ return 0 ;
459+ }
460+
404461void phongo_writeconcern_init_ce (INIT_FUNC_ARGS )
405462{
406463 phongo_writeconcern_ce = register_class_MongoDB_Driver_WriteConcern (phongo_serializable_ce );
407464 phongo_writeconcern_ce -> create_object = phongo_writeconcern_create_object ;
408465
409466 memcpy (& phongo_handler_writeconcern , phongo_get_std_object_handlers (), sizeof (zend_object_handlers ));
467+ phongo_handler_writeconcern .compare = phongo_writeconcern_compare_objects ;
410468 phongo_handler_writeconcern .get_debug_info = phongo_writeconcern_get_debug_info ;
411469 phongo_handler_writeconcern .get_properties = phongo_writeconcern_get_properties ;
412470 phongo_handler_writeconcern .free_obj = phongo_writeconcern_free_object ;
0 commit comments