Upgrade to 4.0.0

This commit is contained in:
Bastian Allgeier
2023-11-28 09:33:56 +01:00
parent f96b96af76
commit 3b0b6546ca
480 changed files with 21371 additions and 13327 deletions

View File

@@ -2,8 +2,8 @@
namespace Kirby\Session;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Exception\LogicException;
use Kirby\Toolkit\A;
/**
* The session object can be used to
@@ -18,9 +18,6 @@ use Kirby\Exception\LogicException;
*/
class SessionData
{
protected $session;
protected $data;
/**
* Creates a new SessionData instance
*
@@ -28,10 +25,10 @@ class SessionData
* @param \Kirby\Session\Session $session Session object this data belongs to
* @param array $data Currently stored session data
*/
public function __construct(Session $session, array $data)
{
$this->session = $session;
$this->data = $data;
public function __construct(
protected Session $session,
protected array $data
) {
}
/**
@@ -39,22 +36,18 @@ class SessionData
*
* @param string|array $key The key to define or a key-value array with multiple values
* @param mixed $value The value for the passed key (only if one $key is passed)
* @return void
*/
public function set($key, $value = null)
{
public function set(
string|array $key,
mixed $value = null
): void {
$this->session->ensureToken();
$this->session->prepareForWriting();
if (is_string($key)) {
if (is_string($key) === true) {
$this->data[$key] = $value;
} elseif (is_array($key)) {
$this->data = array_merge($this->data, $key);
} else {
throw new InvalidArgumentException([
'data' => ['method' => 'SessionData::set', 'argument' => 'key'],
'translate' => false
]);
$this->data = array_replace($this->data, $key);
}
}
@@ -63,53 +56,45 @@ class SessionData
*
* @param string|array $key The key to increment or an array with multiple keys
* @param int $by Increment by which amount?
* @param int $max Maximum amount (value is not incremented further)
* @return void
* @param int|null $max Maximum amount (value is not incremented further)
*/
public function increment($key, int $by = 1, $max = null)
{
if ($max !== null && !is_int($max)) {
throw new InvalidArgumentException([
'data' => ['method' => 'SessionData::increment', 'argument' => 'max'],
'translate' => false
]);
}
if (is_string($key)) {
// make sure we have the correct values before getting
$this->session->prepareForWriting();
$value = $this->get($key, 0);
if (!is_int($value)) {
throw new LogicException([
'key' => 'session.data.increment.nonInt',
'data' => ['key' => $key],
'fallback' => 'Session value "' . $key . '" is not an integer and cannot be incremented',
'translate' => false
]);
}
// increment the value, but ensure $max constraint
if (is_int($max) && $value + $by > $max) {
// set the value to $max
// but not if the current $value is already larger than $max
$value = max($value, $max);
} else {
$value += $by;
}
$this->set($key, $value);
} elseif (is_array($key)) {
public function increment(
string|array $key,
int $by = 1,
int|null $max = null
): void {
// if array passed, call method recursively
if (is_array($key) === true) {
foreach ($key as $k) {
$this->increment($k, $by, $max);
}
} else {
throw new InvalidArgumentException([
'data' => ['method' => 'SessionData::increment', 'argument' => 'key'],
return;
}
// make sure we have the correct values before getting
$this->session->prepareForWriting();
$value = $this->get($key, 0);
if (is_int($value) === false) {
throw new LogicException([
'key' => 'session.data.increment.nonInt',
'data' => ['key' => $key],
'fallback' => 'Session value "' . $key . '" is not an integer and cannot be incremented',
'translate' => false
]);
}
// increment the value, but ensure $max constraint
if (is_int($max) === true && $value + $by > $max) {
// set the value to $max
// but not if the current $value is already larger than $max
$value = max($value, $max);
} else {
$value += $by;
}
$this->set($key, $value);
}
/**
@@ -117,53 +102,45 @@ class SessionData
*
* @param string|array $key The key to decrement or an array with multiple keys
* @param int $by Decrement by which amount?
* @param int $min Minimum amount (value is not decremented further)
* @return void
* @param int|null $min Minimum amount (value is not decremented further)
*/
public function decrement($key, int $by = 1, $min = null)
{
if ($min !== null && !is_int($min)) {
throw new InvalidArgumentException([
'data' => ['method' => 'SessionData::decrement', 'argument' => 'min'],
'translate' => false
]);
}
if (is_string($key)) {
// make sure we have the correct values before getting
$this->session->prepareForWriting();
$value = $this->get($key, 0);
if (!is_int($value)) {
throw new LogicException([
'key' => 'session.data.decrement.nonInt',
'data' => ['key' => $key],
'fallback' => 'Session value "' . $key . '" is not an integer and cannot be decremented',
'translate' => false
]);
}
// decrement the value, but ensure $min constraint
if (is_int($min) && $value - $by < $min) {
// set the value to $min
// but not if the current $value is already smaller than $min
$value = min($value, $min);
} else {
$value -= $by;
}
$this->set($key, $value);
} elseif (is_array($key)) {
public function decrement(
string|array $key,
int $by = 1,
int|null $min = null
): void {
// if array passed, call method recursively
if (is_array($key) === true) {
foreach ($key as $k) {
$this->decrement($k, $by, $min);
}
} else {
throw new InvalidArgumentException([
'data' => ['method' => 'SessionData::decrement', 'argument' => 'key'],
return;
}
// make sure we have the correct values before getting
$this->session->prepareForWriting();
$value = $this->get($key, 0);
if (is_int($value) === false) {
throw new LogicException([
'key' => 'session.data.decrement.nonInt',
'data' => ['key' => $key],
'fallback' => 'Session value "' . $key . '" is not an integer and cannot be decremented',
'translate' => false
]);
}
// decrement the value, but ensure $min constraint
if (is_int($min) === true && $value - $by < $min) {
// set the value to $min
// but not if the current $value is already smaller than $min
$value = min($value, $min);
} else {
$value -= $by;
}
$this->set($key, $value);
}
/**
@@ -171,25 +148,16 @@ class SessionData
*
* @param string|null $key The key to get or null for the entire data array
* @param mixed $default Optional default value to return if the key is not defined
* @return mixed
*/
public function get($key = null, $default = null)
{
if (is_string($key)) {
return $this->data[$key] ?? $default;
}
public function get(
string|null $key = null,
mixed $default = null
): mixed {
if ($key === null) {
return $this->data;
}
throw new InvalidArgumentException([
'data' => [
'method' => 'SessionData::get',
'argument' => 'key'
],
'translate' => false
]);
return $this->data[$key] ?? $default;
}
/**
@@ -197,9 +165,8 @@ class SessionData
*
* @param string $key The key to get
* @param mixed $default Optional default value to return if the key is not defined
* @return mixed
*/
public function pull(string $key, $default = null)
public function pull(string $key, mixed $default = null): mixed
{
// make sure we have the correct value before getting
// we do this here (but not in get) as we need to write anyway
@@ -214,35 +181,22 @@ class SessionData
* Removes one or multiple session values by key
*
* @param string|array $key The key to remove or an array with multiple keys
* @return void
*/
public function remove($key)
public function remove(string|array $key): void
{
$this->session->prepareForWriting();
if (is_string($key)) {
unset($this->data[$key]);
} elseif (is_array($key)) {
foreach ($key as $k) {
unset($this->data[$k]);
}
} else {
throw new InvalidArgumentException([
'data' => ['method' => 'SessionData::remove', 'argument' => 'key'],
'translate' => false
]);
foreach (A::wrap($key) as $k) {
unset($this->data[$k]);
}
}
/**
* Clears all session data
*
* @return void
*/
public function clear()
public function clear(): void
{
$this->session->prepareForWriting();
$this->data = [];
}
@@ -251,9 +205,8 @@ class SessionData
* Only used internally
*
* @param array $data Currently stored session data
* @return void
*/
public function reload(array $data)
public function reload(array $data): void
{
$this->data = $data;
}