MCPcopy Create free account
hub / github.com/MaterializeInc/demos / State

Class State

connection-examples/php/state.php:21–100  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

19}
20
21class State {
22 private $state;
23 private $timestamp;
24 private $valid;
25 private $history;
26
27 public function __construct($collectHistory = false) {
28 $this->state = array();
29 $this->timestamp = 0;
30 $this->valid = true;
31
32 // Define $history as a local variable
33 $this->history = null;
34 if ($collectHistory) {
35 $this->history = array();
36 }
37 }
38
39 public function get_history() {
40 return $this->history;
41 }
42
43 public function get_state() {
44 $list = array();
45
46 foreach ($this->state as $key => $value) {
47 $clone = json_decode($key);
48 $i = 0;
49 while ($i < $value) {
50 $list[] = $clone;
51 $i++;
52 }
53 }
54
55 return $list;
56 }
57
58 public function validate($timestamp) {
59 if (!$this->valid) {
60 throw new Exception("Invalid state.");
61 } elseif ($timestamp < $this->timestamp) {
62 echo "Invalid timestamp.";
63 $this->valid = false;
64 throw new Exception("Update with timestamp ($timestamp) is lower than the last timestamp ({$this->timestamp}). Invalid state.");
65 }
66 }
67
68 public function process(Update $update) {
69 $value = json_encode($update->getValue());
70 $diff = $update->getDiff();
71
72 if (isset($this->state[$value])) {
73 $count = $this->state[$value] + $diff;
74 } else {
75 $count = $diff;
76 }
77
78 if ($count <= 0) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected