Something went wrong. Try again.
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556<?php
declare(strict_types=1);
namespace Wotnak\Atproto\Identifier;
/** * Parses and validates AT Protocol namespaced identifiers. * * @api */final class NsidParser {
private const MAX_LENGTH = 317;
/** * Parses and normalizes an NSID. * * @throws \InvalidArgumentException * Thrown when the NSID syntax is invalid. */ public function parse(string $value): Nsid { if (!$this->isValid($value)) { throw new \InvalidArgumentException(\sprintf('Invalid NSID "%s".', $value)); }
$parts = \explode('.', $value); $name = \array_pop($parts); $authority = \strtolower(\implode('.', $parts)); return new Nsid($authority . '.' . $name, $authority, $name); }
/** * Returns whether the value is a valid NSID. */ public static function isValid(string $value): bool { if ($value === '' || \strlen($value) > self::MAX_LENGTH) { return FALSE; } return ( \preg_match( '/^[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+(\.[a-zA-Z]([a-zA-Z0-9]{0,62})?)$/', $value, ) === 1 ); }
/** * Returns the normalized supplied value. */ public function normalize(string $value): string { return $this->parse($value)->value; }
}