Skip to content

Commit 4a7b69d

Browse files
committed
Deprecates legacy date conversion methods
Replaces legacy timestamp and date conversion methods with new, standardized alternatives (`fromTimestamp`, `fromDateTime`, `toDateTime`). Marks old methods as deprecated to encourage migration to the updated API. Improves consistency and clarity in handling date and datetime conversions. Updates related deprecation notices for better guidance.
1 parent 5d39f71 commit 4a7b69d

3 files changed

Lines changed: 54 additions & 3 deletions

File tree

src/AbraFlexi/Date.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,37 @@ public function __toString(): string
7676
/**
7777
* Convert Timestamp to AbraFlexi Date format.
7878
*
79+
* @deprecated message: Use Date::fromTimestamp() instead
80+
*
7981
* @param int $timpestamp
8082
*
8183
* @return \AbraFlexi\Date or NULL
8284
*/
8385
public static function timestampToFlexiDate($timpestamp = null): self
86+
{
87+
return self::fromTimestamp($timpestamp);
88+
}
89+
90+
public static function fromTimestamp(int $timpestamp): self
8491
{
8592
$flexiDate = new self();
8693

87-
if (null !== $timpestamp) {
88-
$flexiDate->setTimestamp($timpestamp);
89-
}
94+
$flexiDate->setTimestamp($timpestamp);
9095

9196
return $flexiDate;
9297
}
98+
99+
public static function fromDateTime(\DateTime $when): self
100+
{
101+
return new self($when->format(self::$format));
102+
}
103+
104+
public function toDateTime(): \DateTime
105+
{
106+
$dateTime = new \DateTime();
107+
$dateTime->setTimestamp($this->getTimestamp());
108+
$dateTime->setTimezone(new \DateTimeZone(date_default_timezone_get()));
109+
110+
return $dateTime;
111+
}
93112
}

src/AbraFlexi/DateTime.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ public function __toString(): string
7070
/**
7171
* Convert Timestamp to Flexi DateTime format.
7272
*
73+
* @deprecated message: Use DateTime::fromTimestamp() instead
74+
*
7375
* @param int $timpestamp
7476
*
7577
* @return string AbraFlexi DateTime or NULL
@@ -98,4 +100,26 @@ public function setFormat(string $format)
98100

99101
return $this;
100102
}
103+
104+
public static function fromDateTime(\DateTime $when): self
105+
{
106+
return new self($when->format(self::$format));
107+
}
108+
109+
public static function fromTimestamp(int $timpestamp): self
110+
{
111+
$flexiDate = new self();
112+
113+
$flexiDate->setTimestamp($timpestamp);
114+
115+
return $flexiDate;
116+
}
117+
public function toDateTime(): \DateTime
118+
{
119+
$dateTime = new \DateTime();
120+
$dateTime->setTimestamp($this->getTimestamp());
121+
$dateTime->setTimezone(new \DateTimeZone(date_default_timezone_get()));
122+
123+
return $dateTime;
124+
}
101125
}

src/AbraFlexi/Functions.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ public static function flexiUrl(array $data, $joiner = 'and', $defop = 'eq'): st
225225

226226
/**
227227
* PHP Date object to AbraFlexi date format.
228+
*
229+
* @deprecated Use Date::dateToFlexiDate() instead
228230
*/
229231
public static function dateToFlexiDate(\DateTime $date)
230232
{
@@ -233,6 +235,8 @@ public static function dateToFlexiDate(\DateTime $date)
233235

234236
/**
235237
* PHP Date object to AbraFlexi date format.
238+
*
239+
* @abstracted Use DateTime::dateToFlexiDateTime() instead
236240
*/
237241
public static function dateToFlexiDateTime(\DateTime $dateTime)
238242
{
@@ -242,6 +246,8 @@ public static function dateToFlexiDateTime(\DateTime $dateTime)
242246
/**
243247
* AbraFlexi date to PHP DateTime conversion.
244248
*
249+
* @deprecated Use Date::flexiDateToDateTime() instead
250+
*
245251
* @param string $flexidate 2017-05-26 or 2017-05-26Z or 2017-05-26+02:00
246252
*
247253
* @return \DateTime|false
@@ -262,6 +268,8 @@ public static function flexiDateToDateTime(string $flexidate)
262268
/**
263269
* AbraFlexi dateTime to PHP DateTime conversion.
264270
*
271+
* @deprecated Use DateTime::flexiDateTimeToDateTime() instead
272+
*
265273
* @param string $flexidatetime 2017-09-26T10:00:53.755+02:00 or older 2017-05-19T00:00:00+02:00
266274
*
267275
* @return \DateTime|false

0 commit comments

Comments
 (0)