Helpers.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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;
  11. use Predis\Connection\AggregatedConnectionInterface;
  12. use Predis\Connection\ClusterConnectionInterface;
  13. use Predis\Connection\ConnectionInterface;
  14. /**
  15. * Defines a few helper methods.
  16. *
  17. * @author Daniele Alessandri <suppakilla@gmail.com>
  18. */
  19. class Helpers
  20. {
  21. /**
  22. * Offers a generic and reusable method to handle exceptions generated by
  23. * a connection object.
  24. *
  25. * @param CommunicationException $exception Exception.
  26. */
  27. public static function onCommunicationException(CommunicationException $exception)
  28. {
  29. if ($exception->shouldResetConnection()) {
  30. $connection = $exception->getConnection();
  31. if ($connection->isConnected()) {
  32. $connection->disconnect();
  33. }
  34. }
  35. throw $exception;
  36. }
  37. /**
  38. * Normalizes the arguments array passed to a Redis command.
  39. *
  40. * @param array $arguments Arguments for a command.
  41. * @return array
  42. */
  43. public static function filterArrayArguments(Array $arguments)
  44. {
  45. if (count($arguments) === 1 && is_array($arguments[0])) {
  46. return $arguments[0];
  47. }
  48. return $arguments;
  49. }
  50. /**
  51. * Normalizes the arguments array passed to a variadic Redis command.
  52. *
  53. * @param array $arguments Arguments for a command.
  54. * @return array
  55. */
  56. public static function filterVariadicValues(Array $arguments)
  57. {
  58. if (count($arguments) === 2 && is_array($arguments[1])) {
  59. return array_merge(array($arguments[0]), $arguments[1]);
  60. }
  61. return $arguments;
  62. }
  63. }