-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added DateTimeToStringValueConverter and its test. Updated README. Ad…
…ded phpunit to composer.json
- Loading branch information
Showing
6 changed files
with
105 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/Ddeboer/DataImport/ValueConverter/DateTimeToStringValueConverter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Ddeboer\DataImport\ValueConverter; | ||
|
||
/** | ||
* Convert an date time object into string | ||
*/ | ||
class DateTimeToStringValueConverter implements ValueConverterInterface | ||
{ | ||
/** | ||
* Date time format | ||
* | ||
* @var string | ||
* @see http://php.net/manual/en/datetime.createfromformat.php | ||
*/ | ||
protected $outputFormat; | ||
|
||
/** | ||
* @param string $outputFormat | ||
*/ | ||
public function __construct($outputFormat = 'Y-m-d H:i:s') | ||
{ | ||
$this->outputFormat = $outputFormat; | ||
} | ||
|
||
/** | ||
* Convert string to date time object | ||
* using specified format | ||
* | ||
* @param mixed $input | ||
* @return \DateTime|string | ||
* @throws UnexpectedValueException | ||
*/ | ||
public function convert($input) | ||
{ | ||
if (!$input) { | ||
return; | ||
} | ||
|
||
if (!($input instanceof \DateTime)) { | ||
throw new \UnexpectedValueException('Input must be DateTime object.'); | ||
} | ||
|
||
return $input->format($this->outputFormat); | ||
}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
tests/Ddeboer/DataImport/Tests/ValueConverter/DateTimeToStringValueConverterTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Ddeboer\DataImport\Tests\ValueConverter; | ||
|
||
use Ddeboer\DataImport\ValueConverter\DateTimeToStringValueConverter; | ||
|
||
class DateTimeToStringValueConverterTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testConvertWithoutOutputFormatReturnsString() | ||
{ | ||
$value = new \DateTime('2010-01-01 01:00:00'); | ||
$converter = new DateTimeToStringValueConverter; | ||
$output = $converter->convert($value); | ||
$this->assertEquals('2010-01-01 01:00:00', $value->format('Y-m-d H:i:s')); | ||
} | ||
|
||
public function testInvalidInputFormatThrowsException() | ||
{ | ||
$value = '14/10/2008 09:40:20'; | ||
$converter = new DateTimeToStringValueConverter; | ||
$this->setExpectedException("UnexpectedValueException", "Input must be DateTime object."); | ||
$converter->convert($value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters