Преглед изворни кода

Make it possible to cast key prefix processor instance to string.

Mostly useful with string interpolation when getting the current prefix
associated to a client instance.
Daniele Alessandri пре 12 година
родитељ
комит
83720075f3

+ 3 - 0
CHANGELOG.md

@@ -6,6 +6,9 @@ v0.8.1 (201x-xx-xx)
 - Client options accepting callable objects as factories now pass their actual
   instance to the callable as the second argument.
 
+- `Predis\Command\Processor\KeyPrefixProcessor` can now be directly casted to
+  string to obtain the current prefix, useful with string interpolation.
+
 - __FIX__: a missing use directive in `Predis\Transaction\MultiExecContext`
   caused PHP errors when Redis did not return `+QUEUED` replies to commands
   when inside a MULTI / EXEC context.

+ 8 - 0
lib/Predis/Command/Processor/KeyPrefixProcessor.php

@@ -61,4 +61,12 @@ class KeyPrefixProcessor implements CommandProcessorInterface
             $command->prefixKeys($this->prefix);
         }
     }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function __toString()
+    {
+        return $this->getPrefix();
+    }
 }

+ 11 - 0
tests/Predis/Command/Processor/KeyPrefixProcessorTest.php

@@ -80,4 +80,15 @@ class KeyPrefixProcessorTest extends StandardTestCase
 
         $processor->process($command);
     }
+
+    /**
+     * @group disconnected
+     */
+    public function testInstanceCanBeCastedToString()
+    {
+        $prefix = 'prefix:';
+        $processor = new KeyPrefixProcessor($prefix);
+
+        $this->assertEquals($prefix, (string) $processor);
+    }
 }