Helpers.php 2.5 KB

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