diff --git a/README.md b/README.md index 8a0e8a3..ddbbb07 100644 --- a/README.md +++ b/README.md @@ -18,25 +18,35 @@ composer require pulli/collection-macros - mapToCollectionFrom - mapToCollection +- positive - recursiveToArrayFrom - recursiveToArray ### `mapToCollectionFrom` -Maps all arrays/objects recursively to a collection object of collections, which allow nested function calling. +Static method: Maps all arrays/objects recursively to a collection object of collections, which allow nested function calling. ```php -$collection = Collection::mapToCollectionFrom([['test' => 1],2,3]); +$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); +$collection = Collection::mapToCollectionFrom([Item(), Item()], true); +``` + +### `positive` + +Returns a boolean value, if the collection contains elements or not. + +```php +Collection::make([1, 2, 3])->positive() // returns true +Collection::make()->positive() // returns false ``` ### `recursiveToArrayFrom` -Like `mapToCollectionFrom` it maps all arrays/objects recursively to an array. +Static method: Like `mapToCollectionFrom` it maps all arrays/objects recursively to an array. ```php // Item has a toArray() public method, then it can be wrapped into the collection like this: diff --git a/composer.json b/composer.json index dfb28ac..df9ca85 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,7 @@ "illuminate/collections": "^10.0|^11.0" }, "require-dev": { + "laravel/pint": "^1.15", "orchestra/testbench": "^8.17", "pestphp/pest": "^2.16" }, diff --git a/src/CollectionMacroServiceProvider.php b/src/CollectionMacroServiceProvider.php index 52c9649..6ae3b0d 100644 --- a/src/CollectionMacroServiceProvider.php +++ b/src/CollectionMacroServiceProvider.php @@ -4,20 +4,14 @@ use Illuminate\Support\Collection; -if (class_exists('\Illuminate\Support\ServiceProvider')) { - class CollectionMacroServiceProvider extends \Illuminate\Support\ServiceProvider - { - use HasMacros; +class CollectionMacroServiceProvider extends \Illuminate\Support\ServiceProvider +{ + use HasMacros; - public function register(): void - { - Collection::make(self::macros()) - ->reject(fn ($class, $macro) => Collection::hasMacro($macro)) - ->each(fn ($class, $macro) => Collection::macro($macro, app($class)())); - } - } -} else { - class CollectionMacroServiceProvider { - use HasMacros; + public function register(): void + { + Collection::make(self::macros()) + ->reject(fn ($class, $macro) => Collection::hasMacro($macro)) + ->each(fn ($class, $macro) => Collection::macro($macro, app($class)())); } } diff --git a/src/HasMacros.php b/src/HasMacros.php index 9c9e168..bd693bd 100644 --- a/src/HasMacros.php +++ b/src/HasMacros.php @@ -9,6 +9,7 @@ public static function macros(): array return [ 'mapToCollection' => \Pulli\CollectionMacros\Macros\MapToCollection::class, 'mapToCollectionFrom' => \Pulli\CollectionMacros\Macros\MapToCollectionFrom::class, + 'positive' => \Pulli\CollectionMacros\Macros\Positive::class, 'recursiveToArray' => \Pulli\CollectionMacros\Macros\RecursiveToArray::class, 'recursiveToArrayFrom' => \Pulli\CollectionMacros\Macros\RecursiveToArrayFrom::class, ]; diff --git a/src/Macros/MapToCollection.php b/src/Macros/MapToCollection.php index 42444bd..7409795 100644 --- a/src/Macros/MapToCollection.php +++ b/src/Macros/MapToCollection.php @@ -11,7 +11,7 @@ public function __invoke(): Closure { return function (array $ary = [], bool $deep = false): Collection { $ary = static::mapToCollectionFrom($ary, $deep); - $data = static::mapToCollectionFrom($this->all(), $deep); + $data = static::mapToCollectionFrom($this->all(), $deep); if ($ary->isNotEmpty()) { return $data->merge([$ary]); diff --git a/src/Macros/Positive.php b/src/Macros/Positive.php new file mode 100644 index 0000000..ef5587b --- /dev/null +++ b/src/Macros/Positive.php @@ -0,0 +1,15 @@ +count() > 0; + }; + } +} diff --git a/src/StaticCollectionMacroProvider.php b/src/StaticCollectionMacroProvider.php new file mode 100644 index 0000000..8314124 --- /dev/null +++ b/src/StaticCollectionMacroProvider.php @@ -0,0 +1,8 @@ +reject(fn ($class, $macro) => Collection::hasMacro($macro)) ->each(fn ($class, $macro) => Collection::macro($macro, (new $class())())); } diff --git a/tests/Macros/PositiveTest.php b/tests/Macros/PositiveTest.php new file mode 100644 index 0000000..aaed13b --- /dev/null +++ b/tests/Macros/PositiveTest.php @@ -0,0 +1,17 @@ +positive())->toBeTrue(); + }); + + it('returns false for a collection with 0 items', function () { + $data = Collection::make(); + + expect($data->positive())->toBeFalse(); + }); +});