letsgoi / custom-list
PHP Custom List package
Installs: 4 586
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 6
Forks: 0
Open Issues: 0
Requires
- php: ^8.0
Requires (Dev)
- letsgoi/php-code-style: ^1.4
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2024-11-12 08:38:54 UTC
README
Class to wrap array of items to force same type. This is for avoid the php array type hinting problem.
Requirements
- PHP >= 8.2
Usage
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, ...)
Available methods
get($key = null)
Return item by key or all list without it:
$list->get(0); // 'item' $list->get(); // ['item', 'item', ...]
add($item)
Append item to list:
$list->add($item);
merge(... $customLists)
Merges the elements of one or more custom lists together
$list->merge($list1, $list2 ...);
Testing
Run tests:
composer test
Contributing
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.