ResponseMultiBulkHandler.php 1.3 KB

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