Skip to content

Commit

Permalink
Type hint options array param, use + operater for array merge, update…
Browse files Browse the repository at this point in the history
… docblocks
  • Loading branch information
bcrowe committed Jun 11, 2014
1 parent 9678f44 commit cf19f58
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
11 changes: 7 additions & 4 deletions src/Controller/Component/FlashComponent.php
Expand Up @@ -20,14 +20,15 @@
use Cake\Error\InternalErrorException;
use Cake\Event\Event;


/**
* FlashComponent
* The CakePHP FlashComponent provides a way for you to write a flash variable
* to the session from your controllers, to be rendered in a view with the
* FlashHelper.
*/
class FlashComponent extends Component {

/**
* The session
* The Session object instance
*
* @var \Cake\Network\Session
*/
Expand Down Expand Up @@ -64,7 +65,6 @@ public function __construct(ComponentRegistry $collection, array $config = []) {
*
* - `key` The key to set under the session's Message key
* - `element` The element used to render the flash message
* - `escape` Whether or not to escape the flash message, defaults to true
* - `params` An array of variables to make available when using an element
*
* @param string $message Message to be flashed
Expand All @@ -89,6 +89,9 @@ public function set($message, array $options = []) {
/**
* Magic method for verbose flash methods based on element names.
*
* For example: $this->Flash->success('My message') would use the
* flash_success.ctp element for rendering the flash message.
*
* @param string $name Element name to use, omitting the "flash_" prefix.
* @param array $args Parameters to pass when calling `FlashComponent::set`.
* @return void
Expand Down
14 changes: 3 additions & 11 deletions src/View/Helper/FlashHelper.php
Expand Up @@ -36,21 +36,13 @@ class FlashHelper extends Helper {
* to consolidate all the parameters for a given type of flash message into the view.
*
* {{{
* echo $this->Flash->render('flash', ['class' => 'new-flash']);
* }}}
*
* The above would generate a flash message with a custom class name. Using $attrs['params'] you
* can pass additional data into the element rendering that will be made available as local variables
* when the element is rendered:
*
* {{{
* echo $this->Flash->render('flash', ['params' => ['name' => $user['User']['name']]]);
* }}}
*
* This would pass the current user's name into the flash message, so you could create personalized
* messages without the controller needing access to that data.
*
* Lastly you can choose the element that is rendered when creating the flash message. Using
* Lastly you can choose the element that is rendered when rendering the flash message. Using
* custom elements allows you to fully customize how flash messages are generated.
*
* {{{
Expand All @@ -71,15 +63,15 @@ class FlashHelper extends Helper {
* Supports the 'params', and 'element' keys that are used in the helper.
* @return string
*/
public function render($key = 'flash', $options = []) {
public function render($key = 'flash', array $options = []) {
$flash = $this->request->session()->read("Message.$key");
$this->request->session()->delete("Message.$key");

if (!$flash) {
return '';
}

$flash = array_merge($flash, $options);
$flash = $options + $flash;

if ($flash['element'] === null) {
return $flash['message'];
Expand Down

0 comments on commit cf19f58

Please sign in to comment.