lthn.io/app/Mod/Names/Resources/NameResource.php

52 lines
1.5 KiB
PHP
Raw Permalink Normal View History

<?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;
}
}