示例#1
0
 public function testToPairs()
 {
     $obj1 = new \stdClass();
     $obj2 = new \stdClass();
     $pairs = [[$obj1, 'foo'], [$obj2, 'bar'], [12, $obj1]];
     $dict = Dictionary::fromPairs($pairs);
     $this->assertEquals($pairs, $dict->toPairs());
 }
示例#2
0
 public static function sortBy($array, $callback, $direction = 'ASC')
 {
     if (!is_callable($callback)) {
         throw new \Twig_Error_Runtime(sprintf('Second argument of "sort_by" must be callable, but is "%s".', gettype($callback)));
     }
     if (!is_array($array) && !$array instanceof \Traversable) {
         throw new \Twig_Error_Runtime(sprintf('First argument of "sort_by" must be array or Traversable, but is "%s".', gettype($array)));
     }
     if ($array instanceof \Traversable) {
         if ($array instanceof Dictionary) {
             $array = $array->getCopy();
         } else {
             $array = Dictionary::fromArray($array);
         }
         return $array->sortBy($callback, $direction);
     } else {
         $direction = strtoupper($direction) === 'DESC' ? SORT_DESC : SORT_ASC;
         $order = self::map($array, $callback);
         array_multisort($order, $direction, SORT_REGULAR, $array);
         return $array;
     }
 }