1: <?php declare(strict_types=1);
2:
3: /**
4: * Clansuite Server Query
5: *
6: * SPDX-FileCopyrightText: 2003-2025 Jens A. Koch
7: * SPDX-License-Identifier: MIT
8: *
9: * For the full copyright and license information, please view
10: * the LICENSE file that was distributed with this source code.
11: */
12:
13: namespace Clansuite\Capture\Protocol;
14:
15: use function is_a;
16: use function is_string;
17: use Clansuite\Capture\UnknownProtocolException;
18:
19: /**
20: * Resolves protocol names to their corresponding class implementations for server querying.
21: */
22: class ProtocolResolver
23: {
24: /**
25: * Constructor.
26: *
27: * @param array<mixed> $protocolMap
28: */
29: public function __construct(private array $protocolMap)
30: {
31: }
32:
33: /**
34: * resolve method.
35: */
36: public function resolve(string $protocol, string $ip, int $port): ProtocolInterface
37: {
38: if ($protocol === 'auto') {
39: $protocol = $this->detectProtocol();
40: }
41:
42: $class = $this->protocolMap[$protocol] ?? throw new UnknownProtocolException($protocol);
43:
44: if (!is_string($class) || !is_a($class, ProtocolInterface::class, true)) {
45: throw new UnknownProtocolException($protocol);
46: }
47:
48: /** @var class-string<ProtocolInterface> $class */
49: return new $class;
50: }
51:
52: private function detectProtocol(): string
53: {
54: // Stub: default to source
55: return 'source';
56: }
57: }
58: