Update convert
git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@3522 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
@@ -14,12 +14,12 @@ class test_convert extends PHPUnit_Framework_TestCase
|
|||||||
public function test_convertDate2 ()
|
public function test_convertDate2 ()
|
||||||
{
|
{
|
||||||
$this->setExpectedException ("Exception");
|
$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 ()
|
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");
|
$this->assertSame ($res, "2017-13-33");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
16
convert.php
16
convert.php
@@ -8,8 +8,8 @@
|
|||||||
class convert
|
class convert
|
||||||
{
|
{
|
||||||
/** Convert Date received in one format to another.
|
/** Convert Date received in one format to another.
|
||||||
* If the provided string is not corresponding to the format, don't change
|
* If the provided string is not corresponding to the format, generate an
|
||||||
* anything.
|
* exception or return the original string
|
||||||
* Format used http://php.net/manual/en/datetime.createfromformat.php
|
* Format used http://php.net/manual/en/datetime.createfromformat.php
|
||||||
* Do not accept the locale ! The language of the dates is always in english
|
* Do not accept the locale ! The language of the dates is always in english
|
||||||
* @param string $inputDate The date to modify
|
* @param string $inputDate The date to modify
|
||||||
@@ -20,12 +20,18 @@ class convert
|
|||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function convertDate ($inputDate, $inputFormat, $outputFormat,
|
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);
|
$date = \DateTime::CreateFromFormat ($inputFormat, $inputDate);
|
||||||
if ($date === false)
|
if ($date === false)
|
||||||
{
|
{
|
||||||
if ($exception !== false)
|
if ($exception === true)
|
||||||
throw new \Exception (
|
throw new \Exception (
|
||||||
"Invalid date provided or not matching the format", 500);
|
"Invalid date provided or not matching the format", 500);
|
||||||
return $inputDate;
|
return $inputDate;
|
||||||
@@ -33,7 +39,7 @@ class convert
|
|||||||
$errors = $date->getLastErrors();
|
$errors = $date->getLastErrors();
|
||||||
if ($errors["warning_count"] > 0 || $errors["error_count"] > 0)
|
if ($errors["warning_count"] > 0 || $errors["error_count"] > 0)
|
||||||
{
|
{
|
||||||
if ($exception !== false)
|
if ($exception === true)
|
||||||
throw new \Exception (
|
throw new \Exception (
|
||||||
"Invalid date provided or not matching the format", 500);
|
"Invalid date provided or not matching the format", 500);
|
||||||
return $inputDate;
|
return $inputDate;
|
||||||
|
|||||||
Reference in New Issue
Block a user