From 4ccbf5f807d426465d34c38af7f723276ae44b38 Mon Sep 17 00:00:00 2001 From: Aymane Dara Hlamnach Date: Fri, 12 Jul 2024 16:10:44 +0100 Subject: [PATCH] feat: add foggables (#5) --- src/Contracts/Foggable.php | 12 ++++++++++++ src/FogGen.php | 11 ++++++++++- tests/Feature/FoggleTest.php | 8 ++++++++ workbench/app/Models/User.php | 20 ++++++++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 src/Contracts/Foggable.php create mode 100644 workbench/app/Models/User.php diff --git a/src/Contracts/Foggable.php b/src/Contracts/Foggable.php new file mode 100644 index 0000000..e33d140 --- /dev/null +++ b/src/Contracts/Foggable.php @@ -0,0 +1,12 @@ +for($store) instead of foggle()->for($store->getId()) + */ +interface Foggable +{ + public function foggleId(): string; +} diff --git a/src/FogGen.php b/src/FogGen.php index 4dd66b9..9608eea 100644 --- a/src/FogGen.php +++ b/src/FogGen.php @@ -2,6 +2,9 @@ namespace YouCanShop\Foggle; +use InvalidArgumentException; +use YouCanShop\Foggle\Contracts\Foggable; + class FogGen { /** @@ -16,7 +19,13 @@ public static function inconfig(string $path): callable if (in_array('*', $config)) { return true; } - + + $context = $context instanceof Foggable ? $context->foggleId() : $context; + + if (!is_string($context)) { + throw new InvalidArgumentException('Context must be an instance of Foggable or a string'); + } + return in_array($context, config($path, [])); }; } diff --git a/tests/Feature/FoggleTest.php b/tests/Feature/FoggleTest.php index c88ebaf..10ab4d5 100644 --- a/tests/Feature/FoggleTest.php +++ b/tests/Feature/FoggleTest.php @@ -1,5 +1,6 @@ for('1')->active('billing'))->toBeTrue() ->and(foggle()->for('0')->active('billing'))->toBeFalse(); }); + +it('resolves a foggable using its id', function () { + foggle()->define('billing', FogGen::inconfig('features.billing.sellers')); + + expect(foggle()->for(new User('1'))->active('billing'))->toBeTrue() + ->and(foggle()->for(new User('0'))->active('billing'))->toBeFalse(); +}); diff --git a/workbench/app/Models/User.php b/workbench/app/Models/User.php new file mode 100644 index 0000000..3b330de --- /dev/null +++ b/workbench/app/Models/User.php @@ -0,0 +1,20 @@ +id = $id; + } + + public function foggleId(): string + { + return $this->id; + } +}