1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace AlecRabbit\Formatters; |
4
|
|
|
|
5
|
|
|
use AlecRabbit\Counters\Contracts\CounterValuesInterface; |
6
|
|
|
use AlecRabbit\Formatters\Contracts\CounterReportFormatterInterface; |
7
|
|
|
use AlecRabbit\Formatters\Contracts\CounterStrings; |
8
|
|
|
use AlecRabbit\Formatters\Core\ReportFormatter; |
9
|
|
|
use AlecRabbit\Formatters\Core\Formattable; |
10
|
|
|
use AlecRabbit\Reports\SimpleCounterReport; |
11
|
|
|
use const AlecRabbit\Traits\Constants\DEFAULT_NAME; |
12
|
|
|
use function AlecRabbit\typeOf; |
13
|
|
|
|
14
|
|
|
class SimpleCounterReportFormatter extends ReportFormatter implements CounterReportFormatterInterface, CounterStrings |
15
|
|
|
{ |
16
|
|
|
/** {@inheritDoc} */ |
17
|
4 |
|
public function format(Formattable $formattable): string |
18
|
|
|
{ |
19
|
4 |
|
if ($formattable instanceof SimpleCounterReport) { |
20
|
3 |
|
if (DEFAULT_NAME === $formattable->getName()) { |
21
|
2 |
|
return $this->simple($formattable); |
22
|
|
|
} |
23
|
1 |
|
return $this->full($formattable); |
24
|
|
|
} |
25
|
1 |
|
throw new \RuntimeException( |
26
|
1 |
|
'Instance of [' . SimpleCounterReport::class . '] expected, [' . typeOf($formattable) . '] given.' |
27
|
|
|
); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param SimpleCounterReport $report |
32
|
|
|
* @param bool $eol |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
2 |
|
protected function simple(SimpleCounterReport $report, bool $eol = true): string |
36
|
|
|
{ |
37
|
|
|
/** @var CounterValuesInterface $report */ |
38
|
|
|
return |
39
|
2 |
|
sprintf( |
40
|
2 |
|
self::COUNTER . ': %s%s', |
41
|
2 |
|
(string)$report->getValue(), |
42
|
2 |
|
$eol ? PHP_EOL : '' |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param SimpleCounterReport $report |
48
|
|
|
* @param bool $eol |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
1 |
|
protected function full(SimpleCounterReport $report, bool $eol = true): string |
52
|
|
|
{ |
53
|
|
|
return |
54
|
1 |
|
sprintf( |
55
|
1 |
|
self::COUNTER . '[%s]: ' . |
56
|
1 |
|
self::VALUE . ': %s, ' . |
57
|
1 |
|
self::STEP . ': %s, ' . |
58
|
1 |
|
self::BUMPED . ': %s%s', |
59
|
1 |
|
$report->getName(), |
60
|
1 |
|
(string)$report->getValue(), |
61
|
1 |
|
(string)$report->getStep(), |
62
|
1 |
|
$this->computeBumped($report), |
63
|
1 |
|
$eol ? PHP_EOL : '' |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param SimpleCounterReport $report |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
1 |
|
private function computeBumped(SimpleCounterReport $report): string |
72
|
|
|
{ |
73
|
|
|
return |
74
|
1 |
|
sprintf( |
75
|
1 |
|
self::FORWARD . '%s ', |
76
|
1 |
|
$report->getBumped() |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|