66use FiveamCode \LaravelNotionApi \Entities \Contracts \Modifiable ;
77use FiveamCode \LaravelNotionApi \Entities \PropertyItems \RichDate ;
88use FiveamCode \LaravelNotionApi \Exceptions \HandlingException ;
9+ use Illuminate \Support \Arr ;
910
1011/**
1112 * Class Date.
@@ -26,6 +27,39 @@ public static function value(?DateTime $start, ?DateTime $end = null): Date
2627 $ dateProperty = new Date ();
2728 $ dateProperty ->content = $ richDate ;
2829
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+
2963 if ($ richDate ->isRange ()) {
3064 $ dateProperty ->rawContent = [
3165 'date ' => [
@@ -57,19 +91,26 @@ protected function fillDate(): void
5791 {
5892 $ richDate = new RichDate ();
5993
60- if (isset ($ this ->rawContent [ 'start ' ] )) {
94+ if (Arr:: exists ($ this ->rawContent , 'start ' )) {
6195 $ startAsIsoString = $ this ->rawContent ['start ' ];
6296 $ richDate ->setStart (new DateTime ($ startAsIsoString ));
97+ $ richDate ->setHasTime ($ this ->isIsoTimeString ($ startAsIsoString ));
6398 }
6499
65- if (isset ($ this ->rawContent [ 'end ' ] )) {
100+ if (Arr:: exists ($ this ->rawContent , 'end ' )) {
66101 $ endAsIsoString = $ this ->rawContent ['end ' ];
67102 $ richDate ->setEnd (new DateTime ($ endAsIsoString ));
68103 }
69104
70105 $ this ->content = $ richDate ;
71106 }
72107
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+
73114 /**
74115 * @return RichDate
75116 */
@@ -91,14 +132,34 @@ public function isRange(): bool
91132 */
92133 public function getStart (): DateTime
93134 {
135+ if ($ this ->getContent () === null ) {
136+ throw new HandlingException ('Invalid content: The content of the Date Property is null. ' );
137+ }
138+
94139 return $ this ->getContent ()->getStart ();
95140 }
96141
97142 /**
98- * @return DateTime
143+ * @return ? DateTime
99144 */
100- public function getEnd (): DateTime
145+ public function getEnd (): ? DateTime
101146 {
147+ if ($ this ->getContent () === null ) {
148+ return null ;
149+ }
150+
102151 return $ this ->getContent ()->getEnd ();
103152 }
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+ }
104165}
0 commit comments