Permalink
Browse files

Added getIds method

  • Loading branch information...
1 parent ac9897d commit a317af47247a5a3436f8f38eaed97bb3a1fc68ae @jeremyFreeAgent committed Jan 1, 2013
Showing with 58 additions and 1 deletion.
  1. +31 −1 src/FreeAgent/Bitter/Bitter.php
  2. +27 −0 tests/units/Bitter.php
View
32 src/FreeAgent/Bitter/Bitter.php
@@ -43,7 +43,7 @@ public function getRedisClient()
/**
* Set the Redis client
*
- * @param [type] $newredisClient The Redis client
+ * @param object $newredisClient The Redis client
*/
public function setRedisClient($redisClient)
{
@@ -191,6 +191,36 @@ public function bitDateRange($key, $destKey, DateTime $from, DateTime $to)
}
/**
+ * Returns the ids of an key or event
+ *
+ * @param mixed $key The key or the event
+ * @return array The ids array
+ */
+ public function getIds($key)
+ {
+ $key = $key instanceof EventInterface ? $this->prefixKey . $key->getKey() : $this->prefixTempKey . $key;
+
+ $string = $this->getRedisClient()->get($key);
+
+ $data = $this->bitsetToString($string);
+
+ $ids = array();
+ while (false !== ($pos = strpos($data, '1'))) {
+ $data[$pos] = 0;
+ $ids[] = (int)($pos/8)*8 + abs(7-($pos%8));
+ }
+
+ sort($ids);
+
+ return $ids;
+ }
+
+ protected function bitsetToString($bitset = '')
+ {
+ return bitset_to_string($string);
+ }
+
+ /**
* Removes all Bitter keys
*/
public function removeAll()
View
27 tests/units/Bitter.php
@@ -310,6 +310,33 @@ function() use ($redisClient, $prefixKey, $prefixTempKey) {
/**
* @dataProvider dataProviderTestedClients
*/
+ public function testGetIds($redisClient)
+ {
+ $bitter = new TestedBitter($redisClient, $this->getPrefixKey(), $this->getPrefixTempKey());
+
+ $this->removeAll($redisClient);
+
+ $eventKey = 'liloo_multipass';
+ $dateTime = new DateTime('2012-10-12 15:30:45');
+ $event = new Day($eventKey, $dateTime);
+ $ids = array(1, 13, 404, 2, 12700042, 13003, 99);
+
+ foreach ($ids as $id) {
+ $bitter->mark($eventKey, $id, $dateTime);
+ }
+ sort($ids);
+
+ $this
+ ->array($bitter->getIds($event))
+ ->isIdenticalTo($ids)
+ ;
+
+ $this->removeAll($redisClient);
+ }
+
+ /**
+ * @dataProvider dataProviderTestedClients
+ */
public function testRemoveAll($redisClient)
{
$this->removeAll($redisClient);

0 comments on commit a317af4

Please sign in to comment.