23
23
*/
24
24
class DateTimeHelper extends CarbonImmutable
25
25
{
26
- private Language $ language ;
27
-
28
26
private string $ userTimezone ;
29
27
30
28
private string $ userLanguage ;
@@ -33,13 +31,11 @@ class DateTimeHelper extends CarbonImmutable
33
31
34
32
private string $ userTimeFormat ;
35
33
36
- private Environment $ config ;
37
-
38
34
private readonly string $ dbTimezone ;
39
35
40
36
private readonly string $ dbFormat ;
41
37
42
- private CarbonImmutable $ datetime ;
38
+ private ? CarbonImmutable $ datetime ;
43
39
44
40
/**
45
41
* Constructs a new instance of the class.
@@ -53,19 +49,19 @@ public function __construct($time = null, $tz = null)
53
49
{
54
50
parent ::__construct ($ time , $ tz );
55
51
56
- $ this -> language = app ()->make (Language::class);
57
- $ this -> config = app ()->make (Environment::class);
52
+ $ language = app ()->make (Language::class);
53
+ $ config = app ()->make (Environment::class);
58
54
59
55
// These are read only for a reason
60
56
$ this ->dbFormat = 'Y-m-d H:i:s ' ;
61
57
$ this ->dbTimezone = 'UTC ' ;
62
58
63
59
// Session is set in middleware, unlikely to not be set but just in case set defaults.
64
- $ this ->userTimezone = session ('usersettings.timezone ' ) ?? $ this -> config ->defaultTimezone ;
65
- $ this ->userLanguage = str_replace ('- ' , '_ ' , (session ('usersettings.language ' ) ?? $ this -> config ->language ));
60
+ $ this ->userTimezone = session ('usersettings.timezone ' ) ?? $ config ->defaultTimezone ;
61
+ $ this ->userLanguage = str_replace ('- ' , '_ ' , (session ('usersettings.language ' ) ?? $ config ->language ));
66
62
67
- $ this ->userDateFormat = session ('usersettings.date_format ' ) ?? $ this -> language ->__ ('language.dateformat ' );
68
- $ this ->userTimeFormat = session ('usersettings.time_format ' ) ?? $ this -> language ->__ ('language.timeformat ' );
63
+ $ this ->userDateFormat = session ('usersettings.date_format ' ) ?? $ language ->__ ('language.dateformat ' );
64
+ $ this ->userTimeFormat = session ('usersettings.time_format ' ) ?? $ language ->__ ('language.timeformat ' );
69
65
}
70
66
71
67
/**
@@ -76,51 +72,54 @@ public function __construct($time = null, $tz = null)
76
72
* Defaults to an empty string. Can also be one of start|end to denote start or end time of
77
73
* day
78
74
* @return CarbonImmutable The parsed date and time in user timezone as a CarbonImmutable object.
75
+ *
76
+ * @throws InvalidDateException
79
77
*/
80
78
public function parseUserDateTime (string $ userDate , string $ userTime = '' ): CarbonImmutable
81
79
{
82
80
81
+ // Initialize result variable to null
82
+ $ this ->datetime = null ;
83
+
84
+ // Validate input string
83
85
if (! $ this ->isValidDateString ($ userDate )) {
84
86
throw new InvalidDateException ('The string is not a valid date time string to parse as user datetime string ' , $ userDate );
85
87
}
86
88
87
- // Check if provided date is iso8601 (from API)
88
- try {
89
- $ this ->datetime = CarbonImmutable::createFromFormat (DateTime::ISO8601 , $ userDate );
90
-
91
- return $ this ->datetime ;
92
- } catch (\Exception $ e ) {
93
- // Do nothing
94
- }
95
-
96
- // Also try
97
- try {
98
- $ this ->datetime = CarbonImmutable::createFromFormat (DateTime::ISO8601_EXPANDED , $ userDate );
99
-
100
- return $ this ->datetime ;
101
- } catch (\Exception $ e ) {
102
- // Do nothing
89
+ // Define standard formats to try first
90
+ $ standardFormats = [
91
+ DateTime::ATOM ,
92
+ DateTime::ISO8601 ,
93
+ DateTime::ISO8601_EXPANDED ,
94
+ DateTime::W3C ,
95
+ ];
96
+
97
+ // Try standard formats first
98
+ foreach ($ standardFormats as $ format ) {
99
+ try {
100
+ $ this ->datetime = CarbonImmutable::createFromFormat ($ format , $ userDate );
101
+ if ($ this ->datetime !== false && $ this ->datetime !== null ) {
102
+ return $ this ->datetime ;
103
+ }
104
+ } catch (\Exception $ e ) {
105
+ // Continue to next format
106
+ }
103
107
}
104
108
105
- // Lastly try w3c
106
- try {
107
- $ this ->datetime = CarbonImmutable::createFromFormat (DateTime::W3C , $ userDate );
108
-
109
- return $ this ->datetime ;
110
- } catch (\Exception $ e ) {
111
- // Do nothing
112
- }
109
+ // If no standard format worked, handle user format cases
110
+ $ locale = substr ($ this ->userLanguage , 0 , 2 );
111
+ $ trimmedDate = trim ($ userDate );
113
112
114
- if ($ userTime == 'start ' ) {
115
- $ this ->datetime = CarbonImmutable::createFromLocaleFormat ('! ' .$ this ->userDateFormat , substr ( $ this -> userLanguage , 0 , 2 ), trim ( $ userDate ) , $ this ->userTimezone )
113
+ if ($ userTime === 'start ' ) {
114
+ $ this ->datetime = CarbonImmutable::createFromLocaleFormat ('! ' .$ this ->userDateFormat , $ locale , $ trimmedDate , $ this ->userTimezone )
116
115
->startOfDay ();
117
- } elseif ($ userTime == 'end ' ) {
118
- $ this ->datetime = CarbonImmutable::createFromLocaleFormat ('! ' .$ this ->userDateFormat , substr ( $ this -> userLanguage , 0 , 2 ), trim ( $ userDate ) , $ this ->userTimezone )
116
+ } elseif ($ userTime === 'end ' ) {
117
+ $ this ->datetime = CarbonImmutable::createFromLocaleFormat ('! ' .$ this ->userDateFormat , $ locale , $ trimmedDate , $ this ->userTimezone )
119
118
->endOfDay ();
120
- } elseif ($ userTime == '' ) {
121
- $ this ->datetime = CarbonImmutable::createFromLocaleFormat ('! ' .$ this ->userDateFormat , substr ( $ this -> userLanguage , 0 , 2 ), trim ( $ userDate ) , $ this ->userTimezone );
119
+ } elseif ($ userTime === '' ) {
120
+ $ this ->datetime = CarbonImmutable::createFromLocaleFormat ('! ' .$ this ->userDateFormat , $ locale , $ trimmedDate , $ this ->userTimezone );
122
121
} else {
123
- $ this ->datetime = CarbonImmutable::createFromLocaleFormat ('! ' .$ this ->userDateFormat .' ' .$ this ->userTimeFormat , substr ( $ this -> userLanguage , 0 , 2 ), trim ($ userDate .' ' .$ userTime ), $ this ->userTimezone );
122
+ $ this ->datetime = CarbonImmutable::createFromLocaleFormat ('! ' .$ this ->userDateFormat .' ' .$ this ->userTimeFormat , $ locale , trim ($ trimmedDate .' ' .$ userTime ), $ this ->userTimezone );
124
123
}
125
124
126
125
return $ this ->datetime ;
@@ -190,19 +189,13 @@ public function setCarbonDate(CarbonImmutable|Carbon $date): string|CarbonImmuta
190
189
/**
191
190
* isValidDateString - checks if a given string is a valid date and time string
192
191
*
193
- * @param string|null $dateTimeString The date and time string to be validated
194
- * @return bool Returns true if the string is a valid date and time string , false otherwise
192
+ * @param ? string $dateTimeString The date and time string to be validated
193
+ * @return bool Returns true if the string is a valid string that is worth sending to a parser , false otherwise
195
194
*/
196
195
public function isValidDateString (?string $ dateTimeString ): bool
197
196
{
198
- if (
199
- empty ($ dateTimeString ) === false
200
- && $ dateTimeString != '1969-12-31 00:00:00 '
201
- && $ dateTimeString != '0000-00-00 00:00:00 '
202
- ) {
203
- return true ;
204
- }
205
-
206
- return false ;
197
+ return empty ($ dateTimeString ) === false
198
+ && $ dateTimeString !== '1969-12-31 00:00:00 '
199
+ && $ dateTimeString !== '0000-00-00 00:00:00 ' ;
207
200
}
208
201
}
0 commit comments