filter($message, "flash_message", $redirect_to); if (DEBUG and !headers_sent()) header("X-Chyrp-Flash-Messages: ".self::count($type)); if (isset($redirect_to)) redirect($redirect_to, $code); } /** * Function: notice * Create a notice (positive). * * Parameters: * $message - Text of the notice. * $redirect_to - URL to redirect to after the notice is stored. * $code - Numeric HTTP status code to set. */ public static function notice($message, $redirect_to = null, $code = null): void { $trigger = Trigger::current(); $type = self::FLASH_NOTICE; self::prepare($type); $_SESSION[$type][] = $trigger->filter($message, "flash_notice", $redirect_to); if (DEBUG and !headers_sent()) header("X-Chyrp-Flash-Notices: ".self::count($type)); if (isset($redirect_to)) redirect($redirect_to, $code); } /** * Function: warning * Create a warning (negative). * * Parameters: * $message - Text of the warning. * $redirect_to - URL to redirect to after the warning is stored. * $code - Numeric HTTP status code to set. */ public static function warning($message, $redirect_to = null, $code = null): void { $trigger = Trigger::current(); $type = self::FLASH_WARNING; self::prepare($type); $_SESSION[$type][] = $trigger->filter($message, "flash_warning", $redirect_to); if (DEBUG and !headers_sent()) header("X-Chyrp-Flash-Warnings: ".self::count($type)); if (isset($redirect_to)) redirect($redirect_to, $code); } /** * Function: messages * Calls "messages". */ public function messages(): array { return $this->serve(self::FLASH_MESSAGE); } /** * Function: notices * Calls "notices". */ public function notices(): array { return $this->serve(self::FLASH_NOTICE); } /** * Function: warnings * Calls "warnings". */ public function warnings(): array { return $this->serve(self::FLASH_WARNING); } /** * Function: all * Serves an associative array of all flashes. * * Returns: * An array of every flash available, * in the form of [type => [flashes]]. */ public function all(): array { return array( "messages" => $this->messages(), "notices" => $this->notices(), "warnings" => $this->warnings() ); } /** * Function: serve * Serves flashes and removes them from the session. * * Parameters: * $type - Type of flashes to serve. * * Returns: * An array of flashes of the requested type. */ private function serve($type): array { self::prepare($type); if (!empty($_SESSION[$type])) { $served = array_merge( self::$$type, $_SESSION[$type] ); self::$$type = $served; $_SESSION[$type] = array(); } return self::$$type; } /** * Function: exists * Checks for the existence of stored flashes. * * Parameters: * $type - Type to check for (optional). */ public static function exists($type = null): bool { switch ($type) { case self::FLASH_MESSAGE: case self::FLASH_NOTICE: case self::FLASH_WARNING: $check = array($type); break; case null: $check = array( self::FLASH_MESSAGE, self::FLASH_NOTICE, self::FLASH_WARNING ); break; default: return false; } foreach ($check as $type) { self::prepare($type); if (!empty(self::$$type)) return true; if (!empty($_SESSION[$type])) return true; } return false; } /** * Function: count * Counts the total number of stored flashes. * * Parameters: * $type - Type to check for (optional). */ public static function count($type = null): int { $total = 0; switch ($type) { case self::FLASH_MESSAGE: case self::FLASH_NOTICE: case self::FLASH_WARNING: $count = array($type); break; case null: $count = array( self::FLASH_MESSAGE, self::FLASH_NOTICE, self::FLASH_WARNING ); break; default: return $total; } foreach ($count as $type) { self::prepare($type); if (!empty(self::$$type)) $total+= count(self::$$type); if (!empty($_SESSION[$type])) $total+= count($_SESSION[$type]); } return $total; } /** * Function: current * Returns a singleton reference to the current class. */ public static function & current(): self { static $instance = null; $instance = (empty($instance)) ? new self() : $instance ; return $instance; } }