feat(support): add role permissions, translator and helper functions

This commit is contained in:
dualfroz committed 2026-08-23 17:42:27 +02:00
1 parent 32995b714b
commit 2a1d701846
4 files changed
+165

No files matched your search

+31
View File
@@ -0,0 +1,31 @@
<?php
namespace Bazaar\Support;
final class Permissions
{
public const catalogManage = 'catalog.manage';
public const ordersManage = 'orders.manage';
public const reviewsModerate = 'reviews.moderate';
public const analyticsView = 'analytics.view';
public const couponsManage = 'coupons.manage';
public const usersManage = 'users.manage';
private const managerGrants = [
self::catalogManage,
self::ordersManage,
self::reviewsModerate,
self::analyticsView,
];
public static function allows(string $role, string $capability): bool
{
if ($role === Role::admin) {
return true;
}
if ($role === Role::manager) {
return in_array($capability, self::managerGrants, true);
}
return false;
}
}
+31
View File
@@ -0,0 +1,31 @@
<?php
namespace Bazaar\Support;
final class Role
{
public const customer = 'customer';
public const manager = 'manager';
public const admin = 'admin';
public const staffRoles = [self::manager, self::admin];
public static function label(string $role): string
{
return match ($role) {
self::admin => '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];
}
}
+66
View File
@@ -0,0 +1,66 @@
<?php
namespace Bazaar\Support;
final class Translator
{
public const supported = ['en', 'pl'];
private array $catalogs = [];
public function __construct(
private readonly string $langPath,
private string $locale = 'en',
private readonly string $fallback = 'en'
) {
}
public function setLocale(string $locale): void
{
$this->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];
}
}
+37
View File
@@ -0,0 +1,37 @@
<?php
use Bazaar\Core\Config;
function basePath(string $relative = ''): string
{
$root = dirname(__DIR__, 2);
return $relative === '' ? $root : $root . '/' . ltrim($relative, '/');
}
function e(?string $value): string
{
return htmlspecialchars($value ?? '', ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
}
function config(string $key, mixed $default = null): mixed
{
return Config::get($key, $default);
}
function slugify(string $value): string
{
$value = strtolower(trim($value));
$value = preg_replace('/[^a-z0-9]+/', '-', $value);
return trim((string) $value, '-');
}
function formatMoney(int $cents, string $currency = 'USD'): string
{
$symbol = match (strtoupper($currency)) {
'USD' => '$',
'EUR' => 'EUR ',
'PLN' => 'PLN ',
default => strtoupper($currency) . ' ',
};
return $symbol . number_format($cents / 100, 2);
}