PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/rediska/vendor/rediska/Rediska/Key.php

https://bitbucket.org/sudak/rating
PHP | 317 lines | 136 code | 37 blank | 144 comment | 11 complexity | 93232ea529f2921ad9eb019cd1ec1e55 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. // Require Rediska
  3. require_once dirname(__FILE__) . '/../Rediska.php';
  4. /**
  5. * Rediska basic key
  6. *
  7. * @author Ivan Shumkov
  8. * @package Rediska
  9. * @subpackage Key objects
  10. * @version @package_version@
  11. * @link http://rediska.geometria-lab.net
  12. * @license http://www.opensource.org/licenses/bsd-license.php
  13. */
  14. class Rediska_Key extends Rediska_Key_Abstract implements Countable, ArrayAccess
  15. {
  16. /**
  17. * Set key value
  18. *
  19. * @param $value
  20. * @return boolean
  21. */
  22. public function setValue($value)
  23. {
  24. if ($this->getExpire() !== null && !$this->isExpireTimestamp()) {
  25. $reply = $this->setAndExpire($value, $this->getExpire());
  26. } else {
  27. $reply = $this->_getRediskaOn()->set($this->getName(), $value);
  28. if ($reply && !is_null($this->getExpire())) {
  29. $this->expire($this->getExpire(), $this->isExpireTimestamp());
  30. }
  31. }
  32. return $reply;
  33. }
  34. /**
  35. * Get key value
  36. *
  37. * @return mixed
  38. */
  39. public function getValue()
  40. {
  41. return $this->_getRediskaOn()->get($this->getName());
  42. }
  43. /**
  44. * Increment integer value
  45. *
  46. * @param integer $amount
  47. * @return integer
  48. */
  49. public function increment($amount = 1)
  50. {
  51. return $this->_getRediskaOn()->increment($this->getName(), $amount);
  52. }
  53. /**
  54. * Decrement integer value
  55. *
  56. * @param integer $amount
  57. * @return integer
  58. */
  59. public function decrement($amount = 1)
  60. {
  61. return $this->_getRediskaOn()->decrement($this->getName(), $amount);
  62. }
  63. /**
  64. * Set and expire
  65. *
  66. * @param mixed $value
  67. * @param integer $seconds
  68. * @return boolean
  69. */
  70. public function setAndExpire($value, $seconds)
  71. {
  72. return $this->_getRediskaOn()->setAndExpire($this->getName(), $value, $seconds);
  73. }
  74. /**
  75. * Append value
  76. *
  77. * @param mixed $value Value
  78. * @return integer
  79. */
  80. public function append($value)
  81. {
  82. return $this->_getRediskaOn()->append($this->getName(), $value);
  83. }
  84. /**
  85. * Returns the bit value at offset in the string value stored at key
  86. *
  87. * @param integer $offset Offset
  88. * @param integer $bit Bit (0 or 1)
  89. * @return integer
  90. */
  91. public function setBit($offset, $bit)
  92. {
  93. return $this->_getRediskaOn()->setBit($this->getName(), $offset, $bit);
  94. }
  95. /**
  96. * Returns the bit value at offset in the string value stored at key
  97. *
  98. * @param integer $offset Offset
  99. * @return integer
  100. */
  101. public function getBit($offset)
  102. {
  103. return $this->_getRediskaOn()->getBit($this->getName(), $offset);
  104. }
  105. /**
  106. * Set range
  107. *
  108. * @param string $key Key name
  109. * @param integer $offset Offset
  110. * @param integer $value Value
  111. * @return string
  112. */
  113. public function setRange($offset, $value)
  114. {
  115. return $this->_getRediskaOn()->setRange($this->getName(), $offset, $value);
  116. }
  117. /**
  118. * Get range
  119. *
  120. * @param integer $start Start
  121. * @param integer[optional] $end End. If end is omitted, the substring starting from $start until the end of the string will be returned. For default end of string
  122. * @return mixin
  123. */
  124. public function getRange($start, $end = -1)
  125. {
  126. return $this->_getRediskaOn()->getRange($this->getName(), $start, $end);
  127. }
  128. /**
  129. * Get range
  130. *
  131. * @deprecated
  132. * @param integer $start Start
  133. * @param integer[optional] $end End. If end is omitted, the substring starting from $start until the end of the string will be returned. For default end of string
  134. * @return mixin
  135. */
  136. public function substring($start, $end = -1)
  137. {
  138. return $this->_getRediskaOn()->substring($this->getName(), $start, $end);
  139. }
  140. /**
  141. * Returns the length of the string
  142. *
  143. * @return integer
  144. */
  145. public function getLength()
  146. {
  147. return $this->_getRediskaOn()->getLength($this->getName());
  148. }
  149. /**
  150. * Get value, if value not present set it from chain method
  151. *
  152. * @param mixin[optional] $object Object of chain method
  153. * @param integer[optional] $expire Expire
  154. * @param boolean[optional] $expireIsTimestamp If true $expire argument in seconds, or $expire is timestamp
  155. * @return Rediska_Key_GetOrSetValue
  156. */
  157. public function getOrSetValue($object = null, $expire = null, $expireIsTimestamp = false)
  158. {
  159. return new Rediska_Key_GetOrSetValue($this, $object, $expire, $expireIsTimestamp);
  160. }
  161. /**
  162. * Magic for get value
  163. *
  164. * @return string
  165. */
  166. public function __toString()
  167. {
  168. return (string)$this->getValue();
  169. }
  170. /**
  171. * Implements Countable
  172. */
  173. public function count()
  174. {
  175. return $this->getLength();
  176. }
  177. /**
  178. * Implements ArrayAccess
  179. */
  180. public function offsetExists($offset)
  181. {
  182. return (boolean)$this->getBit($offset);
  183. }
  184. public function offsetGet($offset)
  185. {
  186. return $this->getBit($offset);
  187. }
  188. public function offsetSet($offset, $value)
  189. {
  190. if ($offset === null) {
  191. throw new Rediska_Key_Exception("You must specify offset");
  192. }
  193. return $this->setBit($offset, $value);
  194. }
  195. public function offsetUnset($offset)
  196. {
  197. return $this->setBit($offset, 0);
  198. }
  199. }
  200. /**
  201. * GetOrSetValue helper class
  202. *
  203. * @author Ivan Shumkov
  204. * @package Rediska
  205. * @version @package_version@
  206. * @link http://rediska.geometria-lab.net
  207. * @license http://www.opensource.org/licenses/bsd-license.php
  208. */
  209. class Rediska_Key_GetOrSetValue
  210. {
  211. /**
  212. * Key object
  213. *
  214. * @var Rediska_Key
  215. */
  216. protected $_key;
  217. /**
  218. * Object provider
  219. *
  220. * @var object
  221. */
  222. protected $_object;
  223. /**
  224. * Expire
  225. *
  226. * @var integer
  227. */
  228. protected $_expire;
  229. /**
  230. * Is expire in seconds
  231. *
  232. * @var bool
  233. */
  234. protected $_expireIsTimestamp = false;
  235. /**
  236. * Construct GetOrSetValue provider
  237. *
  238. * @param Rediska_Key $key
  239. * @param object $object Provider object
  240. * @param integer[optional] $expire Expire
  241. * @param boolean[optional] $expireIsTimestamp If true $expire argument in seconds, or $expire is timestamp
  242. */
  243. public function __construct(Rediska_Key $key, $object = null, $expire = null, $expireIsTimestamp = false)
  244. {
  245. $this->_key = $key;
  246. $this->_object = $object;
  247. $this->_expire = $expire;
  248. $this->_expireIsTimestamp = $expireIsTimestamp;
  249. }
  250. public function __call($method, $args)
  251. {
  252. $value = $this->_key->getValue();
  253. if (is_null($value)) {
  254. if (is_null($this->_object)) {
  255. $callback = $method;
  256. } else {
  257. $callback = array($this->_object, $method);
  258. }
  259. $value = call_user_func_array($callback, $args);
  260. $this->_key->setValue($value);
  261. if ($this->_expire !== null) {
  262. $this->_key->expire($this->_expire, $this->_expireIsTimestamp);
  263. }
  264. }
  265. return $value;
  266. }
  267. public function __get($attribute)
  268. {
  269. $value = $this->_key->getValue();
  270. if (is_null($value)) {
  271. $value = $this->_object->{$attribute};
  272. $this->_key->setValue($value);
  273. }
  274. return $value;
  275. }
  276. public function __toString()
  277. {
  278. return (string)$this->_object;
  279. }
  280. }