ResponseMultiBulkStreamHandler.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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\Connection\ComposableConnectionInterface;
  13. use Predis\Iterator\MultiBulkResponseSimple;
  14. use Predis\Protocol\ProtocolException;
  15. use Predis\Protocol\ResponseHandlerInterface;
  16. /**
  17. * Implements a response handler for iterable multi-bulk replies using the
  18. * standard wire protocol defined by Redis.
  19. *
  20. * @link http://redis.io/topics/protocol
  21. * @author Daniele Alessandri <suppakilla@gmail.com>
  22. */
  23. class ResponseMultiBulkStreamHandler implements ResponseHandlerInterface
  24. {
  25. /**
  26. * Handles a multi-bulk reply returned by Redis in a streamable fashion.
  27. *
  28. * @param ComposableConnectionInterface $connection Connection to Redis.
  29. * @param string $lengthString Number of items in the multi-bulk reply.
  30. * @return MultiBulkResponseSimple
  31. */
  32. public function handle(ComposableConnectionInterface $connection, $lengthString)
  33. {
  34. $length = (int) $lengthString;
  35. if ("$length" != $lengthString) {
  36. Helpers::onCommunicationException(new ProtocolException(
  37. $connection, "Cannot parse '$lengthString' as multi-bulk length"
  38. ));
  39. }
  40. return new MultiBulkResponseSimple($connection, $length);
  41. }
  42. }