Add FontAwesomeIconBrand class
authorTim Düsterhus <duesterhus@woltlab.com>
Tue, 14 Mar 2023 12:47:19 +0000 (13:47 +0100)
committerTim Düsterhus <duesterhus@woltlab.com>
Tue, 14 Mar 2023 12:47:19 +0000 (13:47 +0100)
see #5040

wcfsetup/install/files/lib/system/style/FontAwesomeIconBrand.class.php [new file with mode: 0644]

diff --git a/wcfsetup/install/files/lib/system/style/FontAwesomeIconBrand.class.php b/wcfsetup/install/files/lib/system/style/FontAwesomeIconBrand.class.php
new file mode 100644 (file)
index 0000000..b6785ef
--- /dev/null
@@ -0,0 +1,69 @@
+<?php
+
+namespace wcf\system\style;
+
+use wcf\system\style\exception\InvalidIconSize;
+use wcf\system\style\exception\UnknownIcon;
+
+/**
+ * Represents a Font Awesome brand icon.
+ *
+ * @author Tim Duesterhus
+ * @copyright 2001-2023 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @since 6.0
+ */
+final class FontAwesomeIconBrand implements IFontAwesomeIcon
+{
+    public const SIZES = IFontAwesomeIcon::SIZES;
+
+    private function __construct(
+        private readonly string $name
+    ) {
+        self::validateName($name);
+    }
+
+    /**
+     * Renders the HTML representation of an icon.
+     *
+     * @throws InvalidIconSize
+     */
+    public function toHtml(int $size = 16): string
+    {
+        if (!\in_array($size, self::SIZES)) {
+            throw new InvalidIconSize($size);
+        }
+
+        $content = \preg_replace(
+            '~^<svg~',
+            '<svg slot="svg"',
+            \file_get_contents(self::getFilename($this->name))
+        );
+
+        return <<<HTML
+        <fa-brand size="{$size}">{$content}</fa-brand>
+        HTML;
+    }
+
+    public static function fromName(string $name): self
+    {
+        return new self($name);
+    }
+
+    public static function isValidName(string $name): bool
+    {
+        return \file_exists(self::getFilename($name));
+    }
+
+    private static function validateName(string $name): void
+    {
+        if (!self::isValidName($name)) {
+            throw new UnknownIcon($name);
+        }
+    }
+
+    private static function getFilename(string $name): string
+    {
+        return \WCF_DIR . "icon/font-awesome/v6/brands/{$name}.svg";
+    }
+}