$notification) { if (Notification::TYPE_PERSISTENT != $notification->type) { static::removeNotification($id); } } } /** * Determine all notifications that needs to be displayed. They are sorted by priority. Highest priorities first. * @return \ArrayObject */ public static function getAllNotificationsToDisplay() { $notifications = static::getAllNotifications(); uasort($notifications, function ($n1, $n2) { /** @var Notification $n1 */ /** @var Notification $n2 */ if ($n1->getPriority() == $n2->getPriority()) { return 0; } return $n1->getPriority() > $n2->getPriority() ? -1 : 1; }); return $notifications; } /** * @param $id * @throws \Exception In case id is empty or if id contains non word characters */ private static function checkId($id) { if (empty($id)) { throw new \Exception('Notification ID is empty.'); } if (!preg_match('/^(\w)*$/', $id)) { throw new \Exception('Invalid Notification ID given. Only word characters (AlNum + underscore) allowed.'); } } private static function addNotification($id, Notification $notification) { if (!self::isEnabled()) { return; } $session = static::getSession(); $session->notifications[$id] = $notification; } private static function getAllNotifications() { if (!self::isEnabled()) { return array(); } $session = static::getSession(); return $session->notifications; } private static function removeNotification($id) { if (!self::isEnabled()) { return; } $session = static::getSession(); if (array_key_exists($id, $session->notifications)) { unset($session->notifications[$id]); } } private static function isEnabled() { return Session::isWritable() && Session::isReadable(); } /** * @return SessionNamespace */ private static function getSession() { if (!isset(static::$session)) { static::$session = new SessionNamespace('notification'); } if (empty(static::$session->notifications) && self::isEnabled()) { static::$session->notifications = array(); } return static::$session; } }