Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x] Add "All Getters" for "Flash" values and "Add" #52

Open
wants to merge 4 commits into
base: 4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ echo $segment->get('foo'); // 'bar'

// because the segment is a reference to $_SESSION, we can modify
// the superglobal directly and the segment values will also change
$_SESSION['Vendor\Package\ClassName']['zim'] = 'gir'
$_SESSION['Vendor\Package\ClassName']['zim'] = 'gir';
echo $segment->get('zim'); // 'gir'
?>
```
Expand Down Expand Up @@ -114,6 +114,9 @@ Then, in subsequent requests, we can read the flash value using `getFlash()`:
<?php
$segment = $session->getSegment('Vendor\Package\ClassName');
$message = $segment->getFlash('message'); // 'Hello world!'

// You can also get all the flash messages
$messages = $segment->getFlashAll();
?>
```

Expand Down Expand Up @@ -185,7 +188,7 @@ Calling `destroy()` will also delete the session cookie via `setcookie()`. If we
// this will be used to delete the session cookie.
$delete_cookie = function ($name, $path, $domain) use ($response) {
$response->cookies->delete($name, $path, $domain);
}
};

$session = $session_factory->newInstance($_COOKIE, $delete_cookie);
?>
Expand Down
75 changes: 75 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 All @@ -165,6 +192,23 @@ public function getFlash($key, $alt = null)
: $alt;
}

/**
*
* Gets all the flash values in the *current* request.
*
* @param mixed $alt An alternative value to return if no flash values are set.
*
* @return mixed The flash values themselves.
*
*/
public function getAllCurrentFlash($alt = array())
{
$this->resumeSession();
return isset($_SESSION[Session::FLASH_NOW][$this->name])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't flash all means flash now + next ?

? $_SESSION[Session::FLASH_NOW][$this->name]
: $alt;
}

/**
*
* Clears flash values for *only* the next request.
Expand Down Expand Up @@ -198,6 +242,23 @@ public function getFlashNext($key, $alt = null)
: $alt;
}

/**
*
* Gets all flash values for the *next* request.
*
* @param mixed $alt An alternative value to return if no flash values set.
*
* @return mixed The flash values themselves.
*
*/
public function getAllFlashNext($alt = array())
{
$this->resumeSession();
return isset($_SESSION[Session::FLASH_NEXT][$this->name])
? $_SESSION[Session::FLASH_NEXT][$this->name]
: $alt;
}

/**
*
* Sets a flash value for the *next* request *and* the current one.
Expand All @@ -214,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
49 changes: 49 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 All @@ -83,6 +101,17 @@ public function setFlash($key, $val);
*/
public function getFlash($key, $alt = null);

/**
*
* Gets all the flash values in the *current* request.
*
* @param mixed $alt An alternative value to return if no flash values are set.
*
* @return mixed The flash values themselves.
*
*/
public function getAllCurrentFlash($alt = array());

/**
*
* Clears flash values for *only* the next request.
Expand All @@ -105,6 +134,17 @@ public function clearFlash();
*/
public function getFlashNext($key, $alt = null);

/**
*
* Gets all flash values for the *next* request.
*
* @param mixed $alt An alternative value to return if no flash values set.
*
* @return mixed The flash values themselves.
*
*/
public function getAllFlashNext($alt = array());

/**
*
* Sets a flash value for the *next* request *and* the current one.
Expand All @@ -116,6 +156,15 @@ public function getFlashNext($key, $alt = null);
*/
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
10 changes: 9 additions & 1 deletion tests/SegmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,33 @@ 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', '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', 'qux'];
$this->assertSame($expect, $this->segment->getFlash('baz'));
$this->assertSame($expectAll, $this->segment->getAllCurrentFlash());
$this->assertSame($expect, $this->segment->getFlashNext('baz'));

// clear the next values
Expand Down