From 267c260a0f3083ff4830046db1db77f4dc4e1883 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Thu, 13 Apr 2017 12:17:57 +0000 Subject: [PATCH] Update convert git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@3522 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- Tests/convertTest.php | 4 ++-- convert.php | 16 +++++++++++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/Tests/convertTest.php b/Tests/convertTest.php index adcfd4c..0c1e82a 100644 --- a/Tests/convertTest.php +++ b/Tests/convertTest.php @@ -14,12 +14,12 @@ class test_convert extends PHPUnit_Framework_TestCase public function test_convertDate2 () { $this->setExpectedException ("Exception"); - $res = \convert::convertDate ("2017-13-33", "Y-m-d", "d/m/Y", true); + $res = \convert::convertDate ("2017-13-33", "Y-m-d", "d/m/Y"); } public function test_convertDate3 () { - $res = \convert::convertDate ("2017-13-33", "Y-m-d", "d/m/Y"); + $res = \convert::convertDate ("2017-13-33", "Y-m-d", "d/m/Y", false); $this->assertSame ($res, "2017-13-33"); } } diff --git a/convert.php b/convert.php index f01f8f6..4853b90 100644 --- a/convert.php +++ b/convert.php @@ -8,8 +8,8 @@ class convert { /** Convert Date received in one format to another. - * If the provided string is not corresponding to the format, don't change - * anything. + * If the provided string is not corresponding to the format, generate an + * exception or return the original string * Format used http://php.net/manual/en/datetime.createfromformat.php * Do not accept the locale ! The language of the dates is always in english * @param string $inputDate The date to modify @@ -20,12 +20,18 @@ class convert * @return string */ public static function convertDate ($inputDate, $inputFormat, $outputFormat, - $exception = false) + $exception = true) { + if (! is_string ($inputDate)) + throw new \Exception ("The date to convert is not a string", 500); + if (! is_string ($inputFormat)) + throw new \Exception ("The convert input format is not a string", 500); + if (! is_string ($outputFormat)) + throw new \Exception ("The convert output format is not a string", 500); $date = \DateTime::CreateFromFormat ($inputFormat, $inputDate); if ($date === false) { - if ($exception !== false) + if ($exception === true) throw new \Exception ( "Invalid date provided or not matching the format", 500); return $inputDate; @@ -33,7 +39,7 @@ class convert $errors = $date->getLastErrors(); if ($errors["warning_count"] > 0 || $errors["error_count"] > 0) { - if ($exception !== false) + if ($exception === true) throw new \Exception ( "Invalid date provided or not matching the format", 500); return $inputDate;