1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace AlecRabbit\Formatters; |
4
|
|
|
|
5
|
|
|
use AlecRabbit\Accessories\Pretty; |
6
|
|
|
use AlecRabbit\Formatters\Contracts\TimerReportFormatterInterface; |
7
|
|
|
use AlecRabbit\Formatters\Core\AbstractFormatter; |
8
|
|
|
use AlecRabbit\Reports\Core\Formattable; |
9
|
|
|
use AlecRabbit\Reports\TimerReport; |
10
|
|
|
use AlecRabbit\Timers\Contracts\TimerStrings; |
11
|
|
|
use Carbon\CarbonInterval; |
12
|
|
|
use const AlecRabbit\Traits\Constants\DEFAULT_NAME; |
13
|
|
|
|
14
|
|
|
class TimerReportFormatter extends AbstractFormatter implements TimerReportFormatterInterface, TimerStrings |
15
|
|
|
{ |
16
|
|
|
protected const MILLISECONDS_THRESHOLD = 10000; |
17
|
|
|
|
18
|
|
|
/** {@inheritdoc} */ |
19
|
7 |
|
public function format(Formattable $formattable): string |
20
|
|
|
{ |
21
|
7 |
|
if ($formattable instanceof TimerReport) { |
22
|
6 |
|
if (0 === $formattable->getCount() && DEFAULT_NAME === $formattable->getName()) { |
23
|
2 |
|
return $this->simple($formattable); |
24
|
|
|
} |
25
|
4 |
|
return $this->full($formattable); |
26
|
|
|
} |
27
|
|
|
return |
28
|
1 |
|
$this->errorMessage($formattable, TimerReport::class); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param TimerReport $report |
33
|
|
|
* @param bool $eol |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
2 |
|
protected function simple(TimerReport $report, bool $eol = true): string |
37
|
|
|
{ |
38
|
|
|
return |
39
|
2 |
|
sprintf( |
40
|
2 |
|
self::ELAPSED . ': %s %s', |
41
|
2 |
|
$this->refineElapsed($report->getElapsed()), |
42
|
2 |
|
$eol ? PHP_EOL : '' |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param \DateInterval $elapsed |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
6 |
|
protected function refineElapsed(\DateInterval $elapsed): string |
51
|
|
|
{ |
52
|
6 |
|
return $this->formatElapsed($elapsed); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** {@inheritdoc} */ |
56
|
7 |
|
public function formatElapsed(\DateInterval $elapsed): string |
57
|
|
|
{ |
58
|
7 |
|
$c = CarbonInterval::instance($elapsed); |
59
|
7 |
|
if ($c->totalMilliseconds < self::MILLISECONDS_THRESHOLD) { |
60
|
|
|
return |
61
|
7 |
|
Pretty::milliseconds($c->totalMilliseconds); |
62
|
|
|
} |
63
|
|
|
// @codeCoverageIgnoreStart |
64
|
|
|
return (string)$c; |
65
|
|
|
// @codeCoverageIgnoreEnd |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param TimerReport $report |
70
|
|
|
* @param bool $eol |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
4 |
|
protected function full(TimerReport $report, bool $eol = true): string |
74
|
|
|
{ |
75
|
4 |
|
$r = $report; |
76
|
4 |
|
$count = $r->getCount(); |
77
|
|
|
$values = |
78
|
4 |
|
0 < $count ? |
79
|
2 |
|
sprintf( |
80
|
2 |
|
self::AVERAGE . ': %s, ' . |
81
|
2 |
|
self::LAST . ': %s, ' . |
82
|
2 |
|
self::MIN . '(%s): %s, ' . |
83
|
2 |
|
self::MAX . '(%s): %s, ' . |
84
|
2 |
|
self::MARKS . ': %s, ', |
85
|
2 |
|
$this->refineSeconds($r->getAverageValue()), |
86
|
2 |
|
$this->refineSeconds($r->getLastValue()), |
87
|
2 |
|
$r->getMinValueIteration(), |
88
|
2 |
|
$this->refineSeconds($r->getMinValue()), |
89
|
2 |
|
$r->getMaxValueIteration(), |
90
|
2 |
|
$this->refineSeconds($r->getMaxValue()), |
91
|
|
|
$count |
92
|
|
|
) : |
93
|
4 |
|
''; |
94
|
|
|
|
95
|
|
|
return |
96
|
4 |
|
sprintf( |
97
|
4 |
|
self::TIMER . '%s: %s' . |
98
|
4 |
|
self::ELAPSED . ': %s%s', |
99
|
4 |
|
$this->refineName($r->getName()), |
100
|
|
|
$values, |
101
|
4 |
|
$this->refineElapsed($r->getElapsed()), |
102
|
4 |
|
$eol ? PHP_EOL : '' |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
2 |
|
protected function refineSeconds(?float $seconds): string |
107
|
|
|
{ |
108
|
|
|
return |
109
|
2 |
|
$seconds ? Pretty::seconds($seconds) : 'NULL'; |
110
|
|
|
} |
111
|
|
|
|
112
|
4 |
|
protected function refineName(string $name): string |
113
|
|
|
{ |
114
|
4 |
|
if (DEFAULT_NAME === $name) { |
115
|
1 |
|
return ''; |
116
|
|
|
} |
117
|
3 |
|
return '[' . $name . ']'; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|