Skip to content

Commit

Permalink
Add positive macro, refactor MacroProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
the-pulli committed Apr 8, 2024
1 parent 7fedf2f commit 1830884
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 21 deletions.
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"illuminate/collections": "^10.0|^11.0"
},
"require-dev": {
"laravel/pint": "^1.15",
"orchestra/testbench": "^8.17",
"pestphp/pest": "^2.16"
},
Expand Down
22 changes: 8 additions & 14 deletions src/CollectionMacroServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)()));
}
}
1 change: 1 addition & 0 deletions src/HasMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
Expand Down
2 changes: 1 addition & 1 deletion src/Macros/MapToCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
15 changes: 15 additions & 0 deletions src/Macros/Positive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Pulli\CollectionMacros\Macros;

use Closure;

class Positive
{
public function __invoke(): Closure
{
return function (): bool {
return $this->count() > 0;
};
}
}
8 changes: 8 additions & 0 deletions src/StaticCollectionMacroProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Pulli\CollectionMacros;

class StaticCollectionMacroProvider
{
use HasMacros;
}
4 changes: 2 additions & 2 deletions src/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

use Illuminate\Support\Collection;

if (!class_exists('\Illuminate\Foundation\Application')) {
Collection::make(\Pulli\CollectionMacros\CollectionMacroServiceProvider::macros())
if (! class_exists('\Illuminate\Foundation\Application')) {
Collection::make(\Pulli\CollectionMacros\StaticCollectionMacroProvider::macros())
->reject(fn ($class, $macro) => Collection::hasMacro($macro))
->each(fn ($class, $macro) => Collection::macro($macro, (new $class())()));
}
17 changes: 17 additions & 0 deletions tests/Macros/PositiveTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use Illuminate\Support\Collection;

describe('positive macro', function () {
it('returns true for a collection with more than 0 items', function () {
$data = Collection::make(['test']);

expect($data->positive())->toBeTrue();
});

it('returns false for a collection with 0 items', function () {
$data = Collection::make();

expect($data->positive())->toBeFalse();
});
});

0 comments on commit 1830884

Please sign in to comment.