pulli / laravel-collection-macros
Package for some custom collection macros.
Fund package maintenance!
the-pulli
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/pulli/laravel-collection-macros
Requires
- php: ^8.4
- illuminate/collections: ^11.0||^12.0
- illuminate/contracts: ^11.0||^12.0
- illuminate/support: ^11.0||^12.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.8
- orchestra/testbench: ^10.0.0||^9.0.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
README
Contains some handy collection macros.
Installation
You can install the package via composer:
composer require pulli/laravel-collection-macros
You can publish the service provider via:
php artisan vendor:publish --tag="pulli-collection-macros-provider"
You can publish the config file with:
php artisan vendor:publish --tag="pulli-collection-macros-config"
This is the contents of the published config file:
return [ 'auto-update' => true, ];
Usage
Macros
- mapToCollectionFrom
- mapToCollection
- positive
- recursiveToArrayFrom
- recursiveToArray
mapToCollectionFrom
Static method: Maps all arrays/objects recursively to a collection object of collections, which allow nested function calling.
$collection = Collection::mapToCollectionFrom([['test' => 1], 2, 3]); $collection->get(0)->get('test'); // returns 1 // Item has a toArray() public method, then it can be wrapped into a collection like this: $collection = Collection::mapToCollectionFrom([Item(), Item()], true);
positive
Returns a boolean value, if the collection contains elements or not.
Collection::make([1, 2, 3])->positive() // returns true Collection::make()->positive() // returns false
recursiveToArrayFrom
Static method: Like mapToCollectionFrom it maps all arrays/objects recursively to an array.
// Item has a toArray() public method, then it can be wrapped into the collection like this: $array = Collection::recursiveToArrayFrom(['item1' => Item(), 'item2' => Item()]);
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.