6
6
use FiveamCode \LaravelNotionApi \Entities \Contracts \Modifiable ;
7
7
use FiveamCode \LaravelNotionApi \Entities \PropertyItems \RichDate ;
8
8
use FiveamCode \LaravelNotionApi \Exceptions \HandlingException ;
9
+ use Illuminate \Support \Arr ;
9
10
10
11
/**
11
12
* Class Date.
@@ -26,6 +27,39 @@ public static function value(?DateTime $start, ?DateTime $end = null): Date
26
27
$ dateProperty = new Date ();
27
28
$ dateProperty ->content = $ richDate ;
28
29
30
+ if ($ richDate ->isRange ()) {
31
+ $ dateProperty ->rawContent = [
32
+ 'date ' => [
33
+ 'start ' => $ start ->format ('Y-m-d ' ),
34
+ 'end ' => $ end ->format ('Y-m-d ' ),
35
+ ],
36
+ ];
37
+ } else {
38
+ $ dateProperty ->rawContent = [
39
+ 'date ' => [
40
+ 'start ' => $ start ->format ('Y-m-d ' ),
41
+ ],
42
+ ];
43
+ }
44
+
45
+ return $ dateProperty ;
46
+ }
47
+
48
+ /**
49
+ * @param $start
50
+ * @param $end
51
+ * @return Date
52
+ */
53
+ public static function valueWithTime (?DateTime $ start , ?DateTime $ end = null ): Date
54
+ {
55
+ $ richDate = new RichDate ();
56
+ $ richDate ->setStart ($ start );
57
+ $ richDate ->setEnd ($ end );
58
+ $ richDate ->setHasTime (true );
59
+
60
+ $ dateProperty = new Date ();
61
+ $ dateProperty ->content = $ richDate ;
62
+
29
63
if ($ richDate ->isRange ()) {
30
64
$ dateProperty ->rawContent = [
31
65
'date ' => [
@@ -57,19 +91,26 @@ protected function fillDate(): void
57
91
{
58
92
$ richDate = new RichDate ();
59
93
60
- if (isset ($ this ->rawContent [ 'start ' ] )) {
94
+ if (Arr:: exists ($ this ->rawContent , 'start ' )) {
61
95
$ startAsIsoString = $ this ->rawContent ['start ' ];
62
96
$ richDate ->setStart (new DateTime ($ startAsIsoString ));
97
+ $ richDate ->setHasTime ($ this ->isIsoTimeString ($ startAsIsoString ));
63
98
}
64
99
65
- if (isset ($ this ->rawContent [ 'end ' ] )) {
100
+ if (Arr:: exists ($ this ->rawContent , 'end ' )) {
66
101
$ endAsIsoString = $ this ->rawContent ['end ' ];
67
102
$ richDate ->setEnd (new DateTime ($ endAsIsoString ));
68
103
}
69
104
70
105
$ this ->content = $ richDate ;
71
106
}
72
107
108
+ // function for checking if ISO datetime string includes time or not
109
+ private function isIsoTimeString (string $ isoTimeDateString ): bool
110
+ {
111
+ return strpos ($ isoTimeDateString , 'T ' ) !== false ;
112
+ }
113
+
73
114
/**
74
115
* @return RichDate
75
116
*/
@@ -91,14 +132,34 @@ public function isRange(): bool
91
132
*/
92
133
public function getStart (): DateTime
93
134
{
135
+ if ($ this ->getContent () === null ) {
136
+ throw new HandlingException ('Invalid content: The content of the Date Property is null. ' );
137
+ }
138
+
94
139
return $ this ->getContent ()->getStart ();
95
140
}
96
141
97
142
/**
98
- * @return DateTime
143
+ * @return ? DateTime
99
144
*/
100
- public function getEnd (): DateTime
145
+ public function getEnd (): ? DateTime
101
146
{
147
+ if ($ this ->getContent () === null ) {
148
+ return null ;
149
+ }
150
+
102
151
return $ this ->getContent ()->getEnd ();
103
152
}
153
+
154
+ /**
155
+ * @return bool
156
+ */
157
+ public function hasTime (): bool
158
+ {
159
+ if ($ this ->getContent () === null ) {
160
+ return false ;
161
+ }
162
+
163
+ return $ this ->getContent ()->hasTime ();
164
+ }
104
165
}
0 commit comments