IntegerResponse.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. $integer = (int) $payload;
  31. return $integer == $payload ? $integer : $payload;
  32. }
  33. if ($payload !== 'nil') {
  34. CommunicationException::handle(new ProtocolException(
  35. $connection, "Cannot parse '$payload' as a valid numeric response [{$connection->getParameters()}]"
  36. ));
  37. }
  38. return;
  39. }
  40. }