File tree

4 files changed

+55
-7
lines changed

4 files changed

+55
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 8.1.3
2+
#### _"Don't throw the masks, yet."_
3+
##### _date to be defined_
4+
- __Core__
5+
- Fixed #860 // Cache item throw an error on save with DateTimeImmutable date objects
6+
17
## 8.1.2
28
#### _"Free the masks"_
39
##### 04 march 2022
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public function expiresAt($expiration): ExtendedCacheItemInterface
162162
* @param DateTimeInterface $expiration
163163
*/
164164
$this->eventManager->dis('CacheItemExpireAt', $this, $expiration);
165-
$this->expirationDate = $expiration;
165+
$this->expirationDate = $this->demutateDatetime($expiration);
166166
} else {
167167
throw new PhpfastcacheInvalidArgumentException('$expiration must be an object implementing the DateTimeInterface got: ' . \gettype($expiration));
168168
}
@@ -211,4 +211,11 @@ public function expiresAfter($time)
211211

212212
return $this;
213213
}
214+
215+
protected function demutateDatetime(\DateTimeInterface $dateTime): \DateTimeInterface
216+
{
217+
return $dateTime instanceof \DateTimeImmutable
218+
? \DateTime::createFromImmutable($dateTime)
219+
: $dateTime;
220+
}
214221
}
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
namespace Phpfastcache\Core\Pool;
1818

1919
use DateTime;
20+
use DateTimeInterface;
2021
use Exception;
2122
use Phpfastcache\Config\ConfigurationOption;
2223
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
@@ -179,27 +180,27 @@ public function driverUnwrapData(array $wrapper)
179180

180181
/**
181182
* @param array $wrapper
182-
* @return DateTime
183+
* @return ?DateTimeInterface
183184
*/
184-
public function driverUnwrapEdate(array $wrapper)
185+
public function driverUnwrapEdate(array $wrapper): ?DateTimeInterface
185186
{
186187
return $wrapper[self::DRIVER_EDATE_WRAPPER_INDEX];
187188
}
188189

189190
/**
190191
* @param array $wrapper
191-
* @return DateTime
192+
* @return ?DateTimeInterface
192193
*/
193-
public function driverUnwrapCdate(array $wrapper)
194+
public function driverUnwrapCdate(array $wrapper): ?DateTimeInterface
194195
{
195196
return $wrapper[self::DRIVER_CDATE_WRAPPER_INDEX];
196197
}
197198

198199
/**
199200
* @param array $wrapper
200-
* @return DateTime
201+
* @return ?DateTimeInterface
201202
*/
202-
public function driverUnwrapMdate(array $wrapper)
203+
public function driverUnwrapMdate(array $wrapper) :?DateTimeInterface
203204
{
204205
return $wrapper[self::DRIVER_MDATE_WRAPPER_INDEX];
205206
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/**
4+
* @author Khoa Bui (khoaofgod) <[email protected]> https://www.phpfastcache.com
5+
* @author Georges.L (Geolim4) <[email protected]>
6+
*/
7+
8+
use Phpfastcache\CacheManager;
9+
use Phpfastcache\Drivers\Files\Config as FilesConfig;
10+
use Phpfastcache\Tests\Helper\TestHelper;
11+
12+
chdir(__DIR__);
13+
require_once __DIR__ . '/../../vendor/autoload.php';
14+
$testHelper = new TestHelper(' issue #860 - Cache item throw an error on save with DateTimeImmutable date objects');
15+
16+
$config = new FilesConfig();
17+
$config->setItemDetailedDate(true);
18+
$cacheInstance = CacheManager::getInstance('Files', $config);
19+
$cacheInstance->clear();
20+
21+
try {
22+
$key = 'pfc_' . bin2hex(random_bytes(12));
23+
$item = $cacheInstance->getItem($key);
24+
$item->set(random_int(1000, 999999))
25+
->setExpirationDate(new DateTimeImmutable('+1 month'))
26+
->setCreationDate(new DateTimeImmutable())
27+
->setModificationDate(new DateTimeImmutable('+1 week'));
28+
$cacheInstance->save($item);
29+
$cacheInstance->detachAllItems();
30+
$item = $cacheInstance->getItem($key);
31+
$testHelper->assertPass(' issue #860 have not regressed.');
32+
} catch (\TypeError $e) {
33+
$testHelper->assertFail(' issue #860 have regressed, exception caught: ' . $e->getMessage());
34+
}

0 commit comments

Comments
 (0)