diff --git a/~dev_rating/modules/kotwig/vendor/twig/lib/Twig/Environment.php b/~dev_rating/modules/kotwig/vendor/twig/lib/Twig/Environment.php index d3ba9be91ff3f98c2da00003e35c50d97fa1f6f0..2de2cd1717a94ce9e6e1458bcd8b54b601cc65bd 100644 --- a/~dev_rating/modules/kotwig/vendor/twig/lib/Twig/Environment.php +++ b/~dev_rating/modules/kotwig/vendor/twig/lib/Twig/Environment.php @@ -111,6 +111,7 @@ class Twig_Environment $this->addExtension(new Twig_Extension_Core()); $this->addExtension(new Twig_Extension_Escaper($options['autoescape'])); $this->addExtension(new Twig_Extension_Optimizer($options['optimizations'])); + $this->addExtension(new Twig_Extension_SortByField()); $this->extensionInitialized = false; $this->staging = new Twig_Extension_Staging(); } diff --git a/~dev_rating/modules/kotwig/vendor/twig/lib/Twig/Extension/SortByField.php b/~dev_rating/modules/kotwig/vendor/twig/lib/Twig/Extension/SortByField.php new file mode 100644 index 0000000000000000000000000000000000000000..ea916215d9b2294fbb8bc31525470e0313edcab3 --- /dev/null +++ b/~dev_rating/modules/kotwig/vendor/twig/lib/Twig/Extension/SortByField.php @@ -0,0 +1,72 @@ +<?php + +/** + * User: Victor Häggqvist + * Date: 3/4/15 + * Time: 2:07 AM + * + * The base of the filter is borrowed from https://github.com/dannynimmo/craftcms-sortbyfield + * + * I have extended it to also sort array structures + */ +class Twig_Extension_SortByField extends \Twig_Extension { + public function getName() { + return 'sortbyfield'; + } + public function getFilters() { + return array( + new \Twig_SimpleFilter('sortbyfield', array($this, 'sortByFieldFilter')) + ); + } + /** + * The "sortByField" filter sorts an array of entries (objects or arrays) by the specified field's value + * + * Usage: {% for entry in master.entries|sortbyfield('ordering', 'desc') %} + */ + public function sortByFieldFilter($content, $sort_by = null, $direction = 'asc') { + if (!is_array($content)) { + throw new \InvalidArgumentException('Variable passed to the sortByField filter is not an array'); + } elseif ($sort_by === null) { + throw new Exception('No sort by parameter passed to the sortByField filter'); + } elseif (!self::isSortable($content[0], $sort_by)) { + throw new Exception('Entries passed to the sortByField filter do not have the field "' . $sort_by . '"'); + } else { + // Unfortunately have to suppress warnings here due to __get function + // causing usort to think that the array has been modified: + // usort(): Array was modified by the user comparison function + @usort($content, function ($a, $b) use($sort_by, $direction) { + $flip = ($direction === 'desc') ? -1 : 1; + if (is_array($a)) + $a_sort_value = $a[$sort_by]; + else + $a_sort_value = $a->$sort_by; + if (is_array($b)) + $b_sort_value = $b[$sort_by]; + else + $b_sort_value = $b->$sort_by; + if($a_sort_value == $b_sort_value) { + return 0; + } else if($a_sort_value > $b_sort_value) { + return (1 * $flip); + } else { + return (-1 * $flip); + } + }); + } + return $content; + } + /** + * Validate the passed $item to check if it can be sorted + * @param $item mixed Collection item to be sorted + * @param $field string + * @return bool If collection item can be sorted + */ + private static function isSortable($item, $field) { + if (is_array($item)) + return array_key_exists($field, $item); + elseif (is_object($item)) + return property_exists($item, $field); + else + return false; + } +}