Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/session/src/Contracts/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Hypervel\Session\Contracts;

use BackedEnum;
use SessionHandlerInterface;

interface Session
Expand Down Expand Up @@ -46,27 +47,27 @@ public function all(): array;
/**
* Checks if a key exists.
*/
public function exists(array|string $key): bool;
public function exists(array|BackedEnum|string $key): bool;

/**
* Checks if a key is present and not null.
*/
public function has(array|string $key): bool;
public function has(array|BackedEnum|string $key): bool;

/**
* Get an item from the session.
*/
public function get(string $key, mixed $default = null): mixed;
public function get(BackedEnum|string $key, mixed $default = null): mixed;

/**
* Get the value of a given key and then forget it.
*/
public function pull(string $key, mixed $default = null): mixed;
public function pull(BackedEnum|string $key, mixed $default = null): mixed;

/**
* Put a key / value pair or array of key / value pairs in the session.
*/
public function put(array|string $key, mixed $value = null): void;
public function put(array|BackedEnum|string $key, mixed $value = null): void;

/**
* Get the CSRF token value.
Expand All @@ -81,12 +82,12 @@ public function regenerateToken(): void;
/**
* Remove an item from the session, returning its value.
*/
public function remove(string $key): mixed;
public function remove(BackedEnum|string $key): mixed;

/**
* Remove one or many items from the session.
*/
public function forget(array|string $keys): void;
public function forget(array|BackedEnum|string $keys): void;

