ResponseIntegerHandler.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 integer 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 ResponseIntegerHandler implements IResponseHandler
  23. {
  24. /**
  25. * Handles an integer reply returned by Redis.
  26. *
  27. * @param IConnectionComposable $connection Connection to Redis.
  28. * @param string $number String representation of an integer.
  29. * @return int
  30. */
  31. public function handle(IConnectionComposable $connection, $number)
  32. {
  33. if (is_numeric($number)) {
  34. return (int) $number;
  35. }
  36. if ($number !== 'nil') {
  37. Helpers::onCommunicationException(new ProtocolException(
  38. $connection, "Cannot parse '$number' as numeric response"
  39. ));
  40. }
  41. return null;
  42. }
  43. }