$template) { self::$templateMap[$name] = new PathTemplate($template); } } private static function getPathTemplate(string $key) { // TODO: Add nullable return type reference once PHP 7.1 is minimum. if (is_null(self::$templateMap)) { self::registerPathTemplates(); } return self::$templateMap[$key] ?? null; } private static function parseFormattedName(string $formattedName, ?string $template = null): array { if (is_null(self::$templateMap)) { self::registerPathTemplates(); } if ($template) { if (!isset(self::$templateMap[$template])) { throw new ValidationException("Template name $template does not exist"); } return self::$templateMap[$template]->match($formattedName); } foreach (self::$templateMap as $templateName => $pathTemplate) { try { return $pathTemplate->match($formattedName); } catch (ValidationException $ex) { // Swallow the exception to continue trying other path templates } } throw new ValidationException("Input did not match any known format. Input: $formattedName"); } }