Skip to content

Commit

Permalink
Merge pull request #68 from chives/dev
Browse files Browse the repository at this point in the history
Added \DateTimeImmutable support for DateTime column type
  • Loading branch information
JarekW authored May 16, 2017
2 parents ef74d2e + 8c9339d commit 99799bb
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 30 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ php:
- 5.5
- 5.6
- 7.0
- 7.1

sudo: false

Expand Down
2 changes: 1 addition & 1 deletion doc/en/columns/datetime.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Provided by ``DataGrid\Extension\Core\CoreExtension``

**datetime_format** Format of showed date and/or time.

**input_type** Kind of data you are giving to column (``array``, ``datetime``, ``string``, ``timestamp``) - if no specified, column will try to guess it.
**input_type** Kind of data you are giving to column (``array``, ``datetime``, ``datetime_interface``, ``string``, ``timestamp``) - if no specified, column will try to guess it.

**input_field_format** Array of formats used if you specify more than one field in field_mapping option (that keys match 'field_mapping` option keys), otherwise its equal to 'datetime_format' option.

Expand Down
40 changes: 40 additions & 0 deletions lib/Extension/Core/ColumnType/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public function initOptions()
'string',
'timestamp',
'datetime',
'datetime_interface',
'array'
));
}
Expand Down Expand Up @@ -151,6 +152,22 @@ private function getInputData($value)
);
}

$inputData[$field] = $value[$field];
break;
case 'datetime_interface':
if (!interface_exists('\DateTimeInterface')) {
throw new DataGridColumnException(
'Input type option has value "datetime_interface" but \DateTimeInterface is not defined'
);
}

if (!empty($value[$field]) && !($value[$field] instanceof \DateTimeInterface)) {
throw new DataGridColumnException(
sprintf('Value in field "%s" is "%s" type instead of "\DateTimeInterface" instance.',
$field, gettype($value[$field]))
);
}

$inputData[$field] = $value[$field];
break;
default:
Expand Down Expand Up @@ -192,6 +209,25 @@ private function getInputData($value)
$inputData[$field] = $value;
break;

case 'datetime_interface':
if (!interface_exists('\DateTimeInterface')) {
throw new DataGridColumnException(
'Input type option has value "datetime_interface" but \DateTimeInterface is not defined'
);
}

$field = key($value);
$value = current($value);

if (!empty($value) && !($value instanceof \DateTimeInterface)) {
throw new DataGridColumnException(
sprintf('Value in field "%s" is not instance of "\DateTimeInterface"', $field)
);
}

$inputData[$field] = $value;
break;

case 'timestamp':
$field = key($value);
$value = current($value);
Expand Down Expand Up @@ -234,6 +270,10 @@ private function guessInput($value)
return 'datetime';
}

if (interface_exists('\DateTimeInterface') && ($value instanceof \DateTimeInterface)) {
return 'datetime_interface';
}

if (is_numeric($value)) {
return 'timestamp';
}
Expand Down
153 changes: 124 additions & 29 deletions tests/Extension/Core/ColumnType/DateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,28 @@ public function testBasicFilterValue()
);
}

public function testFilterValueFromDateTimeImmutable()
{
if (!class_exists('\DateTimeImmutable')) {
$this->markTestSkipped();
}

$dateTimeObject = new \DateTimeImmutable('2012-05-03 12:41:11');

$value = array(
'datetime' => $dateTimeObject
);

$this->column->setOption('field_mapping', array('datetime'));

$this->assertSame(
$this->column->filterValue($value),
array(
'datetime' => $dateTimeObject->format('Y-m-d H:i:s')
)
);
}

public function testFilterValueWithNull()
{
$value = array(
Expand All @@ -65,7 +87,12 @@ public function testFilterValueWithNull()
)
);

