Something went wrong. Try again.
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647<?php
declare(strict_types=1);
namespace Wotnak\Atproto\Identifier;
/** * Parses and validates AT Protocol record keys. * * @api */final class RecordKeyParser {
private const MAX_LENGTH = 512;
/** * Parses and validates an AT Protocol record key. * * @throws \InvalidArgumentException * Thrown when the record key syntax is invalid. */ public function parse(string $value): RecordKey { if (!$this->isValid($value)) { throw new \InvalidArgumentException(\sprintf('Invalid record key "%s".', $value)); } return new RecordKey($value); }
/** * Returns whether the value is a valid record key. */ public static function isValid(string $value): bool { if ($value === '' || \strlen($value) > self::MAX_LENGTH || $value === '.' || $value === '..') { return FALSE; } return \preg_match('/^[A-Za-z0-9.\-_:~]+$/', $value) === 1; }
/** * Returns the normalized supplied value. */ public function normalize(string $value): string { return $this->parse($value)->value; }
}