|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Hyde\Support; |
|
6
|
|
|
|
|
7
|
|
|
use Closure; |
|
8
|
|
|
use Hyde\Facades\Filesystem; |
|
9
|
|
|
use function floor; |
|
10
|
|
|
use function sprintf; |
|
11
|
|
|
use function str_word_count; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Calculate the estimated reading time for a text. |
|
15
|
|
|
* |
|
16
|
|
|
* @see \Hyde\Framework\Testing\Feature\ReadingTimeTest |
|
17
|
|
|
*/ |
|
18
|
|
|
class ReadingTime |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var int How many words per minute is read. Inversely proportional. Increase for a shorter reading time. */ |
|
21
|
|
|
protected const WORDS_PER_MINUTE = 240; |
|
22
|
|
|
|
|
23
|
|
|
/** @var string The text to calculate the reading time for. */ |
|
24
|
|
|
protected readonly string $text; |
|
25
|
|
|
|
|
26
|
|
|
/** @var int The number of words in the text. */ |
|
27
|
|
|
protected int $wordCount; |
|
28
|
|
|
|
|
29
|
|
|
/** @var int The number of seconds it takes to read the text. */ |
|
30
|
|
|
protected int $seconds; |
|
31
|
|
|
|
|
32
|
|
|
public static function fromString(string $text): static |
|
33
|
|
|
{ |
|
34
|
|
|
return new static($text); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public static function fromFile(string $path): static |
|
38
|
|
|
{ |
|
39
|
|
|
return static::fromString(Filesystem::getContents($path)); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function __construct(string $text) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->text = $text; |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
$this->generate(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function getWordCount(): int |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->wordCount; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function getMinutes(): int |
|
55
|
|
|
{ |
|
56
|
|
|
return (int) floor($this->getMinutesAsFloat()); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function getSeconds(): int |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->seconds; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
protected function getMinutesAsFloat(): float |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->seconds / 60; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function getSecondsOver(): int |
|
70
|
|
|
{ |
|
71
|
|
|
return (int) round(($this->getMinutesAsFloat() - $this->getMinutes()) * 60); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function getFormatted(string $format = '%dmin, %dsec'): string |
|
75
|
|
|
{ |
|
76
|
|
|
return sprintf($format, $this->getMinutes(), $this->getSecondsOver()); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** @param \Closure(int, int): string $closure The closure will receive the minutes and seconds as integers and should return a string. */ |
|
80
|
|
|
public function formatUsingClosure(Closure $closure): string |
|
81
|
|
|
{ |
|
82
|
|
|
return $closure($this->getMinutes(), $this->getSecondsOver()); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
protected function generate(): void |
|
86
|
|
|
{ |
|
87
|
|
|
$wordCount = str_word_count($this->text); |
|
88
|
|
|
|
|
89
|
|
|
$minutes = $wordCount / static::WORDS_PER_MINUTE; |
|
90
|
|
|
$seconds = (int) floor($minutes * 60); |
|
91
|
|
|
|
|
92
|
|
|
$this->wordCount = $wordCount; |
|
|
|
|
|
|
93
|
|
|
$this->seconds = $seconds; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|