/**
* Sorts items in this collection.
* e.g:.
*
* ```
* $itemCollection = new ItemCollection();
* // warm-up...
* $items = $itemCollection->sortItems('date', true)->all();
* ```
*
* @param string $attribute The name of the attribute used to sort
* @param bool $descending Is descending sort?
* @param string[] $collections Only the items belong to Collections will be affected
*
* @return Yosymfony\Spress\Core\Support\ItemCollection An intance of itself
*/
public function sortItems($attribute, $descending = true, array $collections = [])
{
$itemCollections = $this->all($collections, true);
$callback = function ($key, ItemInterface $item) use($attribute) {
$attributes = $item->getAttributes();
return isset($attributes[$attribute]) === true ? $attributes[$attribute] : null;
};
foreach ($itemCollections as $collection => $items) {
$arr = new ArrayWrapper($items);
$itemsSorted = $arr->sortBy($callback, null, SORT_REGULAR, $descending);
$this->itemCollections[$collection] = $itemsSorted;
}
$this->items = [];
foreach ($this->itemCollections as $collection => $items) {
$this->items = array_merge($this->items, $items);
}
return $this;
}