Use an enum for the notice type
authorMarcel Werk <burntime@woltlab.com>
Wed, 22 Nov 2023 13:39:10 +0000 (14:39 +0100)
committerMarcel Werk <burntime@woltlab.com>
Wed, 22 Nov 2023 13:39:10 +0000 (14:39 +0100)
wcfsetup/install/files/lib/system/form/builder/NoticeFormNode.class.php
wcfsetup/install/files/lib/system/form/builder/NoticeFormNodeType.class.php [new file with mode: 0644]

index 3b00aa4b99637e946c7c7e9d98a9c7446a1a92e7..19fa232bf995e0381b812705330d1eddb88306fe 100644 (file)
@@ -12,33 +12,27 @@ namespace wcf\system\form\builder;
  */
 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;
     }
diff --git a/wcfsetup/install/files/lib/system/form/builder/NoticeFormNodeType.class.php b/wcfsetup/install/files/lib/system/form/builder/NoticeFormNodeType.class.php
new file mode 100644 (file)
index 0000000..77133ee
--- /dev/null
@@ -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',
+        };
+    }
+}