IntegerResponse.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 integer response type in the standard Redis wire protocol.
  16. * It translates the payload an integer or NULL.
  17. *
  18. * @link http://redis.io/topics/protocol
  19. *
  20. * @author Daniele Alessandri <suppakilla@gmail.com>
  21. */
  22. class IntegerResponse implements ResponseHandlerInterface
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function handle(CompositeConnectionInterface $connection, $payload)
  28. {
  29. if (is_numeric($payload)) {
  30. return (int) $payload;
  31. }
  32. if ($payload !== 'nil') {
  33. CommunicationException::handle(new ProtocolException(
  34. $connection, "Cannot parse '$payload' as a valid numeric response."
  35. ));
  36. }
  37. return;
  38. }
  39. }