diff --git a/src/Cache.php b/src/Cache.php index f479163..38993a0 100644 --- a/src/Cache.php +++ b/src/Cache.php @@ -65,6 +65,15 @@ public function getDateTime(string $name, callable $callback, int $secondsValid return new DateTimeImmutable($value); } + public function getArray(string $name, callable $callback, int $secondsValid = self::DEFAULT_SECONDS_VALID):array { + $value = $this->get($name, $callback, $secondsValid); + if(!is_array($value)) { + throw new TypeError("Value with key '$name' is not an array"); + } + + return $value; + } + /** * @template T * @param class-string $name diff --git a/test/phpunit/CacheTest.php b/test/phpunit/CacheTest.php index 35736d6..c3292c3 100644 --- a/test/phpunit/CacheTest.php +++ b/test/phpunit/CacheTest.php @@ -97,6 +97,24 @@ public function testGetInstance():void { self::assertSame($value->name, $class->name); } + public function testGetArray():void { + $value = [1, 2, 3]; + $sut = $this->getSut([ + "numbers" => $value, + ]); + self::assertSame($value, $sut->getArray("numbers", fn() => [])); + } + + public function testGetArray_notArray():void { + $value = (object)[1, 2, 3]; + $sut = $this->getSut([ + "numbers" => $value, + ]); + self::expectException(\TypeError::class); + self::expectExceptionMessage("Value with key 'numbers' is not an array"); + $sut->getArray("numbers", fn() => []); + } + private function getSut(array $mockFiles = []):Cache { $mockFileAccess = null; if(!empty($mockFiles)) {