diff --git a/Model/Resource.php b/Model/Resource.php index 9975d7e..5af9d4c 100644 --- a/Model/Resource.php +++ b/Model/Resource.php @@ -11,7 +11,9 @@ namespace FSi\Bundle\ResourceRepositoryBundle\Model; +use DateTime; use DateTimeImmutable; +use DateTimeInterface; class Resource implements ResourceValue { @@ -82,7 +84,7 @@ public function getTextValue() public function setDateValue($dateValue) { - $this->dateValue = $dateValue; + $this->dateValue = $this->toDateTimeImmutable($dateValue); } public function getDateValue() @@ -92,7 +94,7 @@ public function getDateValue() public function setDatetimeValue($datetimeValue) { - $this->datetimeValue = $datetimeValue; + $this->datetimeValue = $this->toDateTimeImmutable($datetimeValue); } public function getDatetimeValue() @@ -102,7 +104,7 @@ public function getDatetimeValue() public function setTimeValue($timeValue) { - $this->timeValue = $timeValue; + $this->timeValue = $this->toDateTimeImmutable($timeValue); } public function getTimeValue() @@ -139,4 +141,20 @@ public function getBoolValue() { return $this->boolValue; } + + /** + * Symfony date/time/datetime forms do not allow for default DateTimeImmutable + * value until version 4.2, so the values need to be casted manually. + * + * @param DateTimeInterface|null $value + * @return DateTimeImmutable|null + */ + private function toDateTimeImmutable(?DateTimeInterface $value): ?DateTimeImmutable + { + if (true === $value instanceof DateTime) { + $value = DateTimeImmutable::createFromMutable($value); + } + + return $value; + } } diff --git a/Model/ResourceValue.php b/Model/ResourceValue.php index 72dc831..8eaa7c5 100644 --- a/Model/ResourceValue.php +++ b/Model/ResourceValue.php @@ -11,7 +11,7 @@ namespace FSi\Bundle\ResourceRepositoryBundle\Model; -Interface ResourceValue +interface ResourceValue { /** * @param string $key diff --git a/composer.json b/composer.json index 81c5191..04afcd7 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,7 @@ "fsi/doctrine-extensions-bundle": "^1.0|^2.0", "fsi/form-extensions-bundle": "^2.0", "phpunit/phpunit": "^7.1", - "phpspec/phpspec": "^4.3", + "phpspec/phpspec": "^5.0", "phpspec/prophecy": "^1.7", "sebastian/exporter": "^3.1.0" },