$patternComponent) { if ($patternComponent === '*') { return true; } if ( ! isset($hostComponents[$index])) { return false; } if ($hostComponents[$index] !== $patternComponent) { return false; } } return count($patternComponents) === count($hostComponents); } public static function portMatches($pattern, $port) { if ($pattern === "*") { return true; } if ((string)$pattern === "") { return (string)$port === ""; } if (preg_match('/\A\d+\z/', $pattern)) { return (string)$pattern === (string)$port; } if (preg_match('/\A(?P\d+)-(?P\d+)\z/', $pattern, $captured)) { return $captured['from'] <= $port && $port <= $captured['to']; } throw new \InvalidArgumentException("Invalid port pattern: ${pattern}"); } public static function parseOriginPattern($originPattern, $component = -1) { $matched = preg_match( '!\A (?: (?P ([a-z][a-z0-9+\-.]*) ):// )? (?P (?:\*|[\w-]+)(?:\.[\w-]+)* ) (?: :(?P (?: \*|\d+(?:-\d+)? ) ) )? \z!x', $originPattern, $captured ); if ( ! $matched) { throw new \InvalidArgumentException("Invalid origin pattern ${originPattern}"); } $components = [ 'scheme' => $captured['scheme'] ?: null, 'host' => $captured['host'], 'port' => array_key_exists('port', $captured) ? $captured['port'] : null, ]; switch ($component) { case -1: return $components; case PHP_URL_SCHEME: return $components['scheme']; case PHP_URL_HOST: return $components['host']; case PHP_URL_PORT: return $components['port']; } throw new \InvalidArgumentException("Invalid component: ${component}"); } }