Skip to content

Commit 5c7ca60

Browse files
committed
Add add methods to append numeric key values
1 parent fa67dd9 commit 5c7ca60

File tree

3 files changed

+74
-3
lines changed

3 files changed

+74
-3
lines changed

Diff for: src/Segment.php

+41
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,19 @@ public function set($key, $val)
9999
$_SESSION[$this->name][$key] = $val;
100100
}
101101

102+
/**
103+
*
104+
* Append a value to a numeric key in the segment.
105+
*
106+
* @param mixed $val The value to append.
107+
*
108+
*/
109+
public function add($val)
110+
{
111+
$this->resumeOrStartSession();
112+
$_SESSION[$this->name][] = $val;
113+
}
114+
102115
/**
103116
*
104117
* Clear all data from the segment.
@@ -146,6 +159,20 @@ public function setFlash($key, $val)
146159
$_SESSION[Session::FLASH_NEXT][$this->name][$key] = $val;
147160
}
148161

162+
163+
/**
164+
*
165+
* Append a flash value with a numeric key for the *next* request.
166+
*
167+
* @param mixed $val The flash value itself.
168+
*
169+
*/
170+
public function addFlash($val)
171+
{
172+
$this->resumeOrStartSession();
173+
$_SESSION[Session::FLASH_NEXT][$this->name][] = $val;
174+
}
175+
149176
/**
150177
*
151178
* Gets the flash value for a key in the *current* request.
@@ -248,6 +275,20 @@ public function setFlashNow($key, $val)
248275
$_SESSION[Session::FLASH_NEXT][$this->name][$key] = $val;
249276
}
250277

278+
/**
279+
*
280+
* Append a flash value with a numeric key for the *next* request *and* the current one.
281+
*
282+
* @param mixed $val The flash value itself.
283+
*
284+
*/
285+
public function addFlashNow($val)
286+
{
287+
$this->resumeOrStartSession();
288+
$_SESSION[Session::FLASH_NOW][$this->name][] = $val;
289+
$_SESSION[Session::FLASH_NEXT][$this->name][] = $val;
290+
}
291+
251292
/**
252293
*
253294
* Clears flash values for *both* the next request *and* the current one.

Diff for: src/SegmentInterface.php

+27
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ public function getSegment();
5050
*/
5151
public function set($key, $val);
5252

53+
/**
54+
*
55+
* Append a value to a numeric key in the segment.
56+
*
57+
* @param mixed $val The value to append.
58+
*
59+
*/
60+
public function add($val);
61+
5362
/**
5463
*
5564
* Clear all data from the segment.
@@ -70,6 +79,15 @@ public function clear();
7079
*/
7180
public function setFlash($key, $val);
7281

82+
/**
83+
*
84+
* Append a flash value with a numeric key for the *next* request.
85+
*
86+
* @param mixed $val The flash value itself.
87+
*
88+
*/
89+
public function addFlash($val);
90+
7391
/**
7492
*
7593
* Gets the flash value for a key in the *current* request.
@@ -138,6 +156,15 @@ public function getAllFlashNext($alt = array());
138156
*/
139157
public function setFlashNow($key, $val);
140158

159+
/**
160+
*
161+
* Append a flash value with a numeric key for the *next* request *and* the current one.
162+
*
163+
* @param mixed $val The flash value itself.
164+
*
165+
*/
166+
public function addFlashNow($val);
167+
141168
/**
142169
*
143170
* Clears flash values for *both* the next request *and* the current one.

Diff for: tests/SegmentTest.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -72,28 +72,31 @@ public function testGetSegment()
7272
{
7373
$this->segment->set('foo', 'bar');
7474
$this->segment->set('baz', 'dib');
75+
$this->segment->add('qux');
7576
$this->assertSame('bar', $this->getValue('foo'));
7677
$this->assertSame('dib', $this->getValue('baz'));
7778

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

8283
public function testFlash()
8384
{
8485
// set a value
8586
$this->segment->setFlash('foo', 'bar');
87+
$this->segment->addFlash('baz');
8688
$expect = 'bar';
87-
$expectAll = ['foo' => 'bar'];
89+
$expectAll = ['foo' => 'bar', 'baz'];
8890
$this->assertSame($expect, $this->segment->getFlashNext('foo'));
8991
$this->assertSame($expectAll, $this->segment->getAllFlashNext());
9092
$this->assertNull($this->segment->getFlash('foo'));
9193
$this->assertSame(array(), $this->segment->getAllCurrentFlash());
9294

9395
// set a value and make it available now
9496
$this->segment->setFlashNow('baz', 'dib');
97+
$this->segment->addFlashNow('qux');
9598
$expect = 'dib';
96-
$expectAll = ['baz' => 'dib'];
99+
$expectAll = ['baz' => 'dib', 'qux'];
97100
$this->assertSame($expect, $this->segment->getFlash('baz'));
98101
$this->assertSame($expectAll, $this->segment->getAllCurrentFlash());
99102
$this->assertSame($expect, $this->segment->getFlashNext('baz'));

0 commit comments

Comments
 (0)