ResponseMultiBulkHandler.php 1.3 KB

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