|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace MisterIcy\ExcelWriter\Properties\Traits; |
|
5
|
|
|
|
|
6
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\Date; |
|
7
|
|
|
use DateTimeInterface; |
|
8
|
|
|
use TypeError; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Trait DateTimeTrait |
|
12
|
|
|
* @package MisterIcy\ExcelWriter\Properties |
|
13
|
|
|
* |
|
14
|
|
|
* @author Alexandros Koutroulis |
|
15
|
|
|
* @license MIT |
|
16
|
|
|
*/ |
|
17
|
|
|
trait DateTimeTrait |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Allows null dates or other artifacts that are not of type {@see DateTimeInterface}. |
|
21
|
|
|
* @var bool |
|
22
|
|
|
*/ |
|
23
|
|
|
private $nullAllowed = true; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @return bool |
|
27
|
|
|
*/ |
|
28
|
|
|
public function isNullAllowed(): bool |
|
29
|
|
|
{ |
|
30
|
|
|
return $this->nullAllowed; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param bool $nullAllowed |
|
35
|
|
|
* @return self |
|
36
|
|
|
*/ |
|
37
|
|
|
public function setNullAllowed(bool $nullAllowed) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->nullAllowed = $nullAllowed; |
|
40
|
|
|
return $this; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Coverts a DateTime object to Excel Time (float) |
|
45
|
|
|
* |
|
46
|
|
|
* @param DateTimeInterface|null $object |
|
47
|
|
|
* @return null|float |
|
48
|
|
|
* @throws TypeError |
|
49
|
|
|
*/ |
|
50
|
|
|
public function convertDateTimeToExcelTime(?DateTimeInterface $object) |
|
51
|
|
|
{ |
|
52
|
|
|
//This is unrecoverable - we can't just assume Null. |
|
53
|
|
|
//The developer must handle this error. |
|
54
|
|
|
if (!$object instanceof DateTimeInterface) { |
|
55
|
|
|
if ($this->isNullAllowed()) { |
|
56
|
|
|
return null; |
|
57
|
|
|
} |
|
58
|
|
|
throw new TypeError("Invalid date object supplied to datetime converter"); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$hour = intval($object->format('H')); |
|
62
|
|
|
$minute = intval($object->format('i')); |
|
63
|
|
|
$second = intval($object->format('s')); |
|
64
|
|
|
|
|
65
|
|
|
return (Date::formattedPHPToExcel(1900, 1, 1, $hour, $minute, $second) - 1); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Coverts a DateTime object to excel Date (float) |
|
70
|
|
|
* |
|
71
|
|
|
* @param DateTimeInterface|null $object |
|
72
|
|
|
* @return null|float |
|
73
|
|
|
* @throws TypeError |
|
74
|
|
|
*/ |
|
75
|
|
|
public function convertDateTimeToExcelDate(?DateTimeInterface $object) |
|
76
|
|
|
{ |
|
77
|
|
|
//This is unrecoverable - we can't just assume Null. |
|
78
|
|
|
//The developer must handle this error. |
|
79
|
|
|
if (!$object instanceof DateTimeInterface) { |
|
80
|
|
|
if ($this->isNullAllowed()) { |
|
81
|
|
|
return null; |
|
82
|
|
|
} |
|
83
|
|
|
throw new TypeError("Invalid date object supplied to datetime converter"); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$year = intval($object->format('Y')); |
|
87
|
|
|
$month = intval($object->format('m')); |
|
88
|
|
|
$day = intval($object->format('d')); |
|
89
|
|
|
|
|
90
|
|
|
return Date::formattedPHPToExcel($year, $month, $day); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param DateTimeInterface|null $object |
|
95
|
|
|
* @return float|null |
|
96
|
|
|
* @throws TypeError |
|
97
|
|
|
*/ |
|
98
|
|
|
public function convertDateTimeToExcelDateTime(?DateTimeInterface $object) |
|
99
|
|
|
{ |
|
100
|
|
|
$date = $this->convertDateTimeToExcelDate($object); |
|
101
|
|
|
$time = $this->convertDateTimeToExcelTime($object); |
|
102
|
|
|
if (is_null($date) && is_null($time)) { |
|
103
|
|
|
return null; |
|
104
|
|
|
} |
|
105
|
|
|
return $date + $time; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|