Add column renderer for currencies
authorMarcel Werk <burntime@woltlab.com>
Wed, 13 Nov 2024 13:14:48 +0000 (14:14 +0100)
committerMarcel Werk <burntime@woltlab.com>
Wed, 13 Nov 2024 13:14:48 +0000 (14:14 +0100)
wcfsetup/install/files/lib/system/gridView/renderer/CurrencyColumnRenderer.class.php [new file with mode: 0644]

diff --git a/wcfsetup/install/files/lib/system/gridView/renderer/CurrencyColumnRenderer.class.php b/wcfsetup/install/files/lib/system/gridView/renderer/CurrencyColumnRenderer.class.php
new file mode 100644 (file)
index 0000000..6c07c28
--- /dev/null
@@ -0,0 +1,38 @@
+<?php
+
+namespace wcf\system\gridView\renderer;
+
+use wcf\system\WCF;
+use wcf\util\StringUtil;
+
+/**
+ * Formats the content of a column as a currency.
+ *
+ * @author      Marcel Werk
+ * @copyright   2001-2024 WoltLab GmbH
+ * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @since       6.2
+ */
+class CurrencyColumnRenderer extends AbstractColumnRenderer
+{
+    public function __construct(
+        private readonly string $currency,
+    ) {}
+
+    #[\Override]
+    public function render(mixed $value, mixed $context = null): string
+    {
+        return \number_format(
+            \round($value, 2),
+            2,
+            WCF::getLanguage()->get('wcf.global.decimalPoint'),
+            WCF::getLanguage()->get('wcf.global.thousandsSeparator')
+        ) . ' ' . StringUtil::encodeHTML($this->currency);
+    }
+
+    #[\Override]
+    public function getClasses(): string
+    {
+        return 'gridView__column--digits';
+    }
+}