engelsystem/src/Controllers/HasUserNotifications.php

46 lines
1.1 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace Engelsystem\Controllers;
use Illuminate\Support\Collection;
trait HasUserNotifications
{
2023-02-02 22:53:51 +01:00
protected function addNotification(string|array $value, NotificationType $type = NotificationType::MESSAGE): void
{
2023-02-02 22:53:51 +01:00
$type = 'messages.' . $type->value;
session()->set(
$type,
2023-02-02 22:53:51 +01:00
array_merge_recursive(session()->get($type, []), (array) $value)
);
}
2023-02-02 22:53:51 +01:00
/**
* @param NotificationType[]|null $types
* @return array<string,Collection|array<string>>
*/
protected function getNotifications(array $types = null): array
{
$return = [];
2023-02-02 22:53:51 +01:00
$types = $types ?: [
NotificationType::ERROR,
NotificationType::WARNING,
NotificationType::INFORMATION,
NotificationType::MESSAGE,
];
foreach ($types as $type) {
$type = $type->value;
$path = 'messages.' . $type;
$return[$type] = Collection::make(
session()->get($path, [])
)->flatten();
session()->remove($path);
}
return $return;
}
}