From cb3adc81c91b74d161960f33cf3f5b2617afff71 Mon Sep 17 00:00:00 2001 From: Gregor Harlan Date: Sun, 5 Feb 2017 22:13:36 +0100 Subject: [PATCH] Make intl extension optional --- src/AbstractDateTime.php | 11 +++++++++-- src/Date.php | 6 +++++- src/DateTime.php | 14 +++++++++++--- src/DateTimeInterface.php | 2 +- 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/AbstractDateTime.php b/src/AbstractDateTime.php index 5f4b11c..f25386d 100644 --- a/src/AbstractDateTime.php +++ b/src/AbstractDateTime.php @@ -100,9 +100,16 @@ public function formatLocalized(string $format): string return strftime($format, $this->getTimestamp()); } - public function formatIntl(int $format = \IntlDateFormatter::LONG, int $timeFormat = null): string + public function formatIntl(int $format = null, int $timeFormat = null): string { - $formatter = new \IntlDateFormatter(\Locale::getDefault(), $format, $timeFormat ?? $format); + if (!class_exists(\IntlDateFormatter::class)) { + throw new \Exception(sprintf('%s can not be used without the intl extension.', __METHOD__)); + } + + $format = $format ?? \IntlDateFormatter::LONG; + $timeFormat = $timeFormat ?? $format; + + $formatter = new \IntlDateFormatter(\Locale::getDefault(), $format, $timeFormat); return $formatter->format($this->getTimestamp()); } diff --git a/src/Date.php b/src/Date.php index 69b77bd..7734544 100644 --- a/src/Date.php +++ b/src/Date.php @@ -51,8 +51,12 @@ public function formatIso(): string return $this->format('Y-m-d'); } - public function formatIntl(int $format = \IntlDateFormatter::LONG, int $timeFormat = null): string + public function formatIntl(int $format = null, int $timeFormat = null): string { + if (!class_exists(\IntlDateFormatter::class)) { + throw new \Exception(sprintf('%s can not be used without the intl extension.', __METHOD__)); + } + return parent::formatIntl($format, $timeFormat ?? \IntlDateFormatter::NONE); } diff --git a/src/DateTime.php b/src/DateTime.php index 278447f..732bb9b 100644 --- a/src/DateTime.php +++ b/src/DateTime.php @@ -51,14 +51,22 @@ public function formatIsoTime(): string return $this->format('H:i:s'); } - public function formatIntlDate(int $format = \IntlDateFormatter::LONG): string + public function formatIntlDate(int $format = null): string { + if (!class_exists(\IntlDateFormatter::class)) { + throw new \Exception(sprintf('%s can not be used without the intl extension.', __METHOD__)); + } + return static::formatIntl($format, \IntlDateFormatter::NONE); } - public function formatIntlTime(int $format = \IntlDateFormatter::LONG): string + public function formatIntlTime(int $format = null): string { - return static::formatIntl(\IntlDateFormatter::NONE, $format); + if (!class_exists(\IntlDateFormatter::class)) { + throw new \Exception(sprintf('%s can not be used without the intl extension.', __METHOD__)); + } + + return static::formatIntl(\IntlDateFormatter::NONE, $format ?? \IntlDateFormatter::LONG); } public function toDate(): Date diff --git a/src/DateTimeInterface.php b/src/DateTimeInterface.php index 4ca900a..f2ad9f3 100644 --- a/src/DateTimeInterface.php +++ b/src/DateTimeInterface.php @@ -38,7 +38,7 @@ public function formatLocalized(string $format): string; * * @return string */ - public function formatIntl(int $format = \IntlDateFormatter::LONG, int $timeFormat = null): string; + public function formatIntl(int $format = null, int $timeFormat = null): string; /** * @return \DateTime