Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
21.05% covered (danger)
21.05%
4 / 19
25.00% covered (danger)
25.00%
2 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
ArrayCache
21.05% covered (danger)
21.05%
4 / 19
25.00% covered (danger)
25.00%
2 / 8
96.16
0.00% covered (danger)
0.00%
0 / 1
 get
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 set
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 delete
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 clear
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getMultiple
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 setMultiple
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
20
 deleteMultiple
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 has
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace LambdaTwelve\OneRecord\Testing;
6
7use DateInterval;
8use Psr\SimpleCache\CacheInterface;
9
10/**
11 * A PSR-16 cache in an array, ignoring TTLs, counting writes.
12 */
13final class ArrayCache implements CacheInterface
14{
15    /** @var array<string, mixed> */
16    public array $items = [];
17
18    public int $writes = 0;
19
20    public function get(string $key, mixed $default = null): mixed
21    {
22        return $this->items[$key] ?? $default;
23    }
24
25    public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool
26    {
27        $this->items[$key] = $value;
28        $this->writes++;
29
30        return true;
31    }
32
33    public function delete(string $key): bool
34    {
35        unset($this->items[$key]);
36
37        return true;
38    }
39
40    public function clear(): bool
41    {
42        $this->items = [];
43
44        return true;
45    }
46
47    public function getMultiple(iterable $keys, mixed $default = null): iterable
48    {
49        $out = [];
50        foreach ($keys as $key) {
51            $out[$key] = $this->get($key, $default);
52        }
53
54        return $out;
55    }
56
57    /**
58     * @param iterable<mixed, mixed> $values
59     */
60    public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool
61    {
62        foreach ($values as $key => $value) {
63            $this->set(\is_string($key) ? $key : (string) (\is_scalar($key) ? $key : ''), $value, $ttl);
64        }
65
66        return true;
67    }
68
69    public function deleteMultiple(iterable $keys): bool
70    {
71        foreach ($keys as $key) {
72            $this->delete($key);
73        }
74
75        return true;
76    }
77
78    public function has(string $key): bool
79    {
80        return \array_key_exists($key, $this->items);
81    }
82}