Skip to content

Commit

Permalink
Adjusted "add" method behaviour to work exactly as documented
Browse files Browse the repository at this point in the history
No more multiple messages assignment using the "add" method
  • Loading branch information
jbruni committed Sep 3, 2013
1 parent 37fa708 commit e25871c
Showing 1 changed file with 6 additions and 20 deletions.
26 changes: 6 additions & 20 deletions src/Jbruni/Larnotify/NotificationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,24 +138,22 @@ public function getBagType($where = 'default.msg')
* It depends on what has been optionally set by the first parameter:
* 1) A STRING or an ARRAY containing the remaining "sprintf" arguments. Example 1: "J. Bruni" Example 2: array("J. Bruni", "male")
* 2) An ARRAY containing the template data. Example: array("name" => "J. Bruni", "country" => "Brazil")
* 3) You can specify a STRING containing a single message or an ARRAY of string messages.
* 3) The message. Usually a STRING, but it can be of any type.
*
* @param string $template OPTIONAL (see above)
* @param string|array $message Message contents (see above)
* @param mixed $message Message contents (see above)
*/
public function add($template = '', $message = '')
{
$multiple = FALSE;
$where = $template;

if (func_num_args() < 2)
{
$message = $template;
$where = 'default.msg';
$multiple = is_array($message);
$message = $template;
}

if (preg_match('/^(([^.]+\.)?view):(.*)$/', $template, $matches) === 1)
elseif (preg_match('/^(([^.]+\.)?view):(.*)$/', $template, $matches) === 1)
{
$where = $matches[1];
$message = array(
Expand All @@ -164,7 +162,7 @@ public function add($template = '', $message = '')
);
}

if (preg_match('/^(([^.]+\.)?sprintf):(.*)$/', $template, $matches) === 1)
elseif (preg_match('/^(([^.]+\.)?sprintf):(.*)$/', $template, $matches) === 1)
{
$where = $matches[1];
$message = array_merge(
Expand All @@ -175,19 +173,7 @@ public function add($template = '', $message = '')

list($bag, $type) = $this->getBagType($where);

$messageBag = $this->getMessageBag($bag, self::CREATE_ATTACHED);

if (!$multiple)
{
return $messageBag->add($type, $message);
}

foreach ($message as $msg)
{
$result = $messageBag->add($type, $msg);
}

return $result;
return $this->getMessageBag($bag, self::CREATE_ATTACHED)->add($type, $message);
}

/**
Expand Down

0 comments on commit e25871c

Please sign in to comment.