Helpers.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace Predis;
  3. use Predis\Network\IConnection;
  4. use Predis\Network\IConnectionCluster;
  5. class Helpers
  6. {
  7. public static function isCluster(IConnection $connection)
  8. {
  9. return $connection instanceof IConnectionCluster;
  10. }
  11. public static function onCommunicationException(CommunicationException $exception)
  12. {
  13. if ($exception->shouldResetConnection()) {
  14. $connection = $exception->getConnection();
  15. if ($connection->isConnected()) {
  16. $connection->disconnect();
  17. }
  18. }
  19. throw $exception;
  20. }
  21. public static function filterArrayArguments(Array $arguments)
  22. {
  23. if (count($arguments) === 1 && is_array($arguments[0])) {
  24. return $arguments[0];
  25. }
  26. return $arguments;
  27. }
  28. public static function filterVariadicValues(Array $arguments)
  29. {
  30. if (count($arguments) === 2 && is_array($arguments[1])) {
  31. return array_merge(array($arguments[0]), $arguments[1]);
  32. }
  33. return $arguments;
  34. }
  35. public static function getKeyHashablePart($key)
  36. {
  37. $start = strpos($key, '{');
  38. if ($start !== false) {
  39. $end = strpos($key, '}', $start);
  40. if ($end !== false) {
  41. $key = substr($key, ++$start, $end - $start);
  42. }
  43. }
  44. return $key;
  45. }
  46. }