Skip to content

Commit

Permalink
Merge pull request #21 from klatys/configurable-datetime
Browse files Browse the repository at this point in the history
Add configurable datetime object support
  • Loading branch information
mzstic authored Dec 6, 2017
2 parents 2d4784b + 9a8e4e1 commit 013af64
Show file tree
Hide file tree
Showing 7 changed files with 723 additions and 29 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1
- hhvm

cache:
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
"require": {
"php": ">=5.4",
"doctrine/annotations": "~1.2",
"nette/php-generator": "~2.3",
"symfony/finder": "~2.7|~3.0",
"symfony/console": "~2.7|~3.0"
"nette/php-generator": "~2.6",
"symfony/finder": "~2.7|~3.0|~4.0",
"symfony/console": "~2.7|~3.0|~4.0"
},
"require-dev": {
"phpunit/phpunit": "~4.6"
Expand Down
13 changes: 2 additions & 11 deletions src/Skrz/Meta/BaseModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,17 +205,8 @@ public function onGenerate(AbstractMetaSpec $spec, MetaSpecMatcher $matcher, Typ
$hash->addBody("{$indent}hash_update(\$ctx, (string){$objectPath});");

} elseif ($baseType instanceof Type) {
$datetimeType = false;

for ($t = $baseType; $t; $t = $t->getParentClass()) {
if ($t->getName() === "DateTime") {
$datetimeType = true;
break;
}
}

if ($datetimeType) {
$hash->addBody("{$indent}hash_update(\$ctx, {$objectPath} instanceof \\DateTime ? {$objectPath}->format(\\DateTime::ISO8601) : '');");
if ($baseType->isDateTime()) {
$hash->addBody("{$indent}hash_update(\$ctx, {$objectPath} instanceof \\DateTimeInterface ? {$objectPath}->format(\\DateTime::ISO8601) : '');");
} else {
$propertyTypeMetaClassName = $spec->createMetaClassName($baseType);
$namespace->addUse($propertyTypeMetaClassName, null, $propertyTypeMetaClassNameAlias);
Expand Down
34 changes: 22 additions & 12 deletions src/Skrz/Meta/DateTimeFormattingSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,22 @@ class DateTimeFormattingSerializer implements PropertySerializerInterface
/** @var string */
private $format;

/** @var string */
private $dateTimeClass;

/** @var string[] */
private $groups = null;

public function __construct($format)
private $emptyValue;

public function __construct($format, $dateTimeClass = \DateTime::class, $emptyValue = '0000-00-00 00:00:00')
{
$this->format = $format;
$this->dateTimeClass = $dateTimeClass;
if (!substr($dateTimeClass, 0, 1) != "\\") {
$this->dateTimeClass = "\\" . $this->dateTimeClass;
}
$this->emptyValue = $emptyValue;
}

public function addGroup($group)
Expand All @@ -36,8 +46,8 @@ public function matches(Property $property, $group)

return
($this->groups === null || in_array($group, $this->groups)) &&
$baseType instanceof Type &&
strtolower($baseType->getName()) === "datetime";
$baseType instanceof Type && $baseType->isDateTime();

}

public function matchesSerialize(Property $property, $group)
Expand All @@ -55,14 +65,14 @@ public function serialize(Property $property, $group, $inputExpression)
return StatementAndExpressionVO::withStatementAndExpression(
"if ({$inputExpression} === null) {\n" .
"\t\$datetimeStringReturn = null;\n" .
"} elseif ({$inputExpression} instanceof \\DateTime) {\n" .
"} elseif ({$inputExpression} instanceof \\DateTimeInterface) {\n" .
"\t\$datetimeStringReturn = {$inputExpression}->format(" . var_export($this->format, true) . ");\n" .
"} elseif (is_numeric({$inputExpression})) {\n" .
"\t\$datetimeStringReturn = (new \\DateTime('@' . intval({$inputExpression})))->format(" . var_export($this->format, true) . ");\n" .
"\t\$datetimeStringReturn = (new " . $this->dateTimeClass . "('@' . intval({$inputExpression})))->format(" . var_export($this->format, true) . ");\n" .
"} elseif (is_string({$inputExpression})) {\n" .
"\t\$datetimeStringReturn = (new \\DateTime({$inputExpression}))->format(" . var_export($this->format, true) . ");\n" .
"\t\$datetimeStringReturn = (new " . $this->dateTimeClass . "({$inputExpression}))->format(" . var_export($this->format, true) . ");\n" .
"} elseif (is_array({$inputExpression}) && isset({$inputExpression}['date'])) {\n" .
"\t\$datetimeStringReturn = (new \\DateTime({$inputExpression}['date']))->format(" . var_export($this->format, true) . ");\n" .
"\t\$datetimeStringReturn = (new " . $this->dateTimeClass . "({$inputExpression}['date']))->format(" . var_export($this->format, true) . ");\n" .
"} else {\n" .
"\tthrow new \\InvalidArgumentException('Could not serialize date of format ' . " . var_export($this->format, true) . " . '.');\n" .
"}",
Expand All @@ -73,18 +83,18 @@ public function serialize(Property $property, $group, $inputExpression)
public function deserialize(Property $property, $group, $inputExpression)
{
return StatementAndExpressionVO::withStatementAndExpression(
"if ({$inputExpression} instanceof \\DateTime) {\n" .
"if ({$inputExpression} instanceof \\DateTimeInterface) {\n" .
"\t\$datetimeInstanceReturn = {$inputExpression};\n" .
"} elseif (is_numeric({$inputExpression})) {\n" .
"\t\$datetimeInstanceReturn = new \\DateTime('@' . intval({$inputExpression}));\n" .
"\t\$datetimeInstanceReturn = new " . $this->dateTimeClass . "('@' . intval({$inputExpression}));\n" .
"} elseif (is_string({$inputExpression})) {\n" .
"\tif ({$inputExpression} === '0000-00-00 00:00:00') {\n" .
"\tif ({$inputExpression} === " . var_export($this->emptyValue, true) . ") {\n" .
"\t\t\$datetimeInstanceReturn = null;\n" .
"\t} else {\n" .
"\t\t\$datetimeInstanceReturn = \\DateTime::createFromFormat(" . var_export($this->format, true) . ", {$inputExpression});\n" .
"\t\t\$datetimeInstanceReturn = " . $this->dateTimeClass . "::createFromFormat(" . var_export($this->format, true) . ", {$inputExpression});\n" .
"\t}\n" .
"} elseif (is_array({$inputExpression}) && isset({$inputExpression}['date'])) {\n" .
"\t\$datetimeInstanceReturn = new \\DateTime({$inputExpression}['date']);\n" .
"\t\$datetimeInstanceReturn = new " . $this->dateTimeClass . "({$inputExpression}['date']);\n" .
"} elseif ({$inputExpression} === null) {\n" .
"\t\$datetimeInstanceReturn = null;\n" .
"} else {\n" .
Expand Down
16 changes: 16 additions & 0 deletions src/Skrz/Meta/Reflection/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -879,4 +879,20 @@ public function __toString()
return $this->getName();
}

public function isDateTime()
{
if ($this->getName() === \DateTime::class
|| $this->getName() === \DateTimeImmutable::class
) {
return true;
}
for ($t = $this; $t; $t = $t->getParentClass()) {
if (in_array(\DateTimeInterface::class, $t->getInterfaces())) {
return true;
break;
}
}
return false;
}

}
2 changes: 1 addition & 1 deletion test/Skrz/Meta/Fixtures/PHP/PhpMetaSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class PhpMetaSpec extends AbstractMetaSpec
{

protected function configure()
protected function configure($dateTimeObject = \DateTime::class)
{
$this->match("Skrz\\Meta\\Fixtures\\PHP\\ClassWith*")
->addModule($phpModule = new PhpModule());
Expand Down
Loading

0 comments on commit 013af64

Please sign in to comment.