BulkResponse.php 1.2 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\Handler;
  11. use Predis\CommunicationException;
  12. use Predis\Connection\ComposableConnectionInterface;
  13. use Predis\Protocol\ProtocolException;
  14. /**
  15. * Handler for the bulk response type of 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. * @author Daniele Alessandri <suppakilla@gmail.com>
  20. */
  21. class BulkResponse implements ResponseHandlerInterface
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function handle(ComposableConnectionInterface $connection, $payload)
  27. {
  28. $length = (int) $payload;
  29. if ("$length" !== $payload) {
  30. CommunicationException::handle(new ProtocolException(
  31. $connection, "Cannot parse '$payload' as the length of the bulk response"
  32. ));
  33. }
  34. if ($length >= 0) {
  35. return substr($connection->readBytes($length + 2), 0, -2);
  36. }
  37. if ($length == -1) {
  38. return null;
  39. }
  40. }
  41. }