AbstractHashLookup.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Common implementation for Redis-based implementations
  4. */
  5. abstract class Redis_Path_AbstractHashLookup extends Redis_AbstractBackend implements
  6. Redis_Path_HashLookupInterface
  7. {
  8. /**
  9. * @todo document me
  10. *
  11. * @param string $key
  12. * @param string $hkey
  13. * @param string $hvalue
  14. */
  15. abstract protected function saveInHash($key, $hkey, $hvalue);
  16. /**
  17. * @todo document me
  18. *
  19. * @param string $key
  20. * @param string $hkey
  21. * @param string $hvalue
  22. */
  23. abstract protected function deleteInHash($key, $hkey, $hvalue);
  24. /**
  25. * @todo document me
  26. *
  27. * @param string $keyPrefix
  28. * @param string $hkey
  29. * @param string $language
  30. */
  31. abstract protected function lookupInHash($keyPrefix, $hkey, $language = null);
  32. /**
  33. * Normalize value to avoid duplicate or false negatives
  34. *
  35. * @param string $value
  36. *
  37. * @return string
  38. */
  39. private function normalize($value)
  40. {
  41. if (null !== $value) {
  42. return strtolower(trim($value));
  43. }
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function saveAlias($source, $alias, $language = null)
  49. {
  50. $alias = $this->normalize($alias);
  51. $source = $this->normalize($source);
  52. if (null === $language) {
  53. $language = LANGUAGE_NONE;
  54. }
  55. if (!empty($source)) {
  56. $this->saveInHash($this->getKey(array(self::KEY_ALIAS, $language)), $source, $alias);
  57. }
  58. if (!empty($alias)) {
  59. $this->saveInHash($this->getKey(array(self::KEY_SOURCE, $language)), $alias, $source);
  60. }
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function deleteAlias($source, $alias, $language = null)
  66. {
  67. $alias = $this->normalize($alias);
  68. $source = $this->normalize($source);
  69. if (null === $language) {
  70. $language = LANGUAGE_NONE;
  71. }
  72. $this->deleteInHash($this->getKey(array(self::KEY_ALIAS, $language)), $source, $alias);
  73. $this->deleteInHash($this->getKey(array(self::KEY_SOURCE, $language)), $alias, $source);
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function lookupAlias($source, $language = null)
  79. {
  80. $source = $this->normalize($source);
  81. return $this->lookupInHash(self::KEY_ALIAS, $source, $language);
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function lookupSource($alias, $language = null)
  87. {
  88. $alias = $this->normalize($alias);
  89. return $this->lookupInHash(self::KEY_SOURCE, $alias, $language);
  90. }
  91. }