Skip to content

Commit

Permalink
Add Optional second paramater to DateTimeValueConverter to format output
Browse files Browse the repository at this point in the history
  • Loading branch information
Aydin committed May 15, 2014
1 parent ad21938 commit 453d487
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 17 deletions.
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,12 @@ Value converters are used to convert specific fields (e.g., columns in database)

#### DateTimeValueConverter

Converts a date representation in a format you specify into a `DateTime` object:
There are two uses for the DateTimeValueConverter:

1. Convert a date representation in a format you specify into a `DateTime` object.
2. Convert a date representation in a format you specify into a different format.

##### Convert a date into a `DateTime` object.

```php
use Ddeboer\DataImport\ValueConverter\DateTimeValueConverter;
Expand All @@ -568,6 +573,33 @@ $converter = new DateTimeValueConverter('d/m/Y H:i:s');
$workflow->addValueConverter('my_date_field', $converter);
```

If your date string is in a format specified at: http://www.php.net/manual/en/datetime.formats.date.php then you can omit the format parameter.

```php
use Ddeboer\DataImport\ValueConverter\DateTimeValueConverter;

$converter = new DateTimeValueConverter();
$workflow->addValueConverter('my_date_field', $converter);
```

##### Convert a date string into a differently formatted date string.

```php
use Ddeboer\DataImport\ValueConverter\DateTimeValueConverter;

$converter = new DateTimeValueConverter('d/m/Y H:i:s', 'd-M-Y');
$workflow->addValueConverter('my_date_field', $converter);
```

If your date is in a format specified at: http://www.php.net/manual/en/datetime.formats.date.php you can pass `null` as the first argument.

```php
use Ddeboer\DataImport\ValueConverter\DateTimeValueConverter;

$converter = new DateTimeValueConverter(null, 'd-M-Y');
$workflow->addValueConverter('my_date_field', $converter);
```

#### ObjectConverter

Converts an object into a scalar value. To use this converter, you must include
Expand Down
48 changes: 34 additions & 14 deletions src/Ddeboer/DataImport/ValueConverter/DateTimeValueConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace Ddeboer\DataImport\ValueConverter;

/**
* Convert an input value to a PHP \DateTime object
*
* Convert an date string into another date string
* Eg. You want to change the format of a string OR
* If no output specified, return DateTime instance
*/
class DateTimeValueConverter implements ValueConverterInterface
{
Expand All @@ -14,41 +15,60 @@ class DateTimeValueConverter implements ValueConverterInterface
* @var string
* @see http://php.net/manual/en/datetime.createfromformat.php
*/
protected $format;
protected $inputFormat;

/**
* Construct a DateTime converter
* Date time format
*
* @param string $format Optional
* @var string
* @see http://php.net/manual/en/datetime.createfromformat.php
*/
public function __construct($format = null)
protected $outputFormat;

/**
* @param string $inputFormat
* @param string $outputFormat
*/
public function __construct($inputFormat = null, $outputFormat = null)
{
$this->format = $format;
$this->inputFormat = $inputFormat;
$this->outputFormat = $outputFormat;
}

/**
* Convert string to date time object
* + then convert back to a string
* using specified format
*
* @param string $input
* If no output format specified then return
* the \DateTime instance
*
* @return \DateTime
* @param mixed $input
* @return \DateTime|string
* @throws UnexpectedValueException
*/
public function convert($input)
{
if (!$input) {
return;
}

if ($this->format) {
$date = \DateTime::createFromFormat($this->format, $input);
if ($this->inputFormat) {
$date = \DateTime::createFromFormat($this->inputFormat, $input);
if (false === $date) {
throw new \UnexpectedValueException(
$input . ' is not a valid date/time according to format ' . $this->format);
$input . ' is not a valid date/time according to format ' . $this->inputFormat
);
}
} else {
$date = new \DateTime($input);
}

return $date;
if ($this->outputFormat) {
return $date->format($this->outputFormat);
}

return new \DateTime($input);
//if no output format specified we just return the \DateTime instance
return $date;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class DateTimeValueConverterTest extends \PHPUnit_Framework_TestCase
{
public function testConvertWithoutFormat()
public function testConvertWithoutInputOrOutputFormatReturnsDateTimeInstance()
{
$value = '2011-10-20 13:05';
$converter = new DateTimeValueConverter;
Expand All @@ -15,12 +15,43 @@ public function testConvertWithoutFormat()
$this->assertEquals('13', $output->format('H'));
}

public function testConvertWithFormat()
public function testConvertWithFormatReturnsDateTimeInstance()
{
$value = '14/10/2008 09:40:20';
$converter = new DateTimeValueConverter('d/m/Y H:i:s');
$output = $converter->convert($value);
$this->assertInstanceOf('\DateTime', $output);
$this->assertEquals('20', $output->format('s'));
}

public function testConvertWithInputAndOutputFormatReturnsString()
{
$value = '14/10/2008 09:40:20';
$converter = new DateTimeValueConverter('d/m/Y H:i:s', 'd-M-Y');
$output = $converter->convert($value);
$this->assertEquals('14-Oct-2008', $output);
}

public function testConvertWithNoInputStringWithOutputFormatReturnsString()
{
$value = '2011-10-20 13:05';
$converter = new DateTimeValueConverter(null, 'd-M-Y');
$output = $converter->convert($value);
$this->assertEquals('20-Oct-2011', $output);

}

public function testInvalidInputFormatThrowsException()
{
$value = '14/10/2008 09:40:20';
$converter = new DateTimeValueConverter('d-m-y', 'd-M-Y');
$this->setExpectedException("UnexpectedValueException", "14/10/2008 09:40:20 is not a valid date/time according to format d-m-y");
$converter->convert($value);
}

public function testNullIsReturnedIfNullPassed()
{
$converter = new DateTimeValueConverter('d-m-y', 'd-M-Y');
$this->assertNull($converter->convert(null));
}
}

0 comments on commit 453d487

Please sign in to comment.