@@ -401,12 +401,67 @@ 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 (mongoc_write_concern_get_w (wc1 ) != mongoc_write_concern_get_w (wc2 )) {
430+ return -1 ;
431+ }
432+
433+ const char * wtag1 = NULL ;
434+ const char * wtag2 = NULL ;
435+
436+ wtag1 = mongoc_write_concern_get_wtag (wc1 );
437+ wtag2 = mongoc_write_concern_get_wtag (wc2 );
438+
439+ if (wtag1 == wtag2 ) {
440+ return 0 ;
441+ }
442+
443+ if (wtag1 == NULL || wtag2 == NULL ) {
444+ return -1 ;
445+ }
446+
447+ if (strcmp (wtag1 , wtag2 ) != 0 ) {
448+ return -1 ;
449+ }
450+
451+ if (mongoc_write_concern_get_journal (wc1 ) != mongoc_write_concern_get_journal (wc2 )) {
452+ return -1 ;
453+ }
454+
455+ return 0 ;
456+ }
457+
404458void phongo_writeconcern_init_ce (INIT_FUNC_ARGS )
405459{
406460 phongo_writeconcern_ce = register_class_MongoDB_Driver_WriteConcern (phongo_serializable_ce );
407461 phongo_writeconcern_ce -> create_object = phongo_writeconcern_create_object ;
408462
409463 memcpy (& phongo_handler_writeconcern , phongo_get_std_object_handlers (), sizeof (zend_object_handlers ));
464+ phongo_handler_writeconcern .compare = phongo_writeconcern_compare_objects ;
410465 phongo_handler_writeconcern .get_debug_info = phongo_writeconcern_get_debug_info ;
411466 phongo_handler_writeconcern .get_properties = phongo_writeconcern_get_properties ;
412467 phongo_handler_writeconcern .free_obj = phongo_writeconcern_free_object ;
0 commit comments