Skip to content

Commit

Permalink
Add configurable datetime object support. Bump nette php-generator ve…
Browse files Browse the repository at this point in the history
…rsion. Drop php5.4 and php5.5 support, add php7.1 tests
  • Loading branch information
klatys committed Nov 29, 2017
1 parent 2d4784b commit 306139e
Show file tree
Hide file tree
Showing 7 changed files with 716 additions and 25 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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"require": {
"php": ">=5.4",
"doctrine/annotations": "~1.2",
"nette/php-generator": "~2.3",
"nette/php-generator": "~2.6",
"symfony/finder": "~2.7|~3.0",
"symfony/console": "~2.7|~3.0"
},
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
27 changes: 17 additions & 10 deletions src/Skrz/Meta/DateTimeFormattingSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@ class DateTimeFormattingSerializer implements PropertySerializerInterface
/** @var string */
private $format;

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

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

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

public function addGroup($group)
Expand All @@ -36,8 +43,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 +62,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 +80,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" .
"\t\t\$datetimeInstanceReturn = null;\n" .
"\t} else {\n" .
"\t\t\$datetimeInstanceReturn = \\DateTime::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 306139e

Please sign in to comment.