Files
fotospiel-app/app/Support/SupportApiRegistry.php
Codex Agent 981df2ee45
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (push) Has been cancelled
tests / ui (push) Has been cancelled
Add support API validation rules
2026-01-28 19:42:28 +01:00

132 lines
2.9 KiB
PHP

<?php
namespace App\Support;
class SupportApiRegistry
{
/**
* @return array<string, array<string, mixed>>
*/
public static function resources(): array
{
return config('support-api.resources', []);
}
/**
* @return array<string, mixed>|null
*/
public static function get(string $resource): ?array
{
$resources = self::resources();
return $resources[$resource] ?? null;
}
public static function validationClass(string $resource, string $action): ?string
{
$config = self::get($resource);
if (! $config) {
return null;
}
$validation = $config['validation'][$action] ?? null;
return is_string($validation) ? $validation : null;
}
/**
* @return array<int, string>
*/
public static function resourceKeys(): array
{
return array_keys(self::resources());
}
public static function resourcePattern(): string
{
$keys = self::resourceKeys();
if ($keys === []) {
return '.*';
}
$escaped = array_map(static fn (string $key): string => preg_quote($key, '/'), $keys);
return implode('|', $escaped);
}
/**
* @return array<int, string>
*/
public static function abilitiesFor(string $resource, string $action): array
{
$config = self::get($resource);
if (! $config) {
return [];
}
$abilities = $config['abilities'][$action] ?? null;
if (is_array($abilities) && $abilities !== []) {
return $abilities;
}
return match ($action) {
'read' => ['support:read'],
'write' => ['support:write'],
'actions' => ['support:actions'],
default => [],
};
}
public static function isReadOnly(string $resource): bool
{
$config = self::get($resource);
return (bool) ($config['read_only'] ?? false);
}
public static function allowsMutation(string $resource, string $action): bool
{
if (self::isReadOnly($resource)) {
return false;
}
$config = self::get($resource);
$mutations = $config['mutations'] ?? null;
if (! is_array($mutations)) {
return true;
}
return (bool) ($mutations[$action] ?? false);
}
/**
* @return array<int, string>
*/
public static function searchFields(string $resource): array
{
$config = self::get($resource);
$fields = $config['search'] ?? [];
return is_array($fields) ? $fields : [];
}
/**
* @return array<int, string>
*/
public static function withRelations(string $resource): array
{
$config = self::get($resource);
$relations = $config['with'] ?? [];
return is_array($relations) ? $relations : [];
}
}