Merge pull request #1151 from samsonasik/implements-session-push

implements session->push()
This commit is contained in:
Lonnie Ezell 2018-08-08 21:31:33 -05:00 committed by GitHub
commit 7c1b2cc475
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 0 deletions

View File

@ -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));
}
}
//--------------------------------------------------------------------
/**

View File

@ -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();