Helpers.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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\Network\IConnection;
  12. use Predis\Network\IConnectionCluster;
  13. class Helpers
  14. {
  15. public static function isCluster(IConnection $connection)
  16. {
  17. return $connection instanceof IConnectionCluster;
  18. }
  19. public static function onCommunicationException(CommunicationException $exception)
  20. {
  21. if ($exception->shouldResetConnection()) {
  22. $connection = $exception->getConnection();
  23. if ($connection->isConnected()) {
  24. $connection->disconnect();
  25. }
  26. }
  27. throw $exception;
  28. }
  29. public static function filterArrayArguments(Array $arguments)
  30. {
  31. if (count($arguments) === 1 && is_array($arguments[0])) {
  32. return $arguments[0];
  33. }
  34. return $arguments;
  35. }
  36. public static function filterVariadicValues(Array $arguments)
  37. {
  38. if (count($arguments) === 2 && is_array($arguments[1])) {
  39. return array_merge(array($arguments[0]), $arguments[1]);
  40. }
  41. return $arguments;
  42. }
  43. public static function getKeyHashablePart($key)
  44. {
  45. $start = strpos($key, '{');
  46. if ($start !== false) {
  47. $end = strpos($key, '}', $start);
  48. if ($end !== false) {
  49. $key = substr($key, ++$start, $end - $start);
  50. }
  51. }
  52. return $key;
  53. }
  54. }