Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add model transformer on child + saveAsTimestamp() on DateTimeChildBu…
…ilder
- Loading branch information
1 parent
5bdfe88
commit 9d312b9
Showing
8 changed files
with
286 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Bdf\Form\Leaf\Date; | ||
|
||
use Bdf\Form\Child\ChildBuilder; | ||
use Bdf\Form\Leaf\Date\Transformer\DateTimeToTimestampTransformer; | ||
|
||
/** | ||
* Child builder for date time elements | ||
* | ||
* @extends ChildBuilder<DateTimeElementBuilder> | ||
*/ | ||
class DateTimeChildBuilder extends ChildBuilder | ||
{ | ||
/** | ||
* The model value of the input will be transformer to a timestamp | ||
* | ||
* <code> | ||
* // The entity : date is a timestamp | ||
* class MyEntity { | ||
* public int $date; | ||
* } | ||
* | ||
* // Build the element | ||
* $builder->dateTime('date')->saveAsTimestamp()->getter()->setter(); | ||
* | ||
* $form->import(MyEntity::get($id)); | ||
* $form['date']->element()->value(); // Value is an instance of DateTime | ||
* | ||
* $entity = $form->value(); | ||
* $entity->date; // date is a timestamp (i.e. integer value) | ||
* </code> | ||
* | ||
* @return $this | ||
* | ||
* @see DateTimeToTimestampTransformer | ||
*/ | ||
public function saveAsTimestamp(): self | ||
{ | ||
return $this->modelTransformer(new DateTimeToTimestampTransformer()); | ||
} | ||
} |
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
76 changes: 76 additions & 0 deletions
76
src/Leaf/Date/Transformer/DateTimeToTimestampTransformer.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,76 @@ | ||
<?php | ||
|
||
namespace Bdf\Form\Leaf\Date\Transformer; | ||
|
||
use Bdf\Form\ElementInterface; | ||
use Bdf\Form\Leaf\Date\DateTimeElement; | ||
use Bdf\Form\Transformer\TransformerInterface; | ||
use DateTime; | ||
use DateTimeInterface; | ||
use DateTimeZone; | ||
|
||
/** | ||
* Transform a DateTime instance from a form element to a timestamp to a model | ||
*/ | ||
final class DateTimeToTimestampTransformer implements TransformerInterface | ||
{ | ||
/** | ||
* @var class-string<DateTimeInterface>|null | ||
*/ | ||
private $className; | ||
|
||
/** | ||
* @var DateTimeZone|null | ||
*/ | ||
private $timezone; | ||
|
||
|
||
/** | ||
* DateTimeToTimestampTransformer constructor. | ||
* | ||
* @param class-string<DateTimeInterface>|null $className The date time class name to use when retrieving value from model. If null, will use the class defined in the input element | ||
* @param DateTimeZone|null $timezone The timezone to set when retrieving value from model. If null will use the element's timezone | ||
*/ | ||
public function __construct(?string $className = null, ?DateTimeZone $timezone = null) | ||
{ | ||
$this->className = $className; | ||
$this->timezone = $timezone; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @psalm-suppress UndefinedInterfaceMethod | ||
* @psalm-suppress PossiblyUndefinedMethod | ||
*/ | ||
public function transformToHttp($value, ElementInterface $input): ?DateTimeInterface | ||
{ | ||
if ($value === null) { | ||
return null; | ||
} | ||
|
||
$className = $this->className ?? ($input instanceof DateTimeElement ? $input->dateTimeClassName() : DateTime::class); | ||
$timezone = $this->timezone ?? ($input instanceof DateTimeElement ? $input->timezone() : null); | ||
|
||
/** @var DateTimeInterface $dateTime */ | ||
$dateTime = new $className; | ||
|
||
if ($timezone) { | ||
$dateTime = $dateTime->setTimezone($timezone); | ||
} | ||
|
||
return $dateTime->setTimestamp($value); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function transformFromHttp($value, ElementInterface $input): ?int | ||
{ | ||
if (!$value instanceof DateTimeInterface) { | ||
return null; | ||
} | ||
|
||
return $value->getTimestamp(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace Bdf\Form\Leaf\Date; | ||
|
||
use Bdf\Form\Aggregate\Collection\ChildrenCollection; | ||
use Bdf\Form\Aggregate\Form; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class DateTimeChildBuilderTest extends TestCase | ||
{ | ||
/** | ||
* | ||
*/ | ||
public function test_saveAsTimestamp() | ||
{ | ||
$builder = new DateTimeChildBuilder('child', new DateTimeElementBuilder()); | ||
|
||
$child = $builder | ||
->immutable() | ||
->getter()->setter() | ||
->saveAsTimestamp() | ||
->buildChild() | ||
; | ||
|
||
$child->setParent(new Form(new ChildrenCollection())); | ||
$child->import(['child' => 123]); | ||
|
||
$this->assertInstanceOf(\DateTimeImmutable::class, $child->element()->value()); | ||
$this->assertEquals(123, $child->element()->value()->getTimestamp()); | ||
|
||
$child->element()->import(new \DateTimeImmutable('2020-10-15 00:00:00')); | ||
$target = []; | ||
$child->fill($target); | ||
|
||
$this->assertSame(['child' => 1602712800], $target); | ||
|
||
$child->import(['child' => null]); | ||
$this->assertNull($child->element()->value()); | ||
|
||
$child->fill($target); | ||
$this->assertSame(['child' => null], $target); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
tests/Leaf/Date/Transformer/DateTimeToTimestampTransformerTest.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,66 @@ | ||
<?php | ||
|
||
namespace Bdf\Form\Leaf\Date\Transformer; | ||
|
||
use Bdf\Form\Aggregate\FormBuilder; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Class DateTimeToTimestampTransformerTest | ||
*/ | ||
class DateTimeToTimestampTransformerTest extends TestCase | ||
{ | ||
/** | ||
* | ||
*/ | ||
public function test_default() | ||
{ | ||
$builder = new FormBuilder(); | ||
$builder->dateTime('foo')->getter()->setter()->modelTransformer(new DateTimeToTimestampTransformer()); | ||
$form = $builder->buildElement(); | ||
|
||
$form->import(['foo' => 123]); | ||
$this->assertSame(['foo' => 123], $form->value()); | ||
|
||
$this->assertEquals(new \DateTime('@123'), $form['foo']->element()->value()); | ||
$this->assertEquals(new \DateTimeZone(date_default_timezone_get()), $form['foo']->element()->value()->getTimezone()); | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
public function test_default_with_className() | ||
{ | ||
$builder = new FormBuilder(); | ||
$builder->dateTime('foo')->getter()->setter()->modelTransformer(new DateTimeToTimestampTransformer(CustomDateTime::class)); | ||
$form = $builder->buildElement(); | ||
|
||
$form->import(['foo' => 123]); | ||
$this->assertSame(['foo' => 123], $form->value()); | ||
|
||
$this->assertInstanceOf(CustomDateTime::class, $form['foo']->element()->value()); | ||
$this->assertEquals(new CustomDateTime('@123'), $form['foo']->element()->value()); | ||
$this->assertEquals(new \DateTimeZone(date_default_timezone_get()), $form['foo']->element()->value()->getTimezone()); | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
public function test_default_with_className_and_timezone() | ||
{ | ||
$builder = new FormBuilder(); | ||
$builder->dateTime('foo')->getter()->setter()->modelTransformer(new DateTimeToTimestampTransformer(CustomDateTime::class, new \DateTimeZone('Asia/Shanghai'))); | ||
$form = $builder->buildElement(); | ||
|
||
$form->import(['foo' => 123]); | ||
$this->assertSame(['foo' => 123], $form->value()); | ||
|
||
$this->assertEquals(new CustomDateTime('@123'), $form['foo']->element()->value()); | ||
$this->assertEquals(new \DateTimeZone('Asia/Shanghai'), $form['foo']->element()->value()->getTimezone()); | ||
} | ||
} | ||
|
||
class CustomDateTime extends \DateTime | ||
{ | ||
|
||
} |
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