MCMatters/laravel-helpers

View on GitHub
src/Macros/CollectionMacros.php

Summary

Maintainability
A
0 mins
Test Coverage
A
90%
<?php

declare(strict_types=1);

namespace McMatters\Helpers\Macros;

use Closure;
use Illuminate\Support\Collection;
use McMatters\Helpers\Helpers\ArrayHelper;

class CollectionMacros extends AbstractMacroable
{
    protected function registerFilterMap(): void
    {
        Collection::macro('filterMap', function (Closure $condition, Closure $map): Collection {
            $data = [];

            foreach ($this->all() as $item) {
                if ($condition($item)) {
                    $data[] = $map($item);
                }
            }

            return new Collection($data);
        });
    }

    protected function registerFirstKey(): void
    {
        Collection::macro('firstKey', function (): int|string|null {
            return ArrayHelper::firstKey($this->all());
        });
    }
}