示例#1
0
 /**
  * Utility function to sort an array of objects on a given field
  *
  * @param   array  $a              An array of objects
  * @param   mixed  $k              The key (string) or a array of key to sort on
  * @param   mixed  $direction      Direction (integer) or an array of direction to sort in [1 = Ascending] [-1 = Descending]
  * @param   mixed  $caseSensitive  Boolean or array of booleans to let sort occur case sensitive or insensitive
  * @param   mixed  $locale         Boolean or array of booleans to let sort occur using the locale language or not
  *
  * @return  array  The sorted array of objects
  *
  * @since   2.0
  */
 public static function sortObjects(array $a, $k, $direction = 1, $caseSensitive = true, $locale = false)
 {
     if (!is_array($locale) || !is_array($locale[0])) {
         $locale = array($locale);
     }
     $sortCase = (array) $caseSensitive;
     $sortDirection = (array) $direction;
     $key = (array) $k;
     $sortLocale = $locale;
     usort($a, function ($a, $b) use($sortCase, $sortDirection, $key, $sortLocale) {
         for ($i = 0, $count = count($key); $i < $count; $i++) {
             if (isset($sortDirection[$i])) {
                 $direction = $sortDirection[$i];
             }
             if (isset($sortCase[$i])) {
                 $caseSensitive = $sortCase[$i];
             }
             if (isset($sortLocale[$i])) {
                 $locale = $sortLocale[$i];
             }
             $va = $a->{$key}[$i];
             $vb = $b->{$key}[$i];
             if ((is_bool($va) || is_numeric($va)) && (is_bool($vb) || is_numeric($vb))) {
                 $cmp = $va - $vb;
             } elseif ($caseSensitive) {
                 $cmp = Utf8String::strcmp($va, $vb, $locale);
             } else {
                 $cmp = Utf8String::strcasecmp($va, $vb, $locale);
             }
             if ($cmp > 0) {
                 return $direction;
             }
             if ($cmp < 0) {
                 return -$direction;
             }
         }
         return 0;
     });
     return $a;
 }
示例#2
0
 /**
  * Test...
  *
  * @param   string  $string1  @todo
  * @param   string  $string2  @todo
  * @param   string  $locale   @todo
  * @param   string  $expect   @todo
  *
  * @return  array
  *
  * @covers        Windwalker\String\Utf8String::strcmp
  * @dataProvider  seedTestStrcmp
  * @since         1.0
  */
 public function testStrcmp($string1, $string2, $locale, $expect)
 {
     // Convert the $locale param to a string if it is an array
     if (is_array($locale)) {
         $locale = "'" . implode("', '", $locale) . "'";
     }
     if (substr(php_uname(), 0, 6) == 'Darwin' && $locale != false) {
         $this->markTestSkipped('Darwin bug prevents foreign conversion from working properly');
     } elseif ($locale != false && !setlocale(LC_COLLATE, $locale)) {
         // If the locale is not available, we can't have to transcode the string and can't reliably compare it.
         $this->markTestSkipped("Locale {$locale} is not available.");
     } else {
         $actual = Utf8String::strcmp($string1, $string2, $locale);
         if ($actual != 0) {
             $actual = $actual / abs($actual);
         }
         $this->assertEquals($expect, $actual);
     }
 }