add logging; fix caching
[pharext/pharext.org] / app / Github / Storage / Item.php
1 <?php
2
3 namespace app\Github\Storage;
4
5 class Item
6 {
7 private $value;
8 private $time;
9 private $ttl;
10
11 function __construct($value, $ttl = null, $time = null) {
12 $this->value = $value;
13 $this->ttl = $ttl;
14 $this->time = $time ?: time();
15 }
16
17 static function __set_state(array $state) {
18 return new static(
19 isset($state["value"]) ? $state["value"] : null,
20 isset($state["ttl"]) ? $state["ttl"] : null,
21 isset($state["time"]) ? $state["time"] : null
22 );
23 }
24
25 function toArray() {
26 return get_object_vars($this);
27 }
28
29 function getTimestamp() {
30 return $this->time;
31 }
32
33 function setTimestamp($ts = null) {
34 $this->time = $ts ?: time();
35 return $this;
36 }
37
38 function getTTL() {
39 return $this->ttl;
40 }
41
42 function setTTL($ttl = null) {
43 $this->ttl = $ttl;
44 return $this;
45 }
46
47 function getAge($from = null) {
48 return ($from ?: time()) - $this->time;
49 }
50
51 function getLTL($from = null) {
52 return $this->ttl - $this->getAge($from);
53 }
54
55 function getValue() {
56 return $this->value;
57 }
58
59 function setValue($value = null) {
60 $this->value = $value;
61 return $this;
62 }
63 }