-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathDifferenceFormatter.php
More file actions
114 lines (105 loc) · 3.58 KB
/
DifferenceFormatter.php
File metadata and controls
114 lines (105 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
declare(strict_types=1);
/**
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @copyright Copyright (c) Brian Nesbitt <brian@nesbot.com>
* @link https://cakephp.org CakePHP(tm) Project
* @license https://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Chronos;
use DateTimeInterface;
/**
* Handles formatting differences in text.
*
* Provides a swappable component for other libraries to leverage.
* when localizing or customizing the difference output.
*
* @internal
*/
class DifferenceFormatter implements DifferenceFormatterInterface
{
/**
* The text translator object
*
* @var \Cake\Chronos\Translator
*/
protected Translator $translate;
/**
* Constructor.
*
* @param \Cake\Chronos\Translator|null $translate The text translator object.
*/
public function __construct(?Translator $translate = null)
{
$this->translate = $translate ?: new Translator();
}
/**
* @inheritDoc
*/
public function diffForHumans(
ChronosDate|DateTimeInterface $first,
ChronosDate|DateTimeInterface|null $second = null,
bool $absolute = false,
): string {
$isNow = $second === null;
if ($second === null) {
if ($first instanceof ChronosDate) {
$second = new ChronosDate(Chronos::now());
} else {
$second = Chronos::now($first->getTimezone());
}
}
assert(
($first instanceof ChronosDate && $second instanceof ChronosDate) ||
($first instanceof DateTimeInterface && $second instanceof DateTimeInterface),
);
$diffInterval = $first->diff($second);
switch (true) {
case $diffInterval->y > 0:
$unit = 'year';
$count = $diffInterval->y;
break;
case $diffInterval->m >= 2:
$unit = 'month';
$count = $diffInterval->m;
break;
case $diffInterval->days >= Chronos::DAYS_PER_WEEK * 3:
$unit = 'week';
$count = (int)($diffInterval->days / Chronos::DAYS_PER_WEEK);
break;
case $diffInterval->d > 0:
$unit = 'day';
$count = $diffInterval->d;
break;
case $diffInterval->h > 0:
$unit = 'hour';
$count = $diffInterval->h;
break;
case $diffInterval->i > 0:
$unit = 'minute';
$count = $diffInterval->i;
break;
default:
$count = $diffInterval->s;
$unit = 'second';
break;
}
$time = $this->translate->plural($unit, $count, ['count' => $count]);
if ($absolute) {
return $time;
}
$isFuture = $diffInterval->invert === 1;
$transId = $isNow ? ($isFuture ? 'from_now' : 'ago') : ($isFuture ? 'after' : 'before');
// Some langs have special pluralization for past and future tense.
$tryKeyExists = $unit . '_' . $transId;
if ($this->translate->exists($tryKeyExists)) {
$time = $this->translate->plural($tryKeyExists, $count, ['count' => $count]);
}
return $this->translate->singular($transId, ['time' => $time]);
}
}