From 0350c3c86f9396d07a4c48955c9f63ce3704d001 Mon Sep 17 00:00:00 2001 From: Ladislav Vondracek Date: Tue, 17 Jun 2014 20:42:13 +0200 Subject: [PATCH] Added check valid date. Added constant with version. --- src/MomentPHP/MomentPHP.php | 42 ++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/src/MomentPHP/MomentPHP.php b/src/MomentPHP/MomentPHP.php index 55368dc..21bdc08 100644 --- a/src/MomentPHP/MomentPHP.php +++ b/src/MomentPHP/MomentPHP.php @@ -10,6 +10,8 @@ class MomentPHP { + const VERSION = '1.1'; + const SECONDS = 'seconds'; const MINUTES = 'minutes'; @@ -50,6 +52,7 @@ class MomentPHP * @param \DateTime|MomentPHP|string|int|null $dateTime Instance of classes \DateTime or MomentPHP or string representing the time or timestamp or null for now. * @param array|string|null $format Field formats or simple formatting options, see http://php.net/manual/en/datetime.createfromformat.php * @param \DateTimeZone|string|null $timeZone Supported Timezones, see http://php.net/manual/en/timezones.php + * @throws InvalidArgumentException */ public function __construct($dateTime = null, $format = null, $timeZone = null) { @@ -59,21 +62,36 @@ public function __construct($dateTime = null, $format = null, $timeZone = null) $timeZone = $this->createDateTimeZone($timeZone); - // set dateTime by type - if (!isset($dateTime)) { - $this->dateTime = new \DateTime('now', $timeZone); - } - elseif ($dateTime instanceof \DateTime) { - $this->dateTime = $dateTime; + try { + // set dateTime by type + if (!isset($dateTime)) { + $this->dateTime = new \DateTime('now', $timeZone); + } + elseif ($dateTime instanceof \DateTime) { + $this->dateTime = $dateTime; + } + elseif ($dateTime instanceof MomentPHP) { + $this->dateTime = $dateTime->dateTime; + } + elseif (is_string($dateTime)) { + $this->dateTime = $this->fromFormat($dateTime, $format, $timeZone); + } + elseif (is_int($dateTime)) { + $this->dateTime = $this->fromFormat($dateTime, 'U', $timeZone); + } } - elseif ($dateTime instanceof MomentPHP) { - $this->dateTime = $dateTime->dateTime; + catch (\Exception $e) { + throw new InvalidArgumentException($e->getMessage()); } - elseif (is_string($dateTime)) { - $this->dateTime = $this->fromFormat($dateTime, $format, $timeZone); + + $error = $this->dateTime->getLastErrors(); + if ($error['warning_count'] > 0) { + $msg = 'WARNINGS: ' . join('; ', $error['warnings']); + throw new InvalidArgumentException($msg); } - elseif (is_int($dateTime)) { - $this->dateTime = $this->fromFormat($dateTime, 'U', $timeZone); + if ($error['error_count'] > 0) { + $msg = 'ERRORS: ' . join('; ', $error['errors']); + throw new InvalidArgumentException($msg); } }