예제 #1
0
 /**
  * Returns -1 if $object1 < $object2, 1 if $object1 > $object2, or 0 if $object1 == $object2
  *
  * @param $object1 object
  * @param $object2 object
  * @return integer -1, 0 or 1
  */
 public function compare($object1, $object2)
 {
     if (is_object($object1) && is_object($object2)) {
         // Comparable objects : use their compare method
         if ($this->use_compare_method && $object1 instanceof Comparable && (is_a($object1, get_class($object2)) || is_a($object2, get_class($object1)))) {
             return call_user_func_array([get_class($object1), 'compare'], [$object1, $object2]);
         } else {
             $path1 = new Object_Property_Path($object1);
             $path2 = new Object_Property_Path($object2);
             foreach ($this->properties_path as $property_path) {
                 if ($reverse = $property_path instanceof Reverse) {
                     $property_path = $property_path->column;
                 }
                 $value1 = $path1->getValue($property_path);
                 $value2 = $path2->getValue($property_path);
                 if (is_object($value1) || is_object($value2)) {
                     $comparator = new Comparator(get_class($value1));
                     $result = $comparator->compare($value1, $value2);
                 } else {
                     $result = $value1 < $value2 ? $reverse ? 1 : -1 : ($value1 > $value2 ? $reverse ? -1 : 1 : 0);
                 }
                 if ($result) {
                     return $result;
                 }
             }
             return 0;
         }
     }
     // at least one of the two values is not an object : compare as values
     if (is_object($object1)) {
         $object1 = strval($object1);
     }
     if (is_object($object2)) {
         $object2 = strval($object2);
     }
     return $object1 < $object2 ? -1 : ($object1 > $object2 ? 1 : 0);
 }
예제 #2
0
파일: Sort.php 프로젝트: TuxBoy/Demo-saf
 /**
  * Sort a collection of objects using current sort columns configuration
  *
  * @param $objects object[]
  * @return object[]
  */
 public function sortObjects(&$objects)
 {
     $comparator = new Comparator($this->class_name, $this->columns);
     $comparator->use_compare_method = false;
     $comparator->sort($objects);
     return $objects;
 }