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\Formattable; |
9
|
|
|
use AlecRabbit\Formatters\Core\ReportFormatter; |
10
|
|
|
use AlecRabbit\Reports\ExtendedCounterReport; |
11
|
|
|
use const AlecRabbit\Traits\Constants\DEFAULT_NAME; |
12
|
|
|
|
13
|
|
|
class ExtendedCounterReportFormatter extends ReportFormatter implements CounterReportFormatterInterface, CounterStrings |
14
|
|
|
{ |
15
|
|
|
/** {@inheritdoc} */ |
16
|
|
|
public function format(Formattable $formattable): string |
17
|
|
|
{ |
18
|
|
|
if ($formattable instanceof ExtendedCounterReport) { |
19
|
|
|
if (DEFAULT_NAME === $formattable->getName()) { |
20
|
|
|
return $this->simple($formattable); |
21
|
|
|
} |
22
|
|
|
return $this->full($formattable); |
23
|
|
|
} |
24
|
|
|
throw new \RuntimeException( |
25
|
|
|
'Instance of [' . ExtendedCounterReport::class . '] expected, [' . get_class($formattable) . '] given.' |
26
|
|
|
); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param ExtendedCounterReport $report |
31
|
|
|
* @param bool $eol |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
protected function simple(ExtendedCounterReport $report, bool $eol = true): string |
35
|
|
|
{ |
36
|
|
|
/** @var CounterValuesInterface $report */ |
37
|
|
|
return |
38
|
|
|
sprintf( |
39
|
|
|
self::COUNTER . ': %s%s', |
40
|
|
|
(string)$report->getValue(), |
41
|
|
|
$eol ? PHP_EOL : '' |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param ExtendedCounterReport $report |
47
|
|
|
* @param bool $eol |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
protected function full(ExtendedCounterReport $report, bool $eol = true): string |
51
|
|
|
{ |
52
|
|
|
return |
53
|
|
|
sprintf( |
54
|
|
|
self::COUNTER . '--[%s]: ' . |
55
|
|
|
self::VALUE . ': %s, ' . |
56
|
|
|
self::STEP . ': %s, ' . |
57
|
|
|
self::BUMPED . ': %s, ' . |
58
|
|
|
self::PATH . ': %s, ' . |
59
|
|
|
self::LENGTH . ': %s, ' . |
60
|
|
|
self::MAX . ': %s, ' . |
61
|
|
|
self::MIN . ': %s, ' . |
62
|
|
|
self::DIFF . ': %s %s', |
63
|
|
|
$report->getName(), |
64
|
|
|
(string)$report->getValue(), |
65
|
|
|
(string)$report->getStep(), |
66
|
|
|
$this->computeBumped($report), |
67
|
|
|
(string)$report->getPath(), |
68
|
|
|
(string)$report->getLength(), |
69
|
|
|
(string)$report->getMax(), |
70
|
|
|
(string)$report->getMin(), |
71
|
|
|
(string)$report->getDiff(), |
72
|
|
|
$eol ? PHP_EOL : '' |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param ExtendedCounterReport $report |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
private function computeBumped(ExtendedCounterReport $report): string |
81
|
|
|
{ |
82
|
|
|
return |
83
|
|
|
sprintf( |
84
|
|
|
self::FORWARD . '%s ' . self::BACKWARD . '%s', |
85
|
|
|
$report->getBumped(), |
86
|
|
|
$report->getBumpedBack() |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|