/**
* Remove all of the items from the session.
Expand Down
53 changes: 33 additions & 20 deletions src/session/src/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@

namespace Hypervel\Session;

use BackedEnum;
use Closure;
use Hyperf\Collection\Arr;
use Hyperf\Collection\Collection;
use Hyperf\Context\Context;
use Hyperf\Macroable\Macroable;
use Hyperf\Stringable\Str;
use Hyperf\Support\MessageBag;
use Hyperf\ViewEngine\ViewErrorBag;
use Hypervel\Session\Contracts\Session;
use Hypervel\Support\Str;
use SessionHandlerInterface;
use stdClass;

Expand Down Expand Up @@ -203,6 +204,7 @@ public function all(): array
*/
public function only(array $keys): array
{
$keys = Str::fromAll($keys);
$attributes = $this->getAttributes();

return Arr::only($attributes, $keys);
Expand All @@ -213,6 +215,7 @@ public function only(array $keys): array
*/
public function except(array $keys): array
{
$keys = Str::fromAll($keys);
$attributes = $this->getAttributes();

return Arr::except($attributes, $keys);
Expand All @@ -221,7 +224,7 @@ public function except(array $keys): array
/**
* Checks if a key exists.
*/
public function exists(array|string $key): bool
public function exists(array|BackedEnum|string $key): bool
{
$placeholder = new stdClass();

Expand All @@ -233,15 +236,15 @@ public function exists(array|string $key): bool
/**
* Determine if the given key is missing from the session data.
*/
public function missing(array|string $key): bool
public function missing(array|BackedEnum|string $key): bool
{
return ! $this->exists($key);
}

/**
* Determine if a key is present and not null.
*/
public function has(array|string $key): bool
public function has(array|BackedEnum|string $key): bool
{
return ! (new Collection(is_array($key) ? $key : func_get_args()))->contains(function ($key) {
return is_null($this->get($key));
Expand All @@ -251,7 +254,7 @@ public function has(array|string $key): bool
/**
* Determine if any of the given keys are present and not null.
*/
public function hasAny(array|string $key): bool
public function hasAny(array|BackedEnum|string $key): bool
{
return (new Collection(is_array($key) ? $key : func_get_args()))->filter(function ($key) {
return ! is_null($this->get($key));
Expand All @@ -261,8 +264,9 @@ public function hasAny(array|string $key): bool
/**
* Get an item from the session.
*/
public function get(string $key, mixed $default = null): mixed
public function get(BackedEnum|string $key, mixed $default = null): mixed
{
$key = Str::from($key);
$attributes = $this->getAttributes();

return Arr::get($attributes, $key, $default);
Expand All @@ -271,8 +275,9 @@ public function get(string $key, mixed $default = null): mixed
/**
* Get the value of a given key and then forget it.
*/
public function pull(string $key, mixed $default = null): mixed
public function pull(BackedEnum|string $key, mixed $default = null): mixed
{
$key = Str::from($key);
$attributes = $this->getAttributes();
$result = Arr::pull($attributes, $key, $default);

Expand All @@ -284,7 +289,7 @@ public function pull(string $key, mixed $default = null): mixed
/**
* Determine if the session contains old input.
*/
public function hasOldInput(?string $key = null): bool
public function hasOldInput(BackedEnum|string|null $key = null): bool
{
$old = $this->getOldInput($key);

Expand All @@ -294,8 +299,10 @@ public function hasOldInput(?string $key = null): bool
/**
* Get the requested item from the flashed input array.
*/
public function getOldInput(?string $key = null, mixed $default = null): mixed
public function getOldInput(BackedEnum|string|null $key = null, mixed $default = null): mixed
{
$key = $key === null ? null : Str::from($key);

return Arr::get($this->get('_old_input', []), $key, $default);
}

Expand All @@ -310,15 +317,15 @@ public function replace(array $attributes): void
/**
* Put a key / value pair or array of key / value pairs in the session.
*/
public function put(array|string $key, mixed $value = null): void
public function put(array|BackedEnum|string $key, mixed $value = null): void
{
if (! is_array($key)) {
$key = [$key => $value];
$key = [Str::from($key) => $value];
}

$attributes = $this->getAttributes();
foreach ($key as $arrayKey => $arrayValue) {
Arr::set($attributes, $arrayKey, $arrayValue);
Arr::set($attributes, Str::from($arrayKey), $arrayValue);
}

$this->setAttributes($attributes);
Expand All @@ -327,7 +334,7 @@ public function put(array|string $key, mixed $value = null): void
/**
* Get an item from the session, or store the default value.
*/
public function remember(string $key, Closure $callback): mixed
public function remember(BackedEnum|string $key, Closure $callback): mixed
{
if (! is_null($value = $this->get($key))) {
return $value;
Expand All @@ -341,7 +348,7 @@ public function remember(string $key, Closure $callback): mixed
/**
* Push a value onto a session array.
*/
public function push(string $key, mixed $value): void
public function push(BackedEnum|string $key, mixed $value): void
{
$array = $this->get($key, []);

Expand All @@ -353,7 +360,7 @@ public function push(string $key, mixed $value): void
/**
* Increment the value of an item in the session.
*/
public function increment(string $key, int $amount = 1): mixed
public function increment(BackedEnum|string $key, int $amount = 1): mixed
{
$this->put($key, $value = $this->get($key, 0) + $amount);

Expand All @@ -363,16 +370,18 @@ public function increment(string $key, int $amount = 1): mixed
/**
* Decrement the value of an item in the session.
*/
public function decrement(string $key, int $amount = 1): int
public function decrement(BackedEnum|string $key, int $amount = 1): int
{
return $this->increment($key, $amount * -1);
}

/**
* Flash a key / value pair to the session.
*/
public function flash(string $key, mixed $value = true): void
public function flash(BackedEnum|string $key, mixed $value = true): void
{
$key = Str::from($key);

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

$this->push('_flash.new', $key);
Expand All @@ -383,8 +392,10 @@ public function flash(string $key, mixed $value = true): void
/**
* Flash a key / value pair to the session for immediate use.
*/
public function now(string $key, mixed $value): void
public function now(BackedEnum|string $key, mixed $value): void
{
$key = Str::from($key);

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

$this->push('_flash.old', $key);
Expand Down Expand Up @@ -441,8 +452,9 @@ public function flashInput(array $value): void
/**
* Remove an item from the session, returning its value.
*/
public function remove(string $key): mixed
public function remove(BackedEnum|string $key): mixed
{
$key = Str::from($key);
$attributes = $this->getAttributes();
$result = Arr::pull($attributes, $key);

Expand All @@ -454,8 +466,9 @@ public function remove(string $key): mixed
/**
* Remove one or many items from the session.
*/
public function forget(array|string $keys): void
public function forget(array|BackedEnum|string $keys): void
{
$keys = is_array($keys) ? Str::fromAll($keys) : Str::from($keys);
$attributes = $this->getAttributes();
Arr::forget($attributes, $keys);

Expand Down
Loading