1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Arbor\Moment\Formatter; |
6
|
|
|
|
7
|
|
|
use Arbor\Moment\DateRange; |
8
|
|
|
use Arbor\Moment\DateTime; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class DateRangeFormatter |
12
|
|
|
* |
13
|
|
|
* @package Arbor\Moment\Formatter |
14
|
|
|
* @author Igor Vuckovic <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class DateRangeFormatter implements FormatterInterface |
17
|
|
|
{ |
18
|
|
|
/** @var DateTime */ |
19
|
|
|
private $startDate; |
20
|
|
|
|
21
|
|
|
/** @var DateTime */ |
22
|
|
|
private $endDate; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param DateRange $dateRange |
26
|
|
|
*/ |
27
|
17 |
|
public function __construct(DateRange $dateRange) |
28
|
|
|
{ |
29
|
17 |
|
$this->startDate = $dateRange->getStartDate(); |
30
|
17 |
|
$this->endDate = $dateRange->getEndDate(); |
31
|
17 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
10 |
|
public function getShort(string $nullPlaceholder = 'Ongoing') : string |
37
|
|
|
{ |
38
|
10 |
|
if ($this->startDate === null && $this->endDate === null) { |
39
|
2 |
|
return $nullPlaceholder; |
40
|
|
|
} |
41
|
|
|
|
42
|
8 |
|
if ($this->startDate === null) { |
43
|
2 |
|
return $nullPlaceholder . ' - ' . $this->endDate->getShort(); |
44
|
|
|
} |
45
|
|
|
|
46
|
6 |
|
if ($this->endDate === null) { |
47
|
2 |
|
return $this->startDate->getShort() . ' - ' . $nullPlaceholder; |
48
|
|
|
} |
49
|
|
|
|
50
|
4 |
|
if ($this->startDate->getShort() === $this->endDate->getShort()) { |
51
|
2 |
|
return $this->startDate->getShort(); |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
return $this->startDate->getShort() . ' - ' . $this->endDate->getShort(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
21 |
|
public function getLong(string $nullPlaceholder = 'Ongoing') : string |
61
|
|
|
{ |
62
|
21 |
|
if ($this->startDate === null && $this->endDate === null) { |
63
|
3 |
|
return $nullPlaceholder; |
64
|
|
|
} |
65
|
|
|
|
66
|
18 |
|
if ($this->startDate === null) { |
67
|
3 |
|
return $nullPlaceholder . ' - ' . $this->endDate->getLong(); |
68
|
|
|
} |
69
|
|
|
|
70
|
15 |
|
if ($this->endDate === null) { |
71
|
3 |
|
return $this->startDate->getLong() . ' - ' . $nullPlaceholder; |
72
|
|
|
} |
73
|
|
|
|
74
|
12 |
|
if ($this->startDate->getLong() === $this->endDate->getLong()) { |
75
|
6 |
|
return $this->startDate->getLong(); |
76
|
|
|
} |
77
|
|
|
|
78
|
6 |
|
if ($this->startDate->getShort() === $this->endDate->getShort()) { |
79
|
3 |
|
return $this->startDate->getLong() . ' - ' . $this->endDate->format(DateTime::FORMAT_TIME); |
80
|
|
|
} |
81
|
|
|
|
82
|
3 |
|
return $this->startDate->getLong() . ' - ' . $this->endDate->getLong(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|