IntegerResponse.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 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. * @author Daniele Alessandri <suppakilla@gmail.com>
  20. */
  21. class IntegerResponse implements ResponseHandlerInterface
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function handle(ComposableConnectionInterface $connection, $payload)
  27. {
  28. if (is_numeric($payload)) {
  29. return (int) $payload;
  30. }
  31. if ($payload !== 'nil') {
  32. CommunicationException::handle(new ProtocolException(
  33. $connection, "Cannot parse '$payload' as a valid numeric response."
  34. ));
  35. }
  36. return null;
  37. }
  38. }