$alsoKnownAs * @param list $services * @param array $raw */ public function __construct( public string $id, public array $alsoKnownAs, public array $services, public array $raw, ) {} /** * Parses a DID document from decoded JSON data. * * @param array $document */ public static function fromArray(array $document): ?self { if (!\is_string($document['id'] ?? NULL)) { return NULL; } $alsoKnownAs = []; if (isset($document['alsoKnownAs']) && \is_array($document['alsoKnownAs'])) { foreach ($document['alsoKnownAs'] as $value) { if (!\is_string($value)) { continue; } $alsoKnownAs[] = $value; } } $services = []; if (isset($document['service']) && \is_array($document['service'])) { foreach ($document['service'] as $service) { if (!\is_array($service)) { continue; } /** @var array $service */ $endpoint = AtprotoServiceEndpoint::fromArray($service); if ($endpoint !== NULL) { $services[] = $endpoint; } } } return new self($document['id'], $alsoKnownAs, $services, $document); } /** * Returns whether the document claims the normalized handle. */ public function hasAlsoKnownAsHandle(string $handle): bool { $parser = new HandleParser(); try { $expected = $parser->normalize($handle); } catch (\InvalidArgumentException) { return FALSE; } foreach ($this->alsoKnownAs as $uri) { if (!\str_starts_with($uri, 'at://')) { continue; } $claimed = \substr($uri, \strlen('at://')); if ($claimed !== \trim($claimed) || !$parser->isValid($claimed)) { continue; } // ATProto uses only the first syntactically valid handle claim. return $parser->normalize($claimed) === $expected; } return FALSE; } /** * Returns the document PDS service endpoint when present. */ public function getPdsEndpoint(): ?string { foreach ($this->services as $service) { if ( $service->type === 'AtprotoPersonalDataServer' && self::isPdsServiceId($service->id) && $this->isValidPdsEndpoint($service->serviceEndpoint) ) { return $service->serviceEndpoint; } } return NULL; } private static function isPdsServiceId(string $id): bool { if ($id === '#atproto_pds') { return TRUE; } return ( \str_ends_with($id, '#atproto_pds') && \preg_match('/^[A-Za-z][A-Za-z0-9+.-]*:/D', $id) === 1 && \preg_match('/[\x00-\x20\x7F]/', $id) !== 1 && \preg_match('/%(?![0-9A-Fa-f]{2})/', $id) !== 1 ); } private static function isValidPdsEndpoint(string $endpoint): bool { try { $parts = \parse_url($endpoint); } // ValueError is an expected invalid-input result at this API boundary. // @mago-expect analysis:avoid-catching-error catch (\ValueError) { return FALSE; } return ( \is_array($parts) && \strtolower($parts['scheme'] ?? '') === 'https' && \is_string($parts['host'] ?? NULL) && !isset($parts['user']) && !isset($parts['pass']) && !isset($parts['path']) && !isset($parts['query']) && !isset($parts['fragment']) ); } }