Factory.php 879 B

1234567891011121314151617181920212223242526272829303132
  1. <?php
  2. /**
  3. * Copyright (C) 2014-2022 Textalk/Abicart and contributors.
  4. *
  5. * This file is part of Websocket PHP and is free software under the ISC License.
  6. * License text: https://raw.githubusercontent.com/Textalk/websocket-php/master/COPYING
  7. */
  8. namespace WebSocket\Message;
  9. use WebSocket\BadOpcodeException;
  10. class Factory
  11. {
  12. public function create(string $opcode, string $payload = ''): Message
  13. {
  14. switch ($opcode) {
  15. case 'text':
  16. return new Text($payload);
  17. case 'binary':
  18. return new Binary($payload);
  19. case 'ping':
  20. return new Ping($payload);
  21. case 'pong':
  22. return new Pong($payload);
  23. case 'close':
  24. return new Close($payload);
  25. }
  26. throw new BadOpcodeException("Invalid opcode '{$opcode}' provided");
  27. }
  28. }