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); +}