From 415a0a96a76afbe7f1ad2f51862641793caf1b6c Mon Sep 17 00:00:00 2001
From: Jonas Kohl
Date: Sun, 8 Sep 2024 17:53:55 +0200
Subject: Initial commit

---
 src/application/mystic/forum/utils/ArrayUtils.php  | 33 ++++++++++++++++++++++
 src/application/mystic/forum/utils/StaticClass.php | 10 +++++++
 src/application/mystic/forum/utils/StringUtils.php | 24 ++++++++++++++++
 3 files changed, 67 insertions(+)
 create mode 100644 src/application/mystic/forum/utils/ArrayUtils.php
 create mode 100644 src/application/mystic/forum/utils/StaticClass.php
 create mode 100644 src/application/mystic/forum/utils/StringUtils.php

(limited to 'src/application/mystic/forum/utils')

diff --git a/src/application/mystic/forum/utils/ArrayUtils.php b/src/application/mystic/forum/utils/ArrayUtils.php
new file mode 100644
index 0000000..ed77c6d
--- /dev/null
+++ b/src/application/mystic/forum/utils/ArrayUtils.php
@@ -0,0 +1,33 @@
+<?php
+declare(strict_types=1);
+
+namespace mystic\forum\utils;
+
+final class ArrayUtils {
+    use StaticClass;
+
+    public static function repeat(mixed $value, int $count): array {
+        $arr = [];
+        for ($i = 0; $i < $count; ++$i)
+            $arr []= $value;
+        return $arr;
+    }
+
+    public static function fill(\Closure $factory, int $count): array {
+        $arr = [];
+        for ($i = 0; $i < $count; ++$i)
+            $arr []= $factory($i, $count);
+        return $arr;
+    }
+
+    public static function assocFromPairs(array $pairs): array {
+        return array_combine(array_column($pairs, 0), array_column($pairs, 1));
+    }
+
+    public static function asPairs(array $arr): array {
+        $out = [];
+        foreach ($arr as $k => $v)
+            $out []= [$k, $v];
+        return $out;
+    }
+}
diff --git a/src/application/mystic/forum/utils/StaticClass.php b/src/application/mystic/forum/utils/StaticClass.php
new file mode 100644
index 0000000..e55ada9
--- /dev/null
+++ b/src/application/mystic/forum/utils/StaticClass.php
@@ -0,0 +1,10 @@
+<?php
+declare(strict_types=1);
+
+namespace mystic\forum\utils;
+
+trait StaticClass {
+    public function __construct() {
+        throw new \RuntimeException("Cannot instantiate static class " . self::class);
+    }
+}
diff --git a/src/application/mystic/forum/utils/StringUtils.php b/src/application/mystic/forum/utils/StringUtils.php
new file mode 100644
index 0000000..7d4bf9d
--- /dev/null
+++ b/src/application/mystic/forum/utils/StringUtils.php
@@ -0,0 +1,24 @@
+<?php
+declare(strict_types=1);
+
+namespace mystic\forum\utils;
+
+final class StringUtils {
+    use StaticClass;
+
+    public static function camelToSnake(string $camelCase): string {
+        $result = '';
+
+        for ($i = 0; $i < strlen($camelCase); $i++) {
+            $char = $camelCase[$i];
+
+            if (ctype_upper($char)) {
+                $result .= '_' . strtolower($char);
+            } else {
+                $result .= $char;
+            }
+        }
+
+        return ltrim($result, '_');
+    }
+}
-- 
cgit v1.2.3