Skip to content

Commit

Permalink
Added DateTimeToStringValueConverter and its test. Updated README. Ad…
Browse files Browse the repository at this point in the history
…ded phpunit to composer.json
  • Loading branch information
Zales0123 committed Feb 4, 2015
1 parent 8f7f225 commit 1e14c81
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 23 deletions.
38 changes: 25 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Documentation
- [Create an item converter](#create-an-item-converter)
- [CallbackItemConverter](#callbackitemconverter)
- [Value converters](#value-converters)
- [DateTimeValueConverter](#datetimevalueconverter)
- [StringToDateTimeValueConverter](#stringtodatetimevalueconverter)
- [ObjectConverter](#objectconverter)
- [StringToObjectConverter](#stringtoobjectconverter)
- [ArrayValueConverterMap](#arrayvalueconvertermap)
Expand Down Expand Up @@ -861,49 +861,61 @@ $converter = new CallbackItemConverter(function ($item) use ($translator) {

Value converters are used to convert specific fields (e.g., columns in database).

#### DateTimeValueConverter
#### StringToDateTimeValueConverter

There are two uses for the DateTimeValueConverter:
There are two uses for the StringToDateTimeValueConverter:

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;
use Ddeboer\DataImport\ValueConverter\StringDateTimeValueConverter;

$converter = new DateTimeValueConverter('d/m/Y H:i:s');
$converter = new StringToDateTimeValueConverter('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;
use Ddeboer\DataImport\ValueConverter\StringToDateTimeValueConverter;

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

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

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

$converter = new DateTimeValueConverter('d/m/Y H:i:s', 'd-M-Y');
$converter = new StringToDateTimeValueConverter('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;
use Ddeboer\DataImport\ValueConverter\StringToDateTimeValueConverter;

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

#### DateTimeToStringValueConverter

There main use of DateTimeToStringValueConverter is to convert DateTime object into it's string representation in proper format.
Default format is 'Y-m-d H:i:s';

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

$converter = new DateTimeToStringValueConverter;
$converter->convert(\DateTime('2010-01-01 01:00:00')); //will return string '2010-01-01 01:00:00'
```

#### ObjectConverter

Converts an object into a scalar value. To use this converter, you must include
Expand Down Expand Up @@ -1068,7 +1080,7 @@ Then you can import the CSV and save it as your entity in the following way.
use Ddeboer\DataImport\Workflow;
use Ddeboer\DataImport\Reader\CsvReader;
use Ddeboer\DataImport\Writer\DoctrineWriter;
use Ddeboer\DataImport\ValueConverter\DateTimeValueConverter;
use Ddeboer\DataImport\ValueConverter\StringToDateTimeValueConverter;

// Create and configure the reader
$file = new \SplFileObject('input.csv');
Expand All @@ -1086,7 +1098,7 @@ $workflow->addWriter($doctrineWriter);

// Add a converter to the workflow that will convert `beginDate` and `endDate`
// to \DateTime objects
$dateTimeConverter = new DateTimeValueConverter('Ymd');
$dateTimeConverter = new StringToDateTimeValueConverter('Ymd');
$workflow
->addValueConverter('beginDate', $dateTimeConverter)
->addValueConverter('endDate', $dateTimeConverter);
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"doctrine/dbal": "*",
"doctrine/orm": "*",
"symfony/console": "~2.5.0",
"symfony/validator": "~2.3.0"
"symfony/validator": "~2.3.0",
"phpunit/phpunit": "3.7.*"
},
"suggest": {
"ext-iconv": "For the CharsetValueConverter",
Expand Down
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);
}}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Eg. You want to change the format of a string OR
* If no output specified, return DateTime instance
*/
class DateTimeValueConverter implements ValueConverterInterface
class StringToDateTimeValueConverter implements ValueConverterInterface
{
/**
* Date time format
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace Ddeboer\DataImport\Tests\ValueConverter;

use Ddeboer\DataImport\ValueConverter\DateTimeValueConverter;
use Ddeboer\DataImport\ValueConverter\StringToDateTimeValueConverter;

class DateTimeValueConverterTest extends \PHPUnit_Framework_TestCase
class StringToStringToDateTimeValueConverterTest extends \PHPUnit_Framework_TestCase
{
public function testConvertWithoutInputOrOutputFormatReturnsDateTimeInstance()
{
$value = '2011-10-20 13:05';
$converter = new DateTimeValueConverter;
$converter = new StringToDateTimeValueConverter;
$output = $converter->convert($value);
$this->assertInstanceOf('\DateTime', $output);
$this->assertEquals('13', $output->format('H'));
Expand All @@ -18,7 +18,7 @@ public function testConvertWithoutInputOrOutputFormatReturnsDateTimeInstance()
public function testConvertWithFormatReturnsDateTimeInstance()
{
$value = '14/10/2008 09:40:20';
$converter = new DateTimeValueConverter('d/m/Y H:i:s');
$converter = new StringToDateTimeValueConverter('d/m/Y H:i:s');
$output = $converter->convert($value);
$this->assertInstanceOf('\DateTime', $output);
$this->assertEquals('20', $output->format('s'));
Expand All @@ -27,15 +27,15 @@ public function testConvertWithFormatReturnsDateTimeInstance()
public function testConvertWithInputAndOutputFormatReturnsString()
{
$value = '14/10/2008 09:40:20';
$converter = new DateTimeValueConverter('d/m/Y H:i:s', 'd-M-Y');
$converter = new StringToDateTimeValueConverter('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');
$converter = new StringToDateTimeValueConverter(null, 'd-M-Y');
$output = $converter->convert($value);
$this->assertEquals('20-Oct-2011', $output);

Expand All @@ -44,14 +44,14 @@ public function testConvertWithNoInputStringWithOutputFormatReturnsString()
public function testInvalidInputFormatThrowsException()
{
$value = '14/10/2008 09:40:20';
$converter = new DateTimeValueConverter('d-m-y', 'd-M-Y');
$converter = new StringToDateTimeValueConverter('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');
$converter = new StringToDateTimeValueConverter('d-m-y', 'd-M-Y');
$this->assertNull($converter->convert(null));
}
}

0 comments on commit 1e14c81

Please sign in to comment.