-
-
Save jsakhil/36eb8a21a03ba4080fe138c4c44df602 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @license MIT | |
* @author Ruben Decleyn [email protected]> | |
* @created 12/02/2017 | |
*/ | |
namespace App\TwigExtensions; | |
class CustomSort extends \Twig_Extension | |
{ | |
public function getFilters() | |
{ | |
return array( | |
new \Twig_SimpleFilter('csort', function ($trav, array $options = []) { | |
return $this->customSort($trav, $options); | |
}, ['is_variadic' => true]), | |
); | |
} | |
/** | |
* Custom Sorter for twig | |
* The first option is always the compare function to use. | |
* Customer compare functions are not yet supported. | |
* The other arguments are the keys of the multi-dimensional array to traverse down to. | |
* | |
* If you want to compare $a['some']['deep]['level'] to $a['some']['deep]['level'] | |
* use {{ data|csort( 'strcasecmp', 'some', 'deep', 'level') }} | |
* | |
* @param $array array Array to be sorted | |
* @param $options array Levels of the multidimensial array to be sorted | |
* @return array The Sorted array | |
*/ | |
public function customSort($array, $options) | |
{ | |
usort($array, function ($a, $b) use ($options) { | |
$sortFunc = array_shift($options); | |
return $sortFunc($this->getToLevel($a, $options), $this->getToLevel($b, $options)); | |
}); | |
return $array; | |
} | |
/** | |
* Recursive function to get to the right element of the multi-dimensial array to compare | |
* | |
* @param $trav | |
* @param array $levels | |
* @return mixed | |
*/ | |
protected function getToLevel($trav, array $levels = []) | |
{ | |
if (count($levels) > 1) { | |
$lvl = array_shift($levels); | |
return $this->getToLevel($trav[$lvl], $levels); | |
} elseif (count($levels) === 1) { | |
$lvl = array_shift($levels); | |
return $trav[$lvl]; | |
} else { | |
return $trav; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment