mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
Merge pull request #1151 from samsonasik/implements-session-push
implements session->push()
This commit is contained in:
commit
7c1b2cc475
@ -516,6 +516,24 @@ class Session implements SessionInterface
|
||||
return isset($_SESSION[$key]);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Push new value onto session value that is array.
|
||||
*
|
||||
* @param string $key Identifier of the session property we are interested in.
|
||||
* @param array $data value to be pushed to existing session key.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function push(string $key, array $data)
|
||||
{
|
||||
if ($this->has($key) && is_array($value = $this->get($key)))
|
||||
{
|
||||
$this->set($key, array_merge($value, $data));
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
|
@ -170,6 +170,23 @@ class SessionTest extends \CIUnitTestCase
|
||||
$this->assertFalse($session->has('bar'));
|
||||
}
|
||||
|
||||
public function testPushNewValueIntoArraySessionValue()
|
||||
{
|
||||
$session = $this->getInstance();
|
||||
$session->start();
|
||||
|
||||
$session->set('hobbies', ['cooking' => 'baking']);
|
||||
$session->push('hobbies', ['sport'=>'tennis']);
|
||||
|
||||
$this->assertEquals(
|
||||
[
|
||||
'cooking' => 'baking',
|
||||
'sport' => 'tennis',
|
||||
],
|
||||
$session->get('hobbies')
|
||||
);
|
||||
}
|
||||
|
||||
public function testRemoveActuallyRemoves()
|
||||
{
|
||||
$session = $this->getInstance();
|
||||
|
Loading…
x
Reference in New Issue
Block a user