ResponseBulkHandler.php 1.3 KB

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