ResponseMultiBulkHandler.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace Predis\Protocols\Text;
  3. use Predis\Helpers;
  4. use Predis\ProtocolException;
  5. use Predis\Protocols\IResponseHandler;
  6. use Predis\Network\IConnectionComposable;
  7. class ResponseMultiBulkHandler implements IResponseHandler {
  8. public function handle(IConnectionComposable $connection, $lengthString) {
  9. $length = (int) $lengthString;
  10. if ($length != $lengthString) {
  11. Helpers::onCommunicationException(new ProtocolException(
  12. $connection, "Cannot parse '$length' as data length"
  13. ));
  14. }
  15. if ($length === -1) {
  16. return null;
  17. }
  18. $list = array();
  19. if ($length > 0) {
  20. $handlersCache = array();
  21. $reader = $connection->getProtocol()->getReader();
  22. for ($i = 0; $i < $length; $i++) {
  23. $header = $connection->readLine();
  24. $prefix = $header[0];
  25. if (isset($handlersCache[$prefix])) {
  26. $handler = $handlersCache[$prefix];
  27. }
  28. else {
  29. $handler = $reader->getHandler($prefix);
  30. $handlersCache[$prefix] = $handler;
  31. }
  32. $list[$i] = $handler->handle($connection, substr($header, 1));
  33. }
  34. }
  35. return $list;
  36. }
  37. }