Skip to content

Commit

Permalink
add timezone option to StringToDateTimeTransformer.php
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehyiah committed Mar 15, 2024
1 parent f6dc6fb commit 439782c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ If you pick as examples the ```DateTimeTransformer``` : It is said as an <u>open
| | | | |

### Narrow-minded:
| Transformer | transform | reverseTransform | options |
|:---------------------------:|:-------------------------------------------:|:----------------------------------------------------:|:-------------------------------------------------------------------------------------------------------------------------------------------------------:|
| StringToDateTimeTransformer | string to DateTime | DateTime to string | ```format``` (use to transform the string with the provided format)<br/> example ```'format' => 'Y/m/d'``` |
| StringToBooleanTransformer | string to Boolean | Boolean to string | ```trueValue``` or ```falseValue``` (used in reverseTransform only) ```'trueValue' => 'MyCustomTrueValue'``` <br/><br/> ```strict``` : true or false |
| StringToEnumTransformer | Keep original value (enum or array of enum) | string or array of strings to enum or array of enum | ```enum``` the class of the enum example : ```'enum' => MyEnum::class``` |
| | | | |
| Transformer | transform | reverseTransform | options |
|:---------------------------:|:-------------------------------------------:|:----------------------------------------------------:|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:|
| StringToDateTimeTransformer | string to DateTime | DateTime to string | ```format``` (use to transform the string with the provided format)<br/> example ```'format' => 'Y/m/d'``` <br/> ```timezone``` a valid DateTimeZone object example : ```'timezone' => new DateTimeZone('UTC')``` |
| StringToBooleanTransformer | string to Boolean | Boolean to string | ```trueValue``` or ```falseValue``` (used in reverseTransform only) ```'trueValue' => 'MyCustomTrueValue'``` <br/><br/> ```strict``` : true or false |
| StringToEnumTransformer | Keep original value (enum or array of enum) | string or array of strings to enum or array of enum | ```enum``` the class of the enum example : ```'enum' => MyEnum::class``` |
| | | | |
6 changes: 5 additions & 1 deletion src/Transformer/StringToDateTimeTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ public function transform(mixed $data, array $options, object $targetObject, obj
throw new WrongDataTypeTransformerException('Data is supposed to be a string to use transform : ' . self::class . ' ' . gettype($data) . ' provided');
}

return new DateTime($data);
if (isset($options['timezone'])) {
$timezone = $options['timezone'];
}

return new DateTime($data, $timezone ?? null);
}

/**
Expand Down
25 changes: 25 additions & 0 deletions tests/Unit/Transformers/StringToDateTimeTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DateTime;
use DateTimeInterface;
use DateTimeZone;
use Ehyiah\MappingBundle\Tests\Dummy\DummyMappedObject;
use Ehyiah\MappingBundle\Tests\Dummy\DummyTargetObject;
use Ehyiah\MappingBundle\Transformer\StringToDateTimeTransformer;
Expand All @@ -28,6 +29,30 @@ public function testTransform(): void
$this->assertInstanceOf(DateTimeInterface::class, $result);
}

/**
* @covers ::transform
*/
public function testTransformWithDateTimeZone(): void
{
$transformer = new StringToDateTimeTransformer();

$data = '2025-12-12';

$result = $transformer->transform($data, ['timezone' => new DateTimeZone('UTC')], new DummyTargetObject(), new DummyMappedObject());

$timezone = $result->getTimezone()->getName();

$this->assertInstanceOf(DateTimeInterface::class, $result);
$this->assertSame('UTC', $timezone);

$result = $transformer->transform($data, ['timezone' => new DateTimeZone('Europe/Tallinn')], new DummyTargetObject(), new DummyMappedObject());

$timezone = $result->getTimezone()->getName();

$this->assertInstanceOf(DateTimeInterface::class, $result);
$this->assertSame('Europe/Tallinn', $timezone);
}

/**
* @covers ::reverseTransform
*/
Expand Down

0 comments on commit 439782c

Please sign in to comment.