From 5c7ca60d9b32b200fcb13d9ebb475e23ef49a860 Mon Sep 17 00:00:00 2001 From: jake johns Date: Mon, 11 Apr 2016 13:28:39 -0400 Subject: [PATCH] Add `add` methods to append numeric key values --- src/Segment.php | 41 ++++++++++++++++++++++++++++++++++++++++ src/SegmentInterface.php | 27 ++++++++++++++++++++++++++ tests/SegmentTest.php | 9 ++++++--- 3 files changed, 74 insertions(+), 3 deletions(-) diff --git a/src/Segment.php b/src/Segment.php index 76c6ba5..05c834a 100644 --- a/src/Segment.php +++ b/src/Segment.php @@ -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. @@ -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. @@ -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. diff --git a/src/SegmentInterface.php b/src/SegmentInterface.php index 987bbda..3402b64 100644 --- a/src/SegmentInterface.php +++ b/src/SegmentInterface.php @@ -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. @@ -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. @@ -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. diff --git a/tests/SegmentTest.php b/tests/SegmentTest.php index a80fc4f..6ecfd22 100644 --- a/tests/SegmentTest.php +++ b/tests/SegmentTest.php @@ -72,19 +72,21 @@ 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')); @@ -92,8 +94,9 @@ public function testFlash() // 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'));