Skip to content

Commit

Permalink
Use an enum for the notice type
Browse files Browse the repository at this point in the history
  • Loading branch information
BurntimeX committed Nov 22, 2023
1 parent ef127c8 commit 830bc12
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,27 @@
*/
class NoticeFormNode extends LanguageItemFormNode
{
const AVAILABLE_TYPES = ['info', 'success', 'warning', 'error'];

protected string $type = 'info';
protected NoticeFormNodeType $type = NoticeFormNodeType::Info;

/**
* @inheritDoc
*/
public function getHtml()
{
return '<woltlab-core-notice type="' . $this->getType() . '">' . parent::getHtml() . '</woltlab-core-notice>';
return '<woltlab-core-notice type="' . $this->getType()->toString() . '">' . parent::getHtml() . '</woltlab-core-notice>';
}

/**
* Sets the type of this notice.
*/
public function type(string $type): static
public function type(NoticeFormNodeType $type): static
{
if (!\in_array($type, self::AVAILABLE_TYPES)) {
throw new \BadMethodCallException("Invalid value '{$type}' given.");
}

$this->type = $type;

return $this;
}

public function getType(): string
public function getType(): NoticeFormNodeType
{
return $this->type;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace wcf\system\form\builder;

/**
* Defines the available types for the notice form node.
*
* @author Marcel Werk
* @copyright 2001-2023 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @since 6.1
*/
enum NoticeFormNodeType
{
case Info;
case Success;
case Warning;
case Error;

public function toString(): string
{
return match ($this) {
self::Info => 'info',
self::Success => 'success',
self::Warning => 'warning',
self::Error => 'error',
};
}
}

0 comments on commit 830bc12

Please sign in to comment.