Skip to content

Commit 795cad7

Browse files
committed
Add enum support to Cookie, Js, ThrottleRequests, and PendingBatch
- Cookie: has(), get(), make(), expire(), unqueue(), forever(), forget() - Js: Changed BackedEnum to UnitEnum, use enum_value() - ThrottleRequests::using(): Accept enum limiter names - PendingBatch/PendingChain::onConnection(): Accept enum connection names - Updated facade docblocks via facade-documenter - Added comprehensive enum tests
1 parent 9c38d3c commit 795cad7

File tree

18 files changed

+435
-71
lines changed

18 files changed

+435
-71
lines changed

src/bus/src/PendingBatch.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,9 @@ public function name(string $name): static
194194
/**
195195
* Specify the queue connection that the batched jobs should run on.
196196
*/
197-
public function onConnection(string $connection): static
197+
public function onConnection(UnitEnum|string $connection): static
198198
{
199-
$this->options['connection'] = $connection;
199+
$this->options['connection'] = enum_value($connection);
200200

201201
return $this;
202202
}

src/bus/src/PendingChain.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ public function __construct(
5656
/**
5757
* Set the desired connection for the job.
5858
*/
59-
public function onConnection(?string $connection): static
59+
public function onConnection(UnitEnum|string|null $connection): static
6060
{
61-
$this->connection = $connection;
61+
$this->connection = enum_value($connection);
6262

6363
return $this;
6464
}

src/cookie/src/Contracts/Cookie.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,25 @@
55
namespace Hypervel\Cookie\Contracts;
66

77
use Hyperf\HttpMessage\Cookie\Cookie as HyperfCookie;
8+
use UnitEnum;
89

910
interface Cookie
1011
{
11-
public function has(string $key): bool;
12+
public function has(UnitEnum|string $key): bool;
1213

13-
public function get(string $key, ?string $default = null): ?string;
14+
public function get(UnitEnum|string $key, ?string $default = null): ?string;
1415

15-
public function make(string $name, string $value, int $minutes = 0, string $path = '', string $domain = '', bool $secure = false, bool $httpOnly = true, bool $raw = false, ?string $sameSite = null): HyperfCookie;
16+
public function make(UnitEnum|string $name, string $value, int $minutes = 0, string $path = '', string $domain = '', bool $secure = false, bool $httpOnly = true, bool $raw = false, ?string $sameSite = null): HyperfCookie;
1617

1718
public function queue(...$parameters): void;
1819

19-
public function expire(string $name, string $path = '', string $domain = ''): void;
20+
public function expire(UnitEnum|string $name, string $path = '', string $domain = ''): void;
2021

21-
public function unqueue(string $name, string $path = ''): void;
22+
public function unqueue(UnitEnum|string $name, string $path = ''): void;
2223

2324
public function getQueuedCookies(): array;
2425

25-
public function forever(string $name, string $value, string $path = '', string $domain = '', bool $secure = false, bool $httpOnly = true, bool $raw = false, ?string $sameSite = null): HyperfCookie;
26+
public function forever(UnitEnum|string $name, string $value, string $path = '', string $domain = '', bool $secure = false, bool $httpOnly = true, bool $raw = false, ?string $sameSite = null): HyperfCookie;
2627

27-
public function forget(string $name, string $path = '', string $domain = ''): HyperfCookie;
28+
public function forget(UnitEnum|string $name, string $path = '', string $domain = ''): HyperfCookie;
2829
}

src/cookie/src/CookieManager.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
use Hyperf\HttpServer\Contract\RequestInterface;
1010
use Hyperf\Support\Traits\InteractsWithTime;
1111
use Hypervel\Cookie\Contracts\Cookie as CookieContract;
12+
use UnitEnum;
13+
14+
use function Hypervel\Support\enum_value;
1215

1316
class CookieManager implements CookieContract
1417
{
@@ -19,25 +22,25 @@ public function __construct(
1922
) {
2023
}
2124

22-
public function has(string $key): bool
25+
public function has(UnitEnum|string $key): bool
2326
{
2427
return ! is_null($this->get($key));
2528
}
2629

27-
public function get(string $key, ?string $default = null): ?string
30+
public function get(UnitEnum|string $key, ?string $default = null): ?string
2831
{
2932
if (! RequestContext::has()) {
3033
return null;
3134
}
3235

33-
return $this->request->cookie($key, $default);
36+
return $this->request->cookie(enum_value($key), $default);
3437
}
3538

36-
public function make(string $name, string $value, int $minutes = 0, string $path = '', string $domain = '', bool $secure = false, bool $httpOnly = true, bool $raw = false, ?string $sameSite = null): Cookie
39+
public function make(UnitEnum|string $name, string $value, int $minutes = 0, string $path = '', string $domain = '', bool $secure = false, bool $httpOnly = true, bool $raw = false, ?string $sameSite = null): Cookie
3740
{
3841
$time = ($minutes == 0) ? 0 : $this->availableAt($minutes * 60);
3942

40-
return new Cookie($name, $value, $time, $path, $domain, $secure, $httpOnly, $raw, $sameSite);
43+
return new Cookie(enum_value($name), $value, $time, $path, $domain, $secure, $httpOnly, $raw, $sameSite);
4144
}
4245

4346
public function queue(...$parameters): void
@@ -51,13 +54,15 @@ public function queue(...$parameters): void
5154
$this->appendToQueue($cookie);
5255
}
5356

54-
public function expire(string $name, string $path = '', string $domain = ''): void
57+
public function expire(UnitEnum|string $name, string $path = '', string $domain = ''): void
5558
{
5659
$this->queue($this->forget($name, $path, $domain));
5760
}
5861

59-
public function unqueue(string $name, string $path = ''): void
62+
public function unqueue(UnitEnum|string $name, string $path = ''): void
6063
{
64+
$name = enum_value($name);
65+
6166
$cookies = $this->getQueuedCookies();
6267
if ($path === '') {
6368
unset($cookies[$name]);
@@ -93,12 +98,12 @@ protected function setQueueCookies(array $cookies): array
9398
return Context::set('http.cookies.queue', $cookies);
9499
}
95100

96-
public function forever(string $name, string $value, string $path = '', string $domain = '', bool $secure = false, bool $httpOnly = true, bool $raw = false, ?string $sameSite = null): Cookie
101+
public function forever(UnitEnum|string $name, string $value, string $path = '', string $domain = '', bool $secure = false, bool $httpOnly = true, bool $raw = false, ?string $sameSite = null): Cookie
97102
{
98103
return $this->make($name, $value, 2628000, $path, $domain, $secure, $httpOnly, $raw, $sameSite);
99104
}
100105

101-
public function forget(string $name, string $path = '', string $domain = ''): Cookie
106+
public function forget(UnitEnum|string $name, string $path = '', string $domain = ''): Cookie
102107
{
103108
return $this->make($name, '', -2628000, $path, $domain);
104109
}

src/router/src/Middleware/ThrottleRequests.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
use Psr\Http\Server\MiddlewareInterface;
2020
use Psr\Http\Server\RequestHandlerInterface;
2121
use RuntimeException;
22+
use UnitEnum;
23+
24+
use function Hypervel\Support\enum_value;
2225

2326
class ThrottleRequests implements MiddlewareInterface
2427
{
@@ -45,9 +48,9 @@ public function __construct(RateLimiter $limiter)
4548
/**
4649
* Specify the named rate limiter to use for the middleware.
4750
*/
48-
public static function using(string $name): string
51+
public static function using(UnitEnum|string $name): string
4952
{
50-
return static::class . ':' . $name;
53+
return static::class . ':' . enum_value($name);
5154
}
5255

5356
/**

src/support/src/Facades/Cache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
* @method static bool putMany(array $values, int $seconds)
4444
* @method static bool flush()
4545
* @method static string getPrefix()
46-
* @method static bool missing(string $key)
46+
* @method static bool missing(\UnitEnum|string $key)
4747
* @method static bool supportsTags()
4848
* @method static int|null getDefaultCacheTime()
4949
* @method static \Hypervel\Cache\Repository setDefaultCacheTime(int|null $seconds)

src/support/src/Facades/Cookie.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
use Hypervel\Cookie\Contracts\Cookie as CookieContract;
88

99
/**
10-
* @method static bool has(string $key)
11-
* @method static string|null get(string $key, string|null $default = null)
12-
* @method static \Hypervel\Cookie\Cookie make(string $name, string $value, int $minutes = 0, string $path = '', string $domain = '', bool $secure = false, bool $httpOnly = true, bool $raw = false, string|null $sameSite = null)
10+
* @method static bool has(\UnitEnum|string $key)
11+
* @method static string|null get(\UnitEnum|string $key, string|null $default = null)
12+
* @method static \Hypervel\Cookie\Cookie make(\UnitEnum|string $name, string $value, int $minutes = 0, string $path = '', string $domain = '', bool $secure = false, bool $httpOnly = true, bool $raw = false, string|null $sameSite = null)
1313
* @method static void queue(mixed ...$parameters)
14-
* @method static void expire(string $name, string $path = '', string $domain = '')
15-
* @method static void unqueue(string $name, string $path = '')
14+
* @method static void expire(\UnitEnum|string $name, string $path = '', string $domain = '')
15+
* @method static void unqueue(\UnitEnum|string $name, string $path = '')
1616
* @method static array getQueuedCookies()
17-
* @method static \Hypervel\Cookie\Cookie forever(string $name, string $value, string $path = '', string $domain = '', bool $secure = false, bool $httpOnly = true, bool $raw = false, string|null $sameSite = null)
18-
* @method static \Hypervel\Cookie\Cookie forget(string $name, string $path = '', string $domain = '')
17+
* @method static \Hypervel\Cookie\Cookie forever(\UnitEnum|string $name, string $value, string $path = '', string $domain = '', bool $secure = false, bool $httpOnly = true, bool $raw = false, string|null $sameSite = null)
18+
* @method static \Hypervel\Cookie\Cookie forget(\UnitEnum|string $name, string $path = '', string $domain = '')
1919
*
2020
* @see \Hypervel\Cookie\CookieManager
2121
*/

src/support/src/Facades/Gate.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@
77
use Hypervel\Auth\Contracts\Gate as GateContract;
88

99
/**
10-
* @method static bool has(array|string $ability)
10+
* @method static bool has(\UnitEnum|array|string $ability)
1111
* @method static \Hypervel\Auth\Access\Response allowIf(\Closure|\Hypervel\Auth\Access\Response|bool $condition, string|null $message = null, string|null $code = null)
1212
* @method static \Hypervel\Auth\Access\Response denyIf(\Closure|\Hypervel\Auth\Access\Response|bool $condition, string|null $message = null, string|null $code = null)
13-
* @method static \Hypervel\Auth\Access\Gate define(string $ability, callable|array|string $callback)
13+
* @method static \Hypervel\Auth\Access\Gate define(\UnitEnum|string $ability, callable|array|string $callback)
1414
* @method static \Hypervel\Auth\Access\Gate resource(string $name, string $class, array|null $abilities = null)
1515
* @method static \Hypervel\Auth\Access\Gate policy(string $class, string $policy)
1616
* @method static \Hypervel\Auth\Access\Gate before(callable $callback)
1717
* @method static \Hypervel\Auth\Access\Gate after(callable $callback)
18-
* @method static bool allows(string $ability, mixed $arguments = [])
19-
* @method static bool denies(string $ability, mixed $arguments = [])
20-
* @method static bool check(\Traversable|array|string $abilities, mixed $arguments = [])
21-
* @method static bool any(\Traversable|array|string $abilities, mixed $arguments = [])
22-
* @method static bool none(\Traversable|array|string $abilities, mixed $arguments = [])
23-
* @method static \Hypervel\Auth\Access\Response authorize(string $ability, mixed $arguments = [])
24-
* @method static \Hypervel\Auth\Access\Response inspect(string $ability, mixed $arguments = [])
18+
* @method static bool allows(\UnitEnum|string $ability, mixed $arguments = [])
19+
* @method static bool denies(\UnitEnum|string $ability, mixed $arguments = [])
20+
* @method static bool check(\Traversable|\UnitEnum|array|string $abilities, mixed $arguments = [])
21+
* @method static bool any(\Traversable|\UnitEnum|array|string $abilities, mixed $arguments = [])
22+
* @method static bool none(\Traversable|\UnitEnum|array|string $abilities, mixed $arguments = [])
23+
* @method static \Hypervel\Auth\Access\Response authorize(\UnitEnum|string $ability, mixed $arguments = [])
24+
* @method static \Hypervel\Auth\Access\Response inspect(\UnitEnum|string $ability, mixed $arguments = [])
2525
* @method static mixed raw(string $ability, mixed $arguments = [])
2626
* @method static mixed|void getPolicyFor(object|string $class)
2727
* @method static mixed resolvePolicy(string $class)

src/support/src/Facades/RateLimiter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
namespace Hypervel\Support\Facades;
66

77
/**
8-
* @method static \Hypervel\Cache\RateLimiter for(string $name, \Closure $callback)
9-
* @method static \Closure|null limiter(string $name)
8+
* @method static \Hypervel\Cache\RateLimiter for(\UnitEnum|string $name, \Closure $callback)
9+
* @method static \Closure|null limiter(\UnitEnum|string $name)
1010
* @method static mixed attempt(string $key, int $maxAttempts, \Closure $callback, int $decaySeconds = 60)
1111
* @method static bool tooManyAttempts(string $key, int $maxAttempts)
1212
* @method static int hit(string $key, int $decaySeconds = 60)

src/support/src/Facades/Redis.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Hypervel\Redis\Redis as RedisClient;
88

99
/**
10-
* @method static \Hypervel\Redis\RedisProxy connection(string $name = 'default')
10+
* @method static \Hypervel\Redis\RedisProxy connection(\UnitEnum|string $name = 'default')
1111
* @method static void release()
1212
* @method static \Hypervel\Redis\RedisConnection shouldTransform(bool $shouldTransform = true)
1313
* @method static bool getShouldTransform()

0 commit comments

Comments
 (0)