FRED™  3.0
FRED™: Framework for Rapid and Easy Development
Message.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi\Fred;
4 
5 class Message extends Component{
6 
7  /**
8  * Add a message to the session.
9  * @param string $type Message type.
10  * @param string $message The message.
11  * @param array $tags Tags for the message translation (set to false to prevent translation).
12  */
13  public function add($type,$message,$tags = null){
14  if($message = $tags === false ? $message : $this->component('trans')->str($message,$tags)){
15  $messages = $this->session->messages ?: [];
16  $messages[$message] = $type;
17  $this->session->messages = $messages;
18  }
19  }
20  /**
21  * Retrieve messages.
22  * @param string $type Retrieve only messages from this type (empty = retrieve all).
23  * @param bool $clear When true, the retrieved messages will be removed from the session.
24  * @return array Messages (key = translated message, value = type).
25  */
26  public function retrieve($type = null,$clear = true){
27  $messages = $this->session->messages ?: [];
28  $result = $type ? array_filter($messages,function($message_type) use ($type){ return $message_type == $type; }) : $messages;
29  if($clear) $this->session->messages = array_diff_key($messages,$result);
30  return $result;
31  }
32 
33  protected function _get($key){
34  return $this->retrieve($key);
35  }
36 
37  protected function _set($key,$value){
38  $this->add($key,$value);
39  }
40 
41  public function __call($func_name,$params){
42  $this->add($func_name,array_shift($params),array_shift($params));
43  }
44 
45  public function __invoke($message,$tags = null){
46  $this->add('message',$message,$tags);
47  }
48 
49 }
__invoke($message, $tags=null)
Definition: Message.php:45
_set($key, $value)
Definition: Message.php:37
Basic component class.
Definition: Component.php:8
add($type, $message, $tags=null)
Add a message to the session.
Definition: Message.php:13
retrieve($type=null, $clear=true)
Retrieve messages.
Definition: Message.php:26
component($name)
Get a component (local or default).
Definition: Component.php:80
__call($func_name, $params)
Definition: Message.php:41