1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace AlecRabbit\Reports\Core; |
4
|
|
|
|
5
|
|
|
use AlecRabbit\Counters\Core\AbstractCounter; |
6
|
|
|
use AlecRabbit\Counters\SimpleCounter; |
7
|
|
|
use AlecRabbit\Counters\Core\Traits\SimpleCounterFields; |
8
|
|
|
use AlecRabbit\Formatters\Contracts\CounterReportFormatterInterface; |
9
|
|
|
use AlecRabbit\Formatters\SimpleCounterReportFormatter; |
10
|
|
|
use AlecRabbit\Reports\Contracts\ReportableInterface; |
11
|
|
|
use AlecRabbit\Reports\Contracts\ReportInterface; |
12
|
|
|
|
13
|
|
|
abstract class AbstractCounterReport extends AbstractReport |
14
|
|
|
{ |
15
|
|
|
use SimpleCounterFields; |
16
|
|
|
|
17
|
|
|
/** @var CounterReportFormatterInterface */ |
18
|
|
|
protected static $reportFormatter; |
19
|
|
|
|
20
|
|
|
public static function setFormatter(CounterReportFormatterInterface $formatter): void |
21
|
|
|
{ |
22
|
|
|
static::$reportFormatter = $formatter; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
protected static function getFormatter(): CounterReportFormatterInterface |
26
|
|
|
{ |
27
|
|
|
if (!static::$reportFormatter instanceof CounterReportFormatterInterface) { |
|
|
|
|
28
|
|
|
static::$reportFormatter = new SimpleCounterReportFormatter(); |
29
|
|
|
} |
30
|
|
|
return static::$reportFormatter; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param ReportableInterface $counter |
35
|
|
|
* @return ReportInterface |
36
|
|
|
* @throws \RuntimeException |
37
|
|
|
* @throws \Exception |
38
|
|
|
*/ |
39
|
12 |
|
public function buildOn(ReportableInterface $counter): ReportInterface |
40
|
|
|
{ |
41
|
12 |
|
if ($counter instanceof SimpleCounter) { |
42
|
11 |
|
$this->name = $counter->getName(); |
43
|
11 |
|
$this->value = $counter->getValue(); |
44
|
11 |
|
$this->step = $counter->getStep(); |
45
|
11 |
|
$this->started = $counter->isStarted(); |
46
|
11 |
|
$this->initialValue = $counter->getInitialValue(); |
47
|
11 |
|
$this->bumped = $counter->getBumped(); |
48
|
11 |
|
return $this; |
49
|
|
|
} |
50
|
1 |
|
throw new \InvalidArgumentException( |
51
|
1 |
|
AbstractCounter::class . ' expected, ' . get_class($counter) . ' given.' |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|