ResponseMultiBulkHandler.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis\Protocol\Text;
  11. use Predis\Helpers;
  12. use Predis\Protocol\IResponseHandler;
  13. use Predis\Protocol\ProtocolException;
  14. use Predis\Network\IConnectionComposable;
  15. class ResponseMultiBulkHandler implements IResponseHandler
  16. {
  17. public function handle(IConnectionComposable $connection, $lengthString)
  18. {
  19. $length = (int) $lengthString;
  20. if ($length != $lengthString) {
  21. Helpers::onCommunicationException(new ProtocolException(
  22. $connection, "Cannot parse '$length' as data length"
  23. ));
  24. }
  25. if ($length === -1) {
  26. return null;
  27. }
  28. $list = array();
  29. if ($length > 0) {
  30. $handlersCache = array();
  31. $reader = $connection->getProtocol()->getReader();
  32. for ($i = 0; $i < $length; $i++) {
  33. $header = $connection->readLine();
  34. $prefix = $header[0];
  35. if (isset($handlersCache[$prefix])) {
  36. $handler = $handlersCache[$prefix];
  37. }
  38. else {
  39. $handler = $reader->getHandler($prefix);
  40. $handlersCache[$prefix] = $handler;
  41. }
  42. $list[$i] = $handler->handle($connection, substr($header, 1));
  43. }
  44. }
  45. return $list;
  46. }
  47. }