2021-11-02 23:37:19 -07:00
|
|
|
<?php
|
2025-03-29 10:50:03 +00:00
|
|
|
|
2022-02-14 20:48:15 -08:00
|
|
|
/**
|
2024-03-09 03:19:55 -08:00
|
|
|
* Base Collection class.
|
2021-11-02 23:37:19 -07:00
|
|
|
*
|
2022-02-14 20:48:15 -08:00
|
|
|
* @package NamelessMC\Collections
|
|
|
|
|
* @author Samerton
|
|
|
|
|
* @version 2.0.0-pr13
|
|
|
|
|
* @license MIT
|
2021-11-02 23:37:19 -07:00
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
class Collection
|
|
|
|
|
{
|
2022-02-14 20:48:15 -08:00
|
|
|
/** @var CollectionItemBase[] */
|
2021-11-02 23:37:19 -07:00
|
|
|
private array $_items;
|
|
|
|
|
|
2024-03-09 03:19:55 -08:00
|
|
|
public function __construct()
|
|
|
|
|
{
|
2021-11-02 23:37:19 -07:00
|
|
|
$this->_items = [];
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-09 03:19:55 -08:00
|
|
|
public function addItem(CollectionItemBase $item): void
|
|
|
|
|
{
|
2021-11-02 23:37:19 -07:00
|
|
|
$this->_items[] = $item;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-30 21:15:17 -07:00
|
|
|
/**
|
|
|
|
|
* @return CollectionItemBase[]
|
|
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
public function getEnabledItems(): array
|
|
|
|
|
{
|
2021-11-02 23:37:19 -07:00
|
|
|
$items = [];
|
|
|
|
|
|
|
|
|
|
foreach ($this->_items as $item) {
|
|
|
|
|
if ($item->isEnabled()) {
|
|
|
|
|
$items[] = $item;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-30 21:15:17 -07:00
|
|
|
uasort($items, static function (CollectionItemBase $a, CollectionItemBase $b) {
|
2021-11-02 23:37:19 -07:00
|
|
|
return $a->getOrder() - $b->getOrder();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return $items;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-30 21:15:17 -07:00
|
|
|
/**
|
|
|
|
|
* @return CollectionItemBase[]
|
|
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
public function getAllItems(): array
|
|
|
|
|
{
|
2021-11-02 23:37:19 -07:00
|
|
|
$items = $this->_items;
|
2022-03-30 21:15:17 -07:00
|
|
|
uasort($items, static function (CollectionItemBase $a, CollectionItemBase $b) {
|
2021-11-02 23:37:19 -07:00
|
|
|
return $a->getOrder() - $b->getOrder();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return $items;
|
|
|
|
|
}
|
|
|
|
|
}
|