foreach (array('datetime', 'string', 'timestamp') as $input_type) {
$inputTypes = array('datetime', 'string', 'timestamp');
if (interface_exists('\DateTimeInterface')) {
$inputTypes[] = 'datetime_interface';
}

foreach ($inputTypes as $input_type) {

$this->column->setOptions(array(
'input_type' => $input_type
Expand Down Expand Up @@ -101,6 +128,31 @@ public function testFormatOption()
);
}

public function testFormatOptionWithDateTimeImmutable()
{
if (!class_exists('\DateTimeImmutable')) {
$this->markTestSkipped();
}

$dateTimeObject = new \DateTimeImmutable('2012-05-03 12:41:11');

$value = array(
'datetime' => $dateTimeObject
);

$this->column->setOptions(array(
'field_mapping' => array('datetime'),
'datetime_format' => 'Y.d.m'
));

$this->assertSame(
$this->column->filterValue($value),
array(
'datetime' => $dateTimeObject->format('Y.d.m')
)
);
}

/**
* @expectedException \InvalidArgumentException
*/
Expand Down Expand Up @@ -191,6 +243,26 @@ public function testMappingFieldsOptionInputArrayMissingMappingFieldsFormat()
$this->column->filterValue($value);
}

public function testMappingFieldsOptionInputArrayMissingMappingFieldsFormatForDateTimeImmutable()
{
if (!class_exists('\DateTimeImmutable')) {
$this->markTestSkipped();
}

$dateTimeObject = new \DateTimeImmutable('2012-05-03 12:41:11');
$dateObject = new \DateTimeImmutable('2012-05-03');

$value = array(
'datetime' => $dateTimeObject->format('Y-m-d H:i:s'),
'time' => $dateObject->format('Y-m-d H:i:s')
);

$this->column->setOption('input_type', 'array');

$this->setExpectedException('\FSi\Component\DataGrid\Exception\DataGridColumnException');
$this->column->filterValue($value);
}

/**
* @expectedException \FSi\Component\DataGrid\Exception\DataGridColumnException
*/
Expand All @@ -217,65 +289,88 @@ public function testMappingFieldsOptionInputArrayWrongMappingFieldsFormat()
public function testMappingFieldsOptionInputArray()
{
$dateTimeObject = new \DateTime('2012-05-03 12:41:11');
if (class_exists('\DateTimeImmutable')) {
$dateTimeImmutableObject = new \DateTimeImmutable('2012-05-03 12:41:11');
}
$dateObject = new \DateTime('2012-05-03');
$value = array(
'datetime' => $dateTimeObject,
'time' => $dateObject,
'string' => $dateTimeObject->format('Y-m-d H:i:s'),
'timestamp' => $dateTimeObject->getTimestamp()
);
if (class_exists('\DateTimeImmutable')) {
$value['datetime_immutable'] = $dateTimeImmutableObject;
}

$inputFieldFormat = array(
'datetime' => array('input_type' => 'datetime'),
'time' => array('input_type' => 'datetime'),
'string' => array('input_type' => 'string', 'datetime_format' => 'Y-m-d H:i:s'),
'timestamp' => array('input_type' => 'timestamp')
);
if (class_exists('\DateTimeImmutable')) {
$inputFieldFormat['datetime_immutable'] = array('input_type' => 'datetime_interface');
}
$this->column->setOptions(array(
'input_type' => 'array',
'input_field_format' => array(
'datetime' => array('input_type' => 'datetime'),
'time' => array('input_type' => 'datetime'),
'string' => array('input_type' => 'string', 'datetime_format' => 'Y-m-d H:i:s'),
'timestamp' => array('input_type' => 'timestamp')
)
'input_field_format' => $inputFieldFormat
));

$this->assertSame(
$this->column->filterValue($value),
array(
'datetime' => $dateTimeObject->format('Y-m-d H:i:s'),
'time' => $dateObject->format('Y-m-d 00:00:00'),
'string' => $dateTimeObject->format('Y-m-d H:i:s'),
'timestamp' => date('Y-m-d H:i:s', $dateTimeObject->getTimestamp()),
)
$expectedResult = array(
'datetime' => $dateTimeObject->format('Y-m-d H:i:s'),
'time' => $dateObject->format('Y-m-d 00:00:00'),
'string' => $dateTimeObject->format('Y-m-d H:i:s'),
'timestamp' => date('Y-m-d H:i:s', $dateTimeObject->getTimestamp()),
);
if (class_exists('\DateTimeImmutable')) {
$expectedResult['datetime_immutable'] = $dateTimeImmutableObject->format('Y-m-d H:i:s');
}
$this->assertSame($this->column->filterValue($value), $expectedResult);
}

public function testMappingFieldsOptionInputArrayWithFormat()
{
$dateTimeObject = new \DateTime('2012-05-03 12:41:11');
if (class_exists('\DateTimeImmutable')) {
$dateTimeImmutableObject = new \DateTimeImmutable('2012-05-03 12:41:11');
}
$dateObject = new \DateTime('2012-05-03');
$value = array(
'datetime' => $dateTimeObject,
'time' => $dateObject,
'string' => $dateTimeObject->format('Y-m-d H:i:s'),
'timestamp' => $dateTimeObject->getTimestamp()
);
if (class_exists('\DateTimeImmutable')) {
$value['datetime_immutable'] = $dateTimeImmutableObject;
}

$inputFieldFormat = array(
'datetime' => array('input_type' => 'datetime'),
'time' => array('input_type' => 'datetime'),
'string' => array('input_type' => 'string', 'datetime_format' => 'Y-m-d H:i:s'),
'timestamp' => array('input_type' => 'timestamp')
);

if (class_exists('\DateTimeImmutable')) {
$inputFieldFormat['datetime_immutable'] = array('input_type' => 'datetime_interface');
}
$this->column->setOptions(array(
'input_type' => 'array',
'datetime_format' => 'Y.d.m',
'input_field_format' => array(
'datetime' => array('input_type' => 'datetime'),
'time' => array('input_type' => 'datetime'),
'string' => array('input_type' => 'string', 'datetime_format' => 'Y-m-d H:i:s'),
'timestamp' => array('input_type' => 'timestamp')
)
'input_field_format' => $inputFieldFormat
));

$this->assertSame(
$this->column->filterValue($value),
array(
'datetime' => $dateTimeObject->format('Y.d.m'),
'time' => $dateObject->format('Y.d.m'),
'string' => $dateTimeObject->format('Y.d.m'),
'timestamp' => $dateTimeObject->format('Y.d.m')
)
$expectedResult = array(
'datetime' => $dateTimeObject->format('Y.d.m'),
'time' => $dateObject->format('Y.d.m'),
'string' => $dateTimeObject->format('Y.d.m'),
'timestamp' => $dateTimeObject->format('Y.d.m')
);
if (class_exists('\DateTimeImmutable')) {
$expectedResult['datetime_immutable'] = $dateTimeImmutableObject->format('Y.d.m');
}
$this->assertSame($this->column->filterValue($value), $expectedResult);
}
}

0 comments on commit 99799bb

Please sign in to comment.