ARedisIterableEntity.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * A base class for iterable redis entities (lists, hashes, sets and sorted sets)
  4. * @author Charles Pick
  5. * @package packages.redis
  6. */
  7. abstract class ARedisIterableEntity extends ARedisEntity implements IteratorAggregate,ArrayAccess,Countable {
  8. /**
  9. * The number of items in the entity
  10. * @var integer
  11. */
  12. protected $_count;
  13. /**
  14. * Holds the data in the entity
  15. * @var array
  16. */
  17. protected $_data;
  18. /**
  19. * Returns an iterator for traversing the items in the set.
  20. * This method is required by the interface IteratorAggregate.
  21. * @return Iterator an iterator for traversing the items in the set.
  22. */
  23. public function getIterator()
  24. {
  25. return new CListIterator($this->getData());
  26. }
  27. /**
  28. * Returns the number of items in the set.
  29. * This method is required by Countable interface.
  30. * @return integer number of items in the set.
  31. */
  32. public function count()
  33. {
  34. return $this->getCount();
  35. }
  36. /**
  37. * Gets a list of items in the set
  38. * @return array the list of items in array
  39. */
  40. public function toArray()
  41. {
  42. return $this->getData();
  43. }
  44. /**
  45. * Gets the number of items in the entity
  46. * @return integer the number of items in the entity
  47. */
  48. abstract public function getCount();
  49. /**
  50. * Gets all the members in the entity
  51. * @param boolean $forceRefresh whether to force a refresh or not
  52. * @return array the members in the entity
  53. */
  54. abstract public function getData($forceRefresh = false);
  55. /**
  56. * Determines whether the item is contained in the entity
  57. * @param mixed $item the item to check for
  58. * @return boolean true if the item exists in the entity, otherwise false
  59. */
  60. public function contains($item) {
  61. return in_array($item, $this->getData());
  62. }
  63. /**
  64. * Removes all the items from the entity
  65. * @return ARedisIterableEntity the current entity
  66. */
  67. public function clear() {
  68. $this->_data = null;
  69. $this->_count = null;
  70. $this->getConnection()->getClient()->delete($this->name);
  71. return $this;
  72. }
  73. }