/** * Get messages. * * $messages = RD::get(); * * // Get error messages * $error_messages = RD::get(RD::ERROR); * * // Get error AND alert messages * $messages = RD::get(array(RD::ERROR, RD::ALERT)); * * // Get everything except error AND alert messages * $messages = RD::get(array(1 => array(RD::ERROR, RD::ALERT))); * * // Customize the default value * $error_messages = RD::get(RD::ERROR, 'my default value'); * * @param mixed $type message type (e.g. RD::SUCCESS, array(RD::ERROR, RD::ALERT)) * @param mixed $default default value to return * @param bool $delete delete the messages? * @param bool $return_array also return messages that were stored as arrays? * @return mixed */ public static function get($type = NULL, $default = NULL, $delete = FALSE, $return_array = true) { // Load existing messages $messages = Session::instance()->get(RD::$storage_key); if ($messages === NULL && count(self::$_msg) == 0) { // No messages found if ($return_array == true) { return $default; } else { $output = array(); foreach (self::$_msg as $msg) { if (!is_array($msg['value'])) { $output[] = $msg; } } return $output; } } if ($type !== NULL) { // Will hold the filtered set of messages to return $return = array(); // Store the remainder in case `delete` OR `get_once` is called $remainder = array(); foreach ($messages as $message) { if ($message['type'] === $type or is_array($type) and in_array($message['type'], $type) or is_array($type) and Arr::is_assoc($type) and !in_array($message['type'], $type[1])) { if ($return_array == true) { $return[] = $message; } else { if ($return_array == false && !is_array($message)) { $return[] = $message; } else { $remainder[] = $message; } } } else { $remainder[] = $message; } } if (empty($return)) { // No messages of '$type' to return return $default; } $messages = $return; } if ($delete === TRUE) { if ($type === NULL or empty($remainder)) { // Nothing to save, delete the key from memory RD::delete(); } else { // Override messages with the remainder to simulate a deletion Session::instance()->set(RD::$storage_key, $remainder); } } return $messages; }