Skip to content

Commit 3a638f7

Browse files
committed
Add Flash "Get All" methods
- Segment::getFlashAll(); - Segment::getFlashNextAll();
1 parent 3a85b6b commit 3a638f7

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

Diff for: README.md

+3
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ Then, in subsequent requests, we can read the flash value using `getFlash()`:
114114
<?php
115115
$segment = $session->getSegment('Vendor\Package\ClassName');
116116
$message = $segment->getFlash('message'); // 'Hello world!'
117+
118+
// You can also get all the flash messages
119+
$messages = $segment->getFlashAll();
117120
?>
118121
```
119122

Diff for: src/Segment.php

+34
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,23 @@ public function getFlash($key, $alt = null)
165165
: $alt;
166166
}
167167

168+
/**
169+
*
170+
* Gets all the flash values in the *current* request.
171+
*
172+
* @param mixed $alt An alternative value to return if no flash values are set.
173+
*
174+
* @return mixed The flash values themselves.
175+
*
176+
*/
177+
public function getFlashAll($alt = array())
178+
{
179+
$this->resumeSession();
180+
return isset($_SESSION[Session::FLASH_NOW][$this->name])
181+
? $_SESSION[Session::FLASH_NOW][$this->name]
182+
: $alt;
183+
}
184+
168185
/**
169186
*
170187
* Clears flash values for *only* the next request.
@@ -198,6 +215,23 @@ public function getFlashNext($key, $alt = null)
198215
: $alt;
199216
}
200217

218+
/**
219+
*
220+
* Gets all flash values for the *next* request.
221+
*
222+
* @param mixed $alt An alternative value to return if no flash values set.
223+
*
224+
* @return mixed The flash values themselves.
225+
*
226+
*/
227+
public function getFlashNextAll($alt = array())
228+
{
229+
$this->resumeSession();
230+
return isset($_SESSION[Session::FLASH_NEXT][$this->name])
231+
? $_SESSION[Session::FLASH_NEXT][$this->name]
232+
: $alt;
233+
}
234+
201235
/**
202236
*
203237
* Sets a flash value for the *next* request *and* the current one.

Diff for: src/SegmentInterface.php

+22
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,17 @@ public function setFlash($key, $val);
8383
*/
8484
public function getFlash($key, $alt = null);
8585

86+
/**
87+
*
88+
* Gets all the flash values in the *current* request.
89+
*
90+
* @param mixed $alt An alternative value to return if no flash values are set.
91+
*
92+
* @return mixed The flash values themselves.
93+
*
94+
*/
95+
public function getFlashAll($alt = array());
96+
8697
/**
8798
*
8899
* Clears flash values for *only* the next request.
@@ -105,6 +116,17 @@ public function clearFlash();
105116
*/
106117
public function getFlashNext($key, $alt = null);
107118

119+
/**
120+
*
121+
* Gets all flash values for the *next* request.
122+
*
123+
* @param mixed $alt An alternative value to return if no flash values set.
124+
*
125+
* @return mixed The flash values themselves.
126+
*
127+
*/
128+
public function getFlashNextAll($alt = array());
129+
108130
/**
109131
*
110132
* Sets a flash value for the *next* request *and* the current one.

Diff for: tests/SegmentTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,18 @@ public function testFlash()
8484
// set a value
8585
$this->segment->setFlash('foo', 'bar');
8686
$expect = 'bar';
87+
$expectAll = ['foo' => 'bar'];
8788
$this->assertSame($expect, $this->segment->getFlashNext('foo'));
89+
$this->assertSame($expectAll, $this->segment->getFlashNextAll());
8890
$this->assertNull($this->segment->getFlash('foo'));
91+
$this->assertSame(array(), $this->segment->getFlashAll());
8992

9093
// set a value and make it available now
9194
$this->segment->setFlashNow('baz', 'dib');
9295
$expect = 'dib';
96+
$expectAll = ['baz' => 'dib'];
9397
$this->assertSame($expect, $this->segment->getFlash('baz'));
98+
$this->assertSame($expectAll, $this->segment->getFlashAll());
9499
$this->assertSame($expect, $this->segment->getFlashNext('baz'));
95100

96101
// clear the next values

0 commit comments

Comments
 (0)