Add support API scaffold
This commit is contained in:
118
app/Support/SupportApiRegistry.php
Normal file
118
app/Support/SupportApiRegistry.php
Normal file
@@ -0,0 +1,118 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 : [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user