Skip to content

Commit

Permalink
Add add methods to append numeric key values
Browse files Browse the repository at this point in the history
  • Loading branch information
jakejohns committed Apr 11, 2016
1 parent fa67dd9 commit 5c7ca60
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
41 changes: 41 additions & 0 deletions src/Segment.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,19 @@ public function set($key, $val)
$_SESSION[$this->name][$key] = $val;
}

/**
*
* Append a value to a numeric key in the segment.
*
* @param mixed $val The value to append.
*
*/
public function add($val)
{
$this->resumeOrStartSession();
$_SESSION[$this->name][] = $val;
}

/**
*
* Clear all data from the segment.
Expand Down Expand Up @@ -146,6 +159,20 @@ public function setFlash($key, $val)
$_SESSION[Session::FLASH_NEXT][$this->name][$key] = $val;
}


/**
*
* Append a flash value with a numeric key for the *next* request.
*
* @param mixed $val The flash value itself.
*
*/
public function addFlash($val)
{
$this->resumeOrStartSession();
$_SESSION[Session::FLASH_NEXT][$this->name][] = $val;
}

/**
*
* Gets the flash value for a key in the *current* request.
Expand Down Expand Up @@ -248,6 +275,20 @@ public function setFlashNow($key, $val)
$_SESSION[Session::FLASH_NEXT][$this->name][$key] = $val;
}

/**
*
* Append a flash value with a numeric key for the *next* request *and* the current one.
*
* @param mixed $val The flash value itself.
*
*/
public function addFlashNow($val)
{
$this->resumeOrStartSession();
$_SESSION[Session::FLASH_NOW][$this->name][] = $val;
$_SESSION[Session::FLASH_NEXT][$this->name][] = $val;
}

/**
*
* Clears flash values for *both* the next request *and* the current one.
Expand Down
27 changes: 27 additions & 0 deletions src/SegmentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ public function getSegment();
*/
public function set($key, $val);

/**
*
* Append a value to a numeric key in the segment.
*
* @param mixed $val The value to append.
*
*/
public function add($val);

/**
*
* Clear all data from the segment.
Expand All @@ -70,6 +79,15 @@ public function clear();
*/
public function setFlash($key, $val);

/**
*
* Append a flash value with a numeric key for the *next* request.
*
* @param mixed $val The flash value itself.
*
*/
public function addFlash($val);

/**
*
* Gets the flash value for a key in the *current* request.
Expand Down Expand Up @@ -138,6 +156,15 @@ public function getAllFlashNext($alt = array());
*/
public function setFlashNow($key, $val);

/**
*
* Append a flash value with a numeric key for the *next* request *and* the current one.
*
* @param mixed $val The flash value itself.
*
*/
public function addFlashNow($val);

/**
*
* Clears flash values for *both* the next request *and* the current one.
Expand Down
9 changes: 6 additions & 3 deletions tests/SegmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,28 +72,31 @@ public function testGetSegment()
{
$this->segment->set('foo', 'bar');
$this->segment->set('baz', 'dib');
$this->segment->add('qux');
$this->assertSame('bar', $this->getValue('foo'));
$this->assertSame('dib', $this->getValue('baz'));

// now get the data
$this->assertSame(array('foo' => 'bar', 'baz' => 'dib'), $this->segment->getSegment());
$this->assertSame(array('foo' => 'bar', 'baz' => 'dib', 'qux'), $this->segment->getSegment());
}

public function testFlash()
{
// set a value
$this->segment->setFlash('foo', 'bar');
$this->segment->addFlash('baz');
$expect = 'bar';
$expectAll = ['foo' => 'bar'];
$expectAll = ['foo' => 'bar', 'baz'];
$this->assertSame($expect, $this->segment->getFlashNext('foo'));
$this->assertSame($expectAll, $this->segment->getAllFlashNext());
$this->assertNull($this->segment->getFlash('foo'));
$this->assertSame(array(), $this->segment->getAllCurrentFlash());

// set a value and make it available now
$this->segment->setFlashNow('baz', 'dib');
$this->segment->addFlashNow('qux');
$expect = 'dib';
$expectAll = ['baz' => 'dib'];
$expectAll = ['baz' => 'dib', 'qux'];
$this->assertSame($expect, $this->segment->getFlash('baz'));
$this->assertSame($expectAll, $this->segment->getAllCurrentFlash());
$this->assertSame($expect, $this->segment->getFlashNext('baz'));
Expand Down

0 comments on commit 5c7ca60

Please sign in to comment.