BulkResponse.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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\Handler;
  11. use Predis\CommunicationException;
  12. use Predis\Connection\CompositeConnectionInterface;
  13. use Predis\Protocol\ProtocolException;
  14. /**
  15. * Handler for the bulk response type in the standard Redis wire protocol.
  16. * It translates the payload to a string or a NULL.
  17. *
  18. * @link http://redis.io/topics/protocol
  19. *
  20. * @author Daniele Alessandri <suppakilla@gmail.com>
  21. */
  22. class BulkResponse implements ResponseHandlerInterface
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function handle(CompositeConnectionInterface $connection, $payload)
  28. {
  29. $length = (int) $payload;
  30. if ("$length" !== $payload) {
  31. CommunicationException::handle(new ProtocolException(
  32. $connection, "Cannot parse '$payload' as a valid length for a bulk response."
  33. ));
  34. }
  35. if ($length >= 0) {
  36. return substr($connection->readBuffer($length + 2), 0, -2);
  37. }
  38. if ($length == -1) {
  39. return;
  40. }
  41. CommunicationException::handle(new ProtocolException(
  42. $connection, "Value '$payload' is not a valid length for a bulk response."
  43. ));
  44. return;
  45. }
  46. }