pulli / laravel-collection-macros
Package for some custom collection macros.
Fund package maintenance!
the-pulli
Installs: 6
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
evenfirstAndLastfirstAndLastKeyimplodeToStringablejoinToStringablemapToCollectionmapToCollectionFromoddpositiverecursiveToArrayFromrecursiveToArray
even
Filter the collection to only integer values and return the even ones.
$collection = Collection::range(0, 6)->even(); // returns [0, 2, 4, 6] // optional you can pass true as parameter to preserve the original array keys $collection = Collection::range(0, 6)->even(true); // returns [0 => 0, 2 => 2, 4 => 4, 6 => 6]
explode
Returns a collection from an exploded string. It uses the same signature as the native explode function.
$collection = Collection::explode(';', 'Twenty;Banana;Boats'); // returns ['Twenty', 'Banana', 'Boats']
firstAndLast
Returns the first and last element as array. Optional a callable and a default value for the first and last element can be specified.
[$first, $last] = Collection::make(['Jane', 'John', 'Joe'])->firstAndLast(); // $first = 'Jane', $last = 'Joe'
firstAndLastKey
Returns the first and last key as array. Optional a callable and a default value for the first and last element can be specified.
[$first, $last] = Collection::make(['Jane', 'John', 'Joe'])->firstAndLastKey(); // $first = 0, $last = 2
implodeToStringable
Implodes the collection to a Stringable object.
$collection = Collection::make(['Jane', 'John'])->implodeToStringable(', '); // Stringable of "Jane, John"
joinToStringable
Joins the collection to a Stringable object.
$collection = Collection::make(['Jane', 'John', 'Jack'])->joinToStringable(', ', ' and '); // Stringable of "Jane, John and Jack"
mapToCollection
Maps all arrays/objects recursively to a collection object of collections, which allow nested function calling.
$collection = Collection::make([['test' => 1], 2, 3])->mapToCollection([4, 5]); $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::make([Item(), Item()])->mapToCollection([Item()]);
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);
odd
Filter the collection to only integer values and return the odd ones.
$collection = Collection::range(0, 6)->odd(); // returns [1, 3, 5] // optional you can pass true as parameter to preserve the original array keys $collection = Collection::range(0, 6)->odd(true); // returns [1 => 1, 3 => 3, 5 => 5]
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
recursiveToArray
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::make(['item1' => Item(), 'item2' => Item()])->recursiveToArray();
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.