Files
monica/app/Helpers/CollectionHelper.php
T

86 lines
2.4 KiB
PHP

<?php
namespace App\Helpers;
use Illuminate\Support\Collection;
class CollectionHelper
{
/**
* Sort the collection using the given callback.
*
* @param callable|string $callback
* @param int $options
* @param bool $descending
* @return static
*/
public static function sortByCollator($collect, $callback, $options = \Collator::SORT_STRING, $descending = false)
{
$results = [];
$callback = static::valueRetriever($callback);
// First we will loop through the items and get the comparator from a callback
// function which we were given. Then, we will sort the returned values and
// and grab the corresponding values for the sorted keys from this array.
foreach ($collect->all() as $key => $value) {
$results[$key] = $callback($value, $key);
}
// Using Collator to sort the array, with locale-sensitive sort ordering support.
static::getCollator()->asort($results, $options);
if ($descending) {
$results = array_reverse($results);
}
// Once we have sorted all of the keys in the array, we will loop through them
// and grab the corresponding model so we can set the underlying items list
// to the sorted version. Then we'll just return the collection instance.
foreach (array_keys($results) as $key) {
$results[$key] = $collect->get($key);
}
return new Collection($results);
}
/**
* Get a Collator object for the locale or current locale.
*
* @param string
* @return \Collator
*/
public static function getCollator($locale = null)
{
static $collators = [];
if (! $locale) {
$locale = app()->getLocale();
}
if (! array_has($collators, $locale)) {
$collator = new \Collator($locale);
$collators[$locale] = $collator;
return $collator;
}
return $collators[$locale];
}
/**
* Get a value retrieving callback.
*
* @param string $value
* @return callable
*/
private static function valueRetriever($value)
{
if (! is_string($value) && is_callable($value)) {
return $value;
}
return function ($item) use ($value) {
return data_get($item, $value);
};
}
}