Skip to content

Commit

Permalink
Make intl extension optional
Browse files Browse the repository at this point in the history
  • Loading branch information
gharlan committed Feb 5, 2017
1 parent a99ddee commit cb3adc8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
11 changes: 9 additions & 2 deletions src/AbstractDateTime.php
Expand Up @@ -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());
}
Expand Down
6 changes: 5 additions & 1 deletion src/Date.php
Expand Up @@ -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);
}

Expand Down
14 changes: 11 additions & 3 deletions src/DateTime.php
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/DateTimeInterface.php
Expand Up @@ -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
Expand Down

0 comments on commit cb3adc8

Please sign in to comment.