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