1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace AlecRabbit\Counters; |
4
|
|
|
|
5
|
|
|
use AlecRabbit\Counters\Contracts\ExtendedCounterValuesInterface; |
6
|
|
|
use AlecRabbit\Counters\Core\AbstractCounter; |
7
|
|
|
use AlecRabbit\Counters\Core\Traits\ExtendedCounterFields; |
8
|
|
|
use AlecRabbit\Reports\Contracts\ReportInterface; |
9
|
|
|
use AlecRabbit\Reports\ExtendedCounterReport; |
10
|
|
|
|
11
|
|
|
class ExtendedCounter extends AbstractCounter implements ExtendedCounterValuesInterface |
12
|
|
|
{ |
13
|
|
|
use ExtendedCounterFields; |
14
|
|
|
|
15
|
7 |
|
/** {@inheritdoc} */ |
16
|
|
|
public function __construct(?string $name = null, ?int $step = null, int $initialValue = 0) |
17
|
7 |
|
{ |
18
|
7 |
|
parent::__construct($name, $step, $initialValue); |
19
|
7 |
|
$this->report = $this->createEmptyReport(); |
20
|
|
|
$this->updateExtendedValues(); |
21
|
7 |
|
} |
22
|
|
|
|
23
|
7 |
|
protected function updateExtendedValues(): void |
24
|
7 |
|
{ |
25
|
7 |
|
$this->updateMaxAndMin(); |
26
|
|
|
$this->updateDiff(); |
27
|
7 |
|
} |
28
|
|
|
|
29
|
7 |
|
protected function updateMaxAndMin(): void |
30
|
7 |
|
{ |
31
|
|
|
if ($this->value > $this->max) { |
32
|
7 |
|
$this->max = $this->value; |
33
|
7 |
|
} |
34
|
|
|
if ($this->value < $this->min) { |
35
|
7 |
|
$this->min = $this->value; |
36
|
|
|
} |
37
|
7 |
|
} |
38
|
|
|
|
39
|
7 |
|
protected function updateDiff(): void |
40
|
7 |
|
{ |
41
|
|
|
$this->diff = $this->value - $this->initialValue; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param int $times |
46
|
7 |
|
* @return int |
47
|
|
|
*/ |
48
|
|
|
public function bumpBack(int $times = 1): int |
49
|
7 |
|
{ |
50
|
|
|
return |
51
|
|
|
$this->bump($times, false); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param int $times |
56
|
|
|
* @param bool $forward |
57
|
7 |
|
* @return int |
58
|
|
|
*/ |
59
|
7 |
|
public function bump(int $times = 1, bool $forward = true): int |
60
|
7 |
|
{ |
61
|
7 |
|
$times = $this->assertTimes($times); |
62
|
|
|
if ($this->isNotStarted()) { |
63
|
7 |
|
$this->start(); |
64
|
7 |
|
} |
65
|
7 |
|
$this->path += $times * $this->step; |
66
|
7 |
|
$this->length += $times * $this->step; |
67
|
7 |
|
if ($forward) { |
68
|
|
|
$this->value += $times * $this->step; |
69
|
7 |
|
$this->bumped++; |
70
|
7 |
|
} else { |
71
|
|
|
$this->value -= $times * $this->step; |
72
|
7 |
|
$this->bumpedBack++; |
73
|
|
|
} |
74
|
7 |
|
$this->updateExtendedValues(); |
75
|
|
|
return |
76
|
|
|
$this->value; |
77
|
|
|
} |
78
|
7 |
|
|
79
|
|
|
protected function createEmptyReport(): ReportInterface |
80
|
7 |
|
{ |
81
|
7 |
|
return (new ExtendedCounterReport())->buildOn($this); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
* |
88
|
7 |
|
*/ |
89
|
|
|
protected function updateValues(int $initialValue): void |
90
|
7 |
|
{ |
91
|
7 |
|
$this->value = $this->initialValue = $this->length = $this->max = $this->min = $initialValue; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|