Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UserNotification] Add more configuration options #176

Merged
merged 17 commits into from
Dec 12, 2023
139 changes: 128 additions & 11 deletions src/Event/NotificationEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,152 @@

namespace KevinPapst\TablerBundle\Event;

use KevinPapst\TablerBundle\Model\NotificationV2Interface;
use KevinPapst\TablerBundle\Model\NotificationInterface;

class NotificationEvent extends ThemeEvent
class NotificationEvent extends ThemeEvent implements NotificationEventInterface
cavasinf marked this conversation as resolved.
Show resolved Hide resolved
{
private ?string $title = null;

private ?string $titleEmpty = null;

private ?string $titleHtml = null;

private bool $withArrow = true;

private string $badgeColor = 'red';

private bool $showBadgeTotal = true;

private int $maxDisplay = 10;

/**
* @var array<NotificationInterface>
* @var $notifications array<int,NotificationInterface | NotificationV2Interface>
*/
private array $notifications = [];

public function getTotal(): int
{
return \count($this->getNotifications(null));
}

public function isVisible(): bool
{
return $this->getTotal() > 0 || ($this->getTotal() === 0 && $this->showIfEmpty);
}

public function __construct(
private bool $showIfEmpty = false,
) {
}

public function isShowIfEmpty(): bool
{
return $this->showIfEmpty;
}

public function setShowIfEmpty(bool $showIfEmpty): void
{
$this->showIfEmpty = $showIfEmpty;
}

public function getTitle(): ?string
{
return $this->title;
}

public function setTitle(?string $title): void
{
$this->title = $title;
}

public function getTitleEmpty(): ?string
{
return $this->titleEmpty;
}

public function setTitleEmpty(?string $titleEmpty): void
{
$this->titleEmpty = $titleEmpty;
}

public function getTitleHtml(): ?string
{
return $this->titleHtml;
}

public function setTitleHtml(?string $titleHtml): void
{
$this->titleHtml = $titleHtml;
}

public function isWithArrow(): bool
{
return $this->withArrow;
}

public function setWithArrow(bool $withArrow): void
{
$this->withArrow = $withArrow;
}

public function getBadgeColor(): string
{
return $this->badgeColor;
}

public function setBadgeColor(string $badgeColor): void
{
$this->badgeColor = $badgeColor;
}

public function isShowBadgeTotal(): bool
{
return $this->showBadgeTotal;
}

public function setShowBadgeTotal(bool $showBadgeTotal): void
{
$this->showBadgeTotal = $showBadgeTotal;
}

public function getMaxDisplay(): int
{
return $this->maxDisplay;
}

public function setMaxDisplay(int $maxDisplay): void
{
$this->maxDisplay = $maxDisplay;
}

/**
* @param int|null $max
* @return NotificationInterface[]
* @return array<int,NotificationInterface | NotificationV2Interface>
*/
public function getNotifications(?int $max = 10): array
{
if (null !== $max) {
return \array_slice($this->notifications, 0, $max);
if ($max === null) {
return $this->notifications;
} elseif ($max !== 10) {
trigger_deprecation('kevinpapst/tabler-bundle', '1.1.0', 'Setting `$max` parameter is deprecated. Use setMaxDisplay() instead!');
}

return $this->notifications;
return \array_slice($this->notifications, 0, $this->maxDisplay);
}

public function addNotification(NotificationInterface $notificationInterface): void
public function addNotification(NotificationV2Interface|NotificationInterface $notification): void
{
$this->notifications[] = $notificationInterface;
// if (($notification instanceof NotificationV2Interface) === false) {
// trigger_deprecation('kevinpapst/tabler-bundle', '1.2.0', 'Notification should implement NotificationV2Interface::class!');
// }

$this->notifications[] = $notification;
}

public function getTotal(): int
public function removeNotification(NotificationInterface $notification): void
{
return \count($this->notifications);
if (($key = array_search($notification, $this->notifications)) !== false) {
unset($this->notifications[$key]);
}
}
}
34 changes: 34 additions & 0 deletions src/Event/NotificationEventInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace KevinPapst\TablerBundle\Event;

use KevinPapst\TablerBundle\Model\NotificationInterface;
use KevinPapst\TablerBundle\Model\NotificationV2Interface;

interface NotificationEventInterface
{
public function getTotal(): int;

public function isVisible(): bool;

public function isShowIfEmpty(): bool;

public function getTitle(): ?string;

public function getTitleEmpty(): ?string;

public function getTitleHtml(): ?string;

public function isWithArrow(): bool;

public function getBadgeColor(): string;

public function isShowBadgeTotal(): bool;

public function getMaxDisplay(): int;

/**
* @return array<int,NotificationInterface | NotificationV2Interface>
*/
public function getNotifications(?int $max = 10): array;
}
42 changes: 21 additions & 21 deletions src/Model/NotificationInterface.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<?php

/*
* This file is part of the Tabler bundle, created by Kevin Papst (www.kevinpapst.de).
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace KevinPapst\TablerBundle\Model;

interface NotificationInterface
{
public function getIdentifier(): string;

public function getMessage(): string;

public function getType(): string;

public function getUrl(): ?string;
}
<?php
/*
* This file is part of the Tabler bundle, created by Kevin Papst (www.kevinpapst.de).
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace KevinPapst\TablerBundle\Model;
interface NotificationInterface
{
kevinpapst marked this conversation as resolved.
Show resolved Hide resolved
public function getIdentifier(): string;
public function getMessage(): string;
public function getType(): string;
public function getUrl(): ?string;
}
88 changes: 76 additions & 12 deletions src/Model/NotificationModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,105 @@

use KevinPapst\TablerBundle\Helper\Constants;

class NotificationModel implements NotificationInterface
class NotificationModel implements NotificationV2Interface
{
private ?string $url = null;
private ?string $url = null;
private bool $active = false;
private bool $disabled = false;
private bool $withBadge = true;
private bool $badgeAnimated = true;

public function __construct(private string $id, private string $message, private string $type = Constants::TYPE_INFO)
{
private bool $html = false;

public function __construct(
private readonly string $id,
private string $message,
private string $type = Constants::TYPE_INFO
) {
}

public function getIdentifier(): string
{
return $this->id;
}

public function getType(): string
public function getUrl(): ?string
{
return $this->type;
return $this->url;
}

public function setMessage(string $message): void
public function setUrl(?string $url): void
{
$this->message = $message;
$this->url = $url;
}

public function isActive(): bool
{
return $this->active;
}

public function setActive(bool $active): void
{
$this->active = $active;
}

public function isDisabled(): bool
{
return $this->disabled;
}

public function setDisabled(bool $disabled): void
{
$this->disabled = $disabled;
}

public function isWithBadge(): bool
{
return $this->withBadge;
}

public function setWithBadge(bool $withBadge): void
{
$this->withBadge = $withBadge;
}

public function isBadgeAnimated(): bool
{
return $this->badgeAnimated;
}

public function setBadgeAnimated(bool $badgeAnimated): void
{
$this->badgeAnimated = $badgeAnimated;
}

public function isHtml(): bool
{
return $this->html;
}

public function setHtml(bool $html): void
{
$this->html = $html;
}

public function getMessage(): string
{
return $this->message;
}

public function setUrl(?string $url): void
public function setMessage(string $message): void
{
$this->url = $url;
$this->message = $message;
}

public function getUrl(): ?string
public function getType(): string
{
return $this->url;
return $this->type;
}

public function setType(string $type): void
{
$this->type = $type;
}
}
24 changes: 24 additions & 0 deletions src/Model/NotificationV2Interface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the Tabler bundle, created by Kevin Papst (www.kevinpapst.de).
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace KevinPapst\TablerBundle\Model;

// TODO : Implement these in NotificationInterface later
interface NotificationV2Interface extends NotificationInterface
cavasinf marked this conversation as resolved.
Show resolved Hide resolved
{
public function isActive(): bool;

public function isDisabled(): bool;

public function isWithBadge(): bool;

public function isBadgeAnimated(): bool;

public function isHtml(): bool;
}
Loading
Loading