ResponseBulkHandler.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. /**
  16. * Implements a response handler for bulk replies using the standard wire
  17. * protocol defined by Redis.
  18. *
  19. * @link http://redis.io/topics/protocol
  20. * @author Daniele Alessandri <suppakilla@gmail.com>
  21. */
  22. class ResponseBulkHandler implements IResponseHandler
  23. {
  24. /**
  25. * Handles a bulk reply returned by Redis.
  26. *
  27. * @param IConnectionComposable $connection Connection to Redis.
  28. * @param string $lengthString Bytes size of the bulk reply.
  29. * @return string
  30. */
  31. public function handle(IConnectionComposable $connection, $lengthString)
  32. {
  33. $length = (int) $lengthString;
  34. if ($length != $lengthString) {
  35. Helpers::onCommunicationException(new ProtocolException(
  36. $connection, "Cannot parse '$length' as data length"
  37. ));
  38. }
  39. if ($length >= 0) {
  40. return substr($connection->readBytes($length + 2), 0, -2);
  41. }
  42. if ($length == -1) {
  43. return null;
  44. }
  45. }
  46. }