|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright (C) 2014-2026 Textalk and contributors. |
| 5 | + * This file is part of Websocket PHP and is free software under the ISC License. |
| 6 | + */ |
| 7 | + |
| 8 | +namespace WebSocket\Message; |
| 9 | + |
| 10 | +use DomainException; |
| 11 | +use InvalidArgumentException; |
| 12 | +use RangeException; |
| 13 | +use WebSocket\Exception\BadOpcodeException; |
| 14 | + |
| 15 | +/** |
| 16 | + * WebSocket\Message\OpcodeRegistry class. |
| 17 | + * Mapping opcode <-> Message implementation class |
| 18 | + */ |
| 19 | +class OpcodeRegistry |
| 20 | +{ |
| 21 | + /** @var array<int<1, 15>, class-string> $map */ |
| 22 | + private array $map = [ |
| 23 | + 1 => Text::class, |
| 24 | + 2 => Binary::class, |
| 25 | + 8 => Close::class, |
| 26 | + 9 => Ping::class, |
| 27 | + 10 => Pong::class, |
| 28 | + ]; |
| 29 | + |
| 30 | + /** |
| 31 | + * @param class-string $classname |
| 32 | + * @return int<1, 15> |
| 33 | + * @throws BadOpcodeException |
| 34 | + */ |
| 35 | + public function getOpcode(string $classname): int |
| 36 | + { |
| 37 | + $opcode = array_search($classname, $this->map); |
| 38 | + if (!is_int($opcode) || $opcode < 1 || $opcode > 15) { |
| 39 | + throw new BadOpcodeException(sprintf( |
| 40 | + 'Opcode must be integer in range 1-15, %s provided', |
| 41 | + json_encode($opcode) |
| 42 | + )); |
| 43 | + } |
| 44 | + return $opcode; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @param int<1, 15> $opcode |
| 49 | + * @return Message |
| 50 | + * @throws BadOpcodeException |
| 51 | + */ |
| 52 | + public function createMessage(int $opcode): Message |
| 53 | + { |
| 54 | + if ($opcode < 1 || $opcode > 15) { |
| 55 | + throw new BadOpcodeException(sprintf( |
| 56 | + 'Opcode must be integer in range 1-15, %s provided', |
| 57 | + json_encode($opcode) |
| 58 | + )); |
| 59 | + } |
| 60 | + $classname = $this->map[$opcode] ?? null; |
| 61 | + if (!is_string($classname) || !class_exists($classname)) { |
| 62 | + throw new BadOpcodeException(sprintf( |
| 63 | + 'Implementation class %s for opcode %s not found', |
| 64 | + json_encode($classname), |
| 65 | + $opcode |
| 66 | + )); |
| 67 | + } |
| 68 | + return new $classname(); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @param int<1, 15> $opcode |
| 73 | + * @param class-string $classname |
| 74 | + * @throws BadOpcodeException |
| 75 | + */ |
| 76 | + public function register(int $opcode, string $classname): void |
| 77 | + { |
| 78 | + if ($opcode < 1 || $opcode > 15) { |
| 79 | + throw new RangeException(sprintf( |
| 80 | + 'Opcode must be integer in range 1-15, %s provided', |
| 81 | + json_encode($opcode) |
| 82 | + )); |
| 83 | + } |
| 84 | + if (empty($classname) || !class_exists($classname)) { |
| 85 | + throw new DomainException(sprintf( |
| 86 | + 'Implementation class %s not found', |
| 87 | + json_encode($classname) |
| 88 | + )); |
| 89 | + } |
| 90 | + $class = new $classname(); |
| 91 | + if (!$class instanceof Message) { |
| 92 | + throw new DomainException(sprintf( |
| 93 | + 'Implementation class %s must extend %s', |
| 94 | + $class::class, |
| 95 | + Message::class, |
| 96 | + )); |
| 97 | + } |
| 98 | + $this->map[$opcode] = $classname; |
| 99 | + } |
| 100 | +} |
0 commit comments