Class to wrap array of items to force same type. This is for avoid the php array type hinting problem.
- PHP >= 8.2
Extends the CustomList
abstract class and set the type of the items with getListType
method:
use Letsgoi\CustomList\CustomList;
class ItemList extends CustomList
{
protected function getListType(): string
{
return Item::class;
}
}
//
$items = [new Item(), new Item(), ...];
$list = new ItemList($items);
To iterate items:
foreach ($list as $item) {
//
}
You can use the list as an array (set, get, ...)
Return item by key or all list without it:
$list->get(0); // 'item'
$list->get(); // ['item', 'item', ...]
Append item to list:
$list->add($item);
Merges the elements of one or more custom lists together
$list->merge($list1, $list2 ...);
Run tests:
composer test
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.