Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263<?php
declare(strict_types=1);
namespace Wotnak\Atproto\Identifier;
/** * Parses and validates AT Protocol DID identifiers. * * @api */final class DidParser {
private const MAX_LENGTH = 2048;
/** * Parses and normalizes a DID. * * @throws \InvalidArgumentException * Thrown when the DID syntax is invalid. */ public function parse(string $value): Did { $value = \trim($value); if (!$this->isValid($value)) { throw new \InvalidArgumentException(\sprintf('Invalid DID "%s".', $value)); }
[, $method, $identifier] = \explode(':', $value, 3); return new Did($value, $method, $identifier, \in_array($method, ['plc', 'web'], TRUE)); }
/** * Returns whether the value is a valid DID. */ public static function isValid(string $value): bool { if ($value === '' || \strlen($value) > self::MAX_LENGTH) { return FALSE; } if (\str_contains($value, '?') || \str_contains($value, '#') || \str_contains($value, '/')) { return FALSE; } if (\preg_match('/%(?![0-9A-Fa-f]{2})/', $value) === 1) { return FALSE; } return \preg_match('/^did:[a-z]+:[a-zA-Z0-9._:%-]*[a-zA-Z0-9._-]$/', $value) === 1; }
/** * Returns whether the DID method can be resolved by this library. */ public function isSupported(string $value): bool { return $this->parse($value)->supported; }
/** * Returns the normalized supplied value. */ public function normalize(string $value): string { return $this->parse($value)->value; }
}