Skip to content

Commit afd8f71

Browse files
authored
Merge pull request #58 from phpgt/56-get-array
feature: getArray
2 parents 5338427 + d6a99a7 commit afd8f71

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/Cache.php

+9
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ public function getDateTime(string $name, callable $callback, int $secondsValid
6565
return new DateTimeImmutable($value);
6666
}
6767

68+
public function getArray(string $name, callable $callback, int $secondsValid = self::DEFAULT_SECONDS_VALID):array {
69+
$value = $this->get($name, $callback, $secondsValid);
70+
if(!is_array($value)) {
71+
throw new TypeError("Value with key '$name' is not an array");
72+
}
73+
74+
return $value;
75+
}
76+
6877
/**
6978
* @template T
7079
* @param class-string<T> $name

test/phpunit/CacheTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,24 @@ public function testGetInstance():void {
9797
self::assertSame($value->name, $class->name);
9898
}
9999

100+
public function testGetArray():void {
101+
$value = [1, 2, 3];
102+
$sut = $this->getSut([
103+
"numbers" => $value,
104+
]);
105+
self::assertSame($value, $sut->getArray("numbers", fn() => []));
106+
}
107+
108+
public function testGetArray_notArray():void {
109+
$value = (object)[1, 2, 3];
110+
$sut = $this->getSut([
111+
"numbers" => $value,
112+
]);
113+
self::expectException(\TypeError::class);
114+
self::expectExceptionMessage("Value with key 'numbers' is not an array");
115+
$sut->getArray("numbers", fn() => []);
116+
}
117+
100118
private function getSut(array $mockFiles = []):Cache {
101119
$mockFileAccess = null;
102120
if(!empty($mockFiles)) {

0 commit comments

Comments
 (0)