From 2a1d701846850542e81646f7c8b9ae3e3152950d Mon Sep 17 00:00:00 2001 From: DualFroz Date: Sun, 23 Aug 2026 17:42:27 +0200 Subject: [PATCH] feat(support): add role permissions, translator and helper functions --- src/Support/Permissions.php | 31 +++++++++++++++++ src/Support/Role.php | 31 +++++++++++++++++ src/Support/Translator.php | 66 +++++++++++++++++++++++++++++++++++++ src/Support/helpers.php | 37 +++++++++++++++++++++ 4 files changed, 165 insertions(+) create mode 100644 src/Support/Permissions.php create mode 100644 src/Support/Role.php create mode 100644 src/Support/Translator.php create mode 100644 src/Support/helpers.php diff --git a/src/Support/Permissions.php b/src/Support/Permissions.php new file mode 100644 index 0000000..00f01bd --- /dev/null +++ b/src/Support/Permissions.php @@ -0,0 +1,31 @@ + 'Administrator', + self::manager => 'Manager', + default => 'Customer', + }; + } + + public static function isStaff(string $role): bool + { + return in_array($role, self::staffRoles, true); + } + + public static function all(): array + { + return [self::customer, self::manager, self::admin]; + } +} diff --git a/src/Support/Translator.php b/src/Support/Translator.php new file mode 100644 index 0000000..dc1c722 --- /dev/null +++ b/src/Support/Translator.php @@ -0,0 +1,66 @@ +locale = self::isSupported($locale) ? $locale : $this->fallback; + } + + public function locale(): string + { + return $this->locale; + } + + public static function isSupported(string $locale): bool + { + return in_array($locale, self::supported, true); + } + + public function get(string $key, array $replacements = []): string + { + $line = $this->lookup($this->locale, $key) + ?? $this->lookup($this->fallback, $key) + ?? $key; + + foreach ($replacements as $token => $value) { + $line = str_replace(':' . $token, (string) $value, $line); + } + return $line; + } + + private function lookup(string $locale, string $key): ?string + { + $catalog = $this->catalog($locale); + $value = $catalog; + foreach (explode('.', $key) as $segment) { + if (!is_array($value) || !array_key_exists($segment, $value)) { + return null; + } + $value = $value[$segment]; + } + return is_string($value) ? $value : null; + } + + private function catalog(string $locale): array + { + if (!isset($this->catalogs[$locale])) { + $file = $this->langPath . '/' . $locale . '.php'; + $this->catalogs[$locale] = is_file($file) ? require $file : []; + } + return $this->catalogs[$locale]; + } +} diff --git a/src/Support/helpers.php b/src/Support/helpers.php new file mode 100644 index 0000000..883b563 --- /dev/null +++ b/src/Support/helpers.php @@ -0,0 +1,37 @@ + '$', + 'EUR' => 'EUR ', + 'PLN' => 'PLN ', + default => strtoupper($currency) . ' ', + }; + return $symbol . number_format($cents / 100, 2); +}