Skip to content

Commit

Permalink
refactor: simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammad-Alavi committed Dec 31, 2024
1 parent a5eecbc commit 42d9500
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 89 deletions.
3 changes: 1 addition & 2 deletions src/Foundation/Providers/ApiatoServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ class ApiatoServiceProvider extends AggregateServiceProvider

protected $providers = [
GeneratorsServiceProvider::class,
CollectionMacroServiceProvider::class,
ConfigMacroServiceProvider::class,
MacroServiceProvider::class,
];

public function register(): void
Expand Down
24 changes: 0 additions & 24 deletions src/Foundation/Providers/CollectionMacroServiceProvider.php

This file was deleted.

25 changes: 0 additions & 25 deletions src/Foundation/Providers/ConfigMacroServiceProvider.php

This file was deleted.

35 changes: 35 additions & 0 deletions src/Foundation/Providers/MacroServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Apiato\Foundation\Providers;

use Apiato\Abstract\Providers\AggregateServiceProvider;
use Illuminate\Config\Repository;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Config;
use Vinkla\Hashids\Facades\Hashids;

final class MacroServiceProvider extends AggregateServiceProvider
{
public function boot(): void
{
if (!Collection::hasMacro('containsDecodedHash')) {
Collection::macro('containsDecodedHash',
/**
* Decodes a hashed value and checks if the decoded value exists in the collection under the specified key.
*/
function (string $hashedValue, string $key = 'id'): bool {
/* @var Collection $this */
return $this->contains($key, Hashids::decode($hashedValue)[0]);
});
}

if (!Config::hasMacro('unset')) {
Config::macro('unset',
function (array|string|int|float $key): void {
/* @var Repository $this */
Arr::forget($this->items, $key);
});
}
}
}
21 changes: 0 additions & 21 deletions src/Foundation/Support/Macros/Collection/ContainsDecodedHash.php

This file was deleted.

17 changes: 0 additions & 17 deletions src/Foundation/Support/Macros/Config/UnsetKey.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Tests\Unit\Foundation\Support\Providers;

use Apiato\Foundation\Providers\MacroServiceProvider;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Config;

describe(class_basename(MacroServiceProvider::class), function (): void {
it('register Collection macros', function (): void {
expect(Collection::hasMacro('containsDecodedHash'))->toBeTrue();
});

it('register Config macros', function (): void {
expect(Config::hasMacro('unset'))->toBeTrue();
});
})->covers(MacroServiceProvider::class);

0 comments on commit 42d9500

Please sign in to comment.