lthn.io/app/Mod/Names/Resources/NameResource.php
Claude 6af0d7ea73
feat: API Resources for consistent JSON formatting
NameResource transforms alias data with parsed type, capabilities,
and owner_type (registry/community). ClaimResource formats claim
responses. Lookup endpoint uses NameResource, claim uses ClaimResource
with additional message. Follows CorePHP patterns/building-rest-apis.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-04 11:49:19 +01:00

51 lines
1.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Mod\Names\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
/**
* Consistent JSON format for name lookups.
*
* return new NameResource($aliasData);
*/
class NameResource extends JsonResource
{
public function toArray(Request $request): array
{
$address = $this['address'] ?? '';
$comment = $this['comment'] ?? '';
$parsed = $this->parseComment($comment);
$registrarWallets = [
'iTHNHN11yXMeBphpFSuHnDaSJ15QxiSEJXNY59VKbxKq4ype4xAH1PZHd1EKTknkPK9hHTu2G2tBBZzvrcRFaYMF8vWTzFZjGY' => 'registry',
'iTHNUNiuu3VP1yy8xH2y5iQaABKXurdjqZmzFiBiyR4dKG3j6534e9jMriY6SM7PH8NibVwVWW1DWJfQEWnSjS8n3Wgx86pQpY' => 'testnet-registry',
];
return [
'name' => $this['alias'] ?? $this['name'] ?? '',
'fqdn' => ($this['alias'] ?? $this['name'] ?? '') . '.lthn',
'address' => $address,
'owner_type' => isset($registrarWallets[$address]) ? 'registry' : 'community',
'type' => $parsed['type'] ?? 'user',
'capabilities' => ! empty($parsed['cap']) ? explode(',', $parsed['cap']) : [],
'comment' => $comment,
];
}
private function parseComment(string $comment): array
{
$parsed = [];
foreach (explode(';', $comment) as $pair) {
$parts = explode('=', $pair, 2);
if (count($parts) === 2) {
$parsed[$parts[0]] = $parts[1];
}
}
return $parsed;
}
}