Skip to content

Commit 63c417e

Browse files
committed
feat(session): add UnitEnum support and use enum_value() helper
- Add UnitEnum support to session contract and store (BackedEnum uses ->value, UnitEnum uses ->name) - Replace Str::from()/fromAll() with enum_value() to match Laravel - Simplify enum_value() helper to match Laravel's direct implementation (remove unnecessary transform() wrapper) - Add comprehensive UnitEnum test coverage (24 new tests) - Update all session method signatures with strict UnitEnum types BREAKING CHANGE: Session contract method signatures now include UnitEnum type. Custom implementations must update their signatures.
1 parent 1a3ea18 commit 63c417e

File tree

4 files changed

+292
-57
lines changed

4 files changed

+292
-57
lines changed

src/session/src/Contracts/Session.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use BackedEnum;
88
use SessionHandlerInterface;
9+
use UnitEnum;
910

1011
interface Session
1112
{
@@ -47,27 +48,27 @@ public function all(): array;
4748
/**
4849
* Checks if a key exists.
4950
*/
50-
public function exists(array|BackedEnum|string $key): bool;
51+
public function exists(array|BackedEnum|UnitEnum|string $key): bool;
5152

5253
/**
5354
* Checks if a key is present and not null.
5455
*/
55-
public function has(array|BackedEnum|string $key): bool;
56+
public function has(array|BackedEnum|UnitEnum|string $key): bool;
5657

5758
/**
5859
* Get an item from the session.
5960
*/
60-
public function get(BackedEnum|string $key, mixed $default = null): mixed;
61+
public function get(BackedEnum|UnitEnum|string $key, mixed $default = null): mixed;
6162

6263
/**
6364
* Get the value of a given key and then forget it.
6465
*/
65-
public function pull(BackedEnum|string $key, mixed $default = null): mixed;
66+
public function pull(BackedEnum|UnitEnum|string $key, mixed $default = null): mixed;
6667

6768
/**
6869
* Put a key / value pair or array of key / value pairs in the session.
6970
*/
70-
public function put(array|BackedEnum|string $key, mixed $value = null): void;
71+
public function put(array|BackedEnum|UnitEnum|string $key, mixed $value = null): void;
7172

7273
/**
7374
* Get the CSRF token value.
@@ -82,12 +83,12 @@ public function regenerateToken(): void;
8283
/**
8384
* Remove an item from the session, returning its value.
8485
*/
85-
public function remove(BackedEnum|string $key): mixed;
86+
public function remove(BackedEnum|UnitEnum|string $key): mixed;
8687

8788
/**
8889
* Remove one or many items from the session.
8990
*/
90-
public function forget(array|BackedEnum|string $keys): void;
91+
public function forget(array|BackedEnum|UnitEnum|string $keys): void;
9192

9293
/**
9394
* Remove all of the items from the session.

src/session/src/Store.php

Lines changed: 34 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use BackedEnum;
88
use Closure;
99
use Hyperf\Collection\Arr;
10-
use Hyperf\Collection\Collection;
1110
use Hyperf\Context\Context;
1211
use Hyperf\Macroable\Macroable;
1312
use Hyperf\Support\MessageBag;
@@ -16,6 +15,9 @@
1615
use Hypervel\Support\Str;
1716
use SessionHandlerInterface;
1817
use stdClass;
18+
use UnitEnum;
19+
20+
use function Hypervel\Support\enum_value;
1921

2022
class Store implements Session
2123
{
@@ -204,82 +206,72 @@ public function all(): array
204206
*/
205207
public function only(array $keys): array
206208
{
207-
$keys = Str::fromAll($keys);
208-
$attributes = $this->getAttributes();
209-
210-
return Arr::only($attributes, $keys);
209+
return Arr::only($this->getAttributes(), array_map(enum_value(...), $keys));
211210
}
212211

213212
/**
214213
* Get all the session data except for a specified array of items.
215214
*/
216215
public function except(array $keys): array
217216
{
218-
$keys = Str::fromAll($keys);
219-
$attributes = $this->getAttributes();
220-
221-
return Arr::except($attributes, $keys);
217+
return Arr::except($this->getAttributes(), array_map(enum_value(...), $keys));
222218
}
223219

224220
/**
225221
* Checks if a key exists.
226222
*/
227-
public function exists(array|BackedEnum|string $key): bool
223+
public function exists(array|BackedEnum|UnitEnum|string $key): bool
228224
{
229225
$placeholder = new stdClass();
230226

231-
return ! (new Collection(is_array($key) ? $key : func_get_args()))->contains(function ($key) use ($placeholder) {
227+
return ! collect(is_array($key) ? $key : func_get_args())->contains(function ($key) use ($placeholder) {
232228
return $this->get($key, $placeholder) === $placeholder;
233229
});
234230
}
235231

236232
/**
237233
* Determine if the given key is missing from the session data.
238234
*/
239-
public function missing(array|BackedEnum|string $key): bool
235+
public function missing(array|BackedEnum|UnitEnum|string $key): bool
240236
{
241237
return ! $this->exists($key);
242238
}
243239

244240
/**
245241
* Determine if a key is present and not null.
246242
*/
247-
public function has(array|BackedEnum|string $key): bool
243+
public function has(array|BackedEnum|UnitEnum|string $key): bool
248244
{
249-
return ! (new Collection(is_array($key) ? $key : func_get_args()))->contains(function ($key) {
245+
return ! collect(is_array($key) ? $key : func_get_args())->contains(function ($key) {
250246
return is_null($this->get($key));
251247
});
252248
}
253249

254250
/**
255251
* Determine if any of the given keys are present and not null.
256252
*/
257-
public function hasAny(array|BackedEnum|string $key): bool
253+
public function hasAny(array|BackedEnum|UnitEnum|string $key): bool
258254
{
259-
return (new Collection(is_array($key) ? $key : func_get_args()))->filter(function ($key) {
255+
return collect(is_array($key) ? $key : func_get_args())->filter(function ($key) {
260256
return ! is_null($this->get($key));
261257
})->count() >= 1;
262258
}
263259

264260
/**
265261
* Get an item from the session.
266262
*/
267-
public function get(BackedEnum|string $key, mixed $default = null): mixed
263+
public function get(BackedEnum|UnitEnum|string $key, mixed $default = null): mixed
268264
{
269-
$key = Str::from($key);
270-
$attributes = $this->getAttributes();
271-
272-
return Arr::get($attributes, $key, $default);
265+
return Arr::get($this->getAttributes(), enum_value($key), $default);
273266
}
274267

275268
/**
276269
* Get the value of a given key and then forget it.
277270
*/
278-
public function pull(BackedEnum|string $key, mixed $default = null): mixed
271+
public function pull(BackedEnum|UnitEnum|string $key, mixed $default = null): mixed
279272
{
280-
$key = Str::from($key);
281273
$attributes = $this->getAttributes();
282-
$result = Arr::pull($attributes, $key, $default);
274+
$result = Arr::pull($attributes, enum_value($key), $default);
283275

284276
$this->setAttributes($attributes);
285277

@@ -289,7 +281,7 @@ public function pull(BackedEnum|string $key, mixed $default = null): mixed
289281
/**
290282
* Determine if the session contains old input.
291283
*/
292-
public function hasOldInput(BackedEnum|string|null $key = null): bool
284+
public function hasOldInput(BackedEnum|UnitEnum|string|null $key = null): bool
293285
{
294286
$old = $this->getOldInput($key);
295287

@@ -299,11 +291,9 @@ public function hasOldInput(BackedEnum|string|null $key = null): bool
299291
/**
300292
* Get the requested item from the flashed input array.
301293
*/
302-
public function getOldInput(BackedEnum|string|null $key = null, mixed $default = null): mixed
294+
public function getOldInput(BackedEnum|UnitEnum|string|null $key = null, mixed $default = null): mixed
303295
{
304-
$key = $key === null ? null : Str::from($key);
305-
306-
return Arr::get($this->get('_old_input', []), $key, $default);
296+
return Arr::get($this->get('_old_input', []), enum_value($key), $default);
307297
}
308298

309299
/**
@@ -317,15 +307,15 @@ public function replace(array $attributes): void
317307
/**
318308
* Put a key / value pair or array of key / value pairs in the session.
319309
*/
320-
public function put(array|BackedEnum|string $key, mixed $value = null): void
310+
public function put(array|BackedEnum|UnitEnum|string $key, mixed $value = null): void
321311
{
322312
if (! is_array($key)) {
323-
$key = [Str::from($key) => $value];
313+
$key = [enum_value($key) => $value];
324314
}
325315

326316
$attributes = $this->getAttributes();
327317
foreach ($key as $arrayKey => $arrayValue) {
328-
Arr::set($attributes, Str::from($arrayKey), $arrayValue);
318+
Arr::set($attributes, enum_value($arrayKey), $arrayValue);
329319
}
330320

331321
$this->setAttributes($attributes);
@@ -334,7 +324,7 @@ public function put(array|BackedEnum|string $key, mixed $value = null): void
334324
/**
335325
* Get an item from the session, or store the default value.
336326
*/
337-
public function remember(BackedEnum|string $key, Closure $callback): mixed
327+
public function remember(BackedEnum|UnitEnum|string $key, Closure $callback): mixed
338328
{
339329
if (! is_null($value = $this->get($key))) {
340330
return $value;
@@ -348,7 +338,7 @@ public function remember(BackedEnum|string $key, Closure $callback): mixed
348338
/**
349339
* Push a value onto a session array.
350340
*/
351-
public function push(BackedEnum|string $key, mixed $value): void
341+
public function push(BackedEnum|UnitEnum|string $key, mixed $value): void
352342
{
353343
$array = $this->get($key, []);
354344

@@ -360,7 +350,7 @@ public function push(BackedEnum|string $key, mixed $value): void
360350
/**
361351
* Increment the value of an item in the session.
362352
*/
363-
public function increment(BackedEnum|string $key, int $amount = 1): mixed
353+
public function increment(BackedEnum|UnitEnum|string $key, int $amount = 1): mixed
364354
{
365355
$this->put($key, $value = $this->get($key, 0) + $amount);
366356

@@ -370,17 +360,17 @@ public function increment(BackedEnum|string $key, int $amount = 1): mixed
370360
/**
371361
* Decrement the value of an item in the session.
372362
*/
373-
public function decrement(BackedEnum|string $key, int $amount = 1): int
363+
public function decrement(BackedEnum|UnitEnum|string $key, int $amount = 1): int
374364
{
375365
return $this->increment($key, $amount * -1);
376366
}
377367

378368
/**
379369
* Flash a key / value pair to the session.
380370
*/
381-
public function flash(BackedEnum|string $key, mixed $value = true): void
371+
public function flash(BackedEnum|UnitEnum|string $key, mixed $value = true): void
382372
{
383-
$key = Str::from($key);
373+
$key = enum_value($key);
384374

385375
$this->put($key, $value);
386376

@@ -392,9 +382,9 @@ public function flash(BackedEnum|string $key, mixed $value = true): void
392382
/**
393383
* Flash a key / value pair to the session for immediate use.
394384
*/
395-
public function now(BackedEnum|string $key, mixed $value): void
385+
public function now(BackedEnum|UnitEnum|string $key, mixed $value): void
396386
{
397-
$key = Str::from($key);
387+
$key = enum_value($key);
398388

399389
$this->put($key, $value);
400390

@@ -452,11 +442,10 @@ public function flashInput(array $value): void
452442
/**
453443
* Remove an item from the session, returning its value.
454444
*/
455-
public function remove(BackedEnum|string $key): mixed
445+
public function remove(BackedEnum|UnitEnum|string $key): mixed
456446
{
457-
$key = Str::from($key);
458447
$attributes = $this->getAttributes();
459-
$result = Arr::pull($attributes, $key);
448+
$result = Arr::pull($attributes, enum_value($key));
460449

461450
$this->setAttributes($attributes);
462451

@@ -466,11 +455,10 @@ public function remove(BackedEnum|string $key): mixed
466455
/**
467456
* Remove one or many items from the session.
468457
*/
469-
public function forget(array|BackedEnum|string $keys): void
458+
public function forget(array|BackedEnum|UnitEnum|string $keys): void
470459
{
471-
$keys = is_array($keys) ? Str::fromAll($keys) : Str::from($keys);
472460
$attributes = $this->getAttributes();
473-
Arr::forget($attributes, $keys);
461+
Arr::forget($attributes, collect((array) $keys)->map(fn ($key) => enum_value($key))->all());
474462

475463
$this->setAttributes($attributes);
476464
}

src/support/src/Functions.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,11 @@ function value(mixed $value, ...$args)
3636
*/
3737
function enum_value($value, $default = null)
3838
{
39-
return transform($value, fn ($value) => match (true) {
39+
return match (true) {
4040
$value instanceof BackedEnum => $value->value,
4141
$value instanceof UnitEnum => $value->name,
42-
43-
default => $value,
44-
}, $default ?? $value);
42+
default => $value ?? value($default),
43+
};
4544
}
4645

4746
/**

0 commit comments

Comments
 (0)