diff --git a/Changelog.md b/Changelog.md index af9fe95..06b0a60 100644 --- a/Changelog.md +++ b/Changelog.md @@ -3,6 +3,11 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release. ## UNRELEASED +## 0.4.3 + +### Fixed + +* Do not lose the data when you start using the `TaggablePSR6PoolAdapter` ## 0.4.2 diff --git a/TaggablePSR6ItemAdapter.php b/TaggablePSR6ItemAdapter.php index 77d5b06..5333966 100644 --- a/TaggablePSR6ItemAdapter.php +++ b/TaggablePSR6ItemAdapter.php @@ -82,9 +82,13 @@ public function get() { $rawItem = $this->cacheItem->get(); - if (is_array($rawItem) && isset($rawItem['value'])) { + // If it is a cache item we created + if ($this->isItemCreatedHere($rawItem)) { return $rawItem['value']; } + + // This is an item stored before we used this fake cache + return $rawItem; } /** @@ -178,7 +182,7 @@ private function initializeTags() if ($this->cacheItem->isHit()) { $rawItem = $this->cacheItem->get(); - if (is_array($rawItem) && isset($rawItem['tags'])) { + if ($this->isItemCreatedHere($rawItem)) { $this->tags = $rawItem['tags']; } } @@ -186,4 +190,16 @@ private function initializeTags() $this->initialized = true; } } + + /** + * Verify that the raw data is a cache item created by this class. + * + * @param mixed $rawItem + * + * @return bool + */ + private function isItemCreatedHere($rawItem) + { + return is_array($rawItem) && isset($rawItem['value']) && isset($rawItem['tags']) && count($rawItem) === 2; + } }