-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGCounter.php
47 lines (39 loc) · 961 Bytes
/
GCounter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
namespace CRDT_GCounter;
class GCounter implements GCounterInterface
{
/**
* The Map holding the values for each instance
* @var array
*/
private $count = [];
/**
* @var string
*/
private $selfIdentifier;
public function __construct(string $selfIdentifier, array $identifiers)
{
$this->selfIdentifier = $selfIdentifier;
foreach ($identifiers as $id) {
$this->count[$id] = 0;
}
}
public function increment(): void
{
$this->count[$this->selfIdentifier]++;
}
public function get(): array
{
return $this->count;
}
public function count(): int
{
return array_sum(array_values($this->count));
}
public function join(array $remoteCount): void
{
foreach ($this->count as $key => $value) {
$this->count[$key] = max($this->count[$key], $remoteCount[$key] ?? 0);
}
}
}