-
Notifications
You must be signed in to change notification settings - Fork 1
/
InMemoryStorage.php
69 lines (53 loc) · 1.6 KB
/
InMemoryStorage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
namespace phputil\flags;
/**
* In-memory storage. Useful for testing purposes.
*/
class InMemoryStorage implements FlagStorage {
/** @var array<string, FlagData> */
private array $flags = [];
private int $lastId = 0;
public function isEnabled( string $key ): bool {
$flag = $this->get( $key );
return $flag === null ? false : $flag->enabled;
}
/** @inheritDoc */
public function get( string $key ): ?FlagData {
return $this->flags[ $key ] ?? null;
}
/** @inheritDoc */
public function touch( string $key, ?bool $enabled = null ): ?FlagData {
$flag = $this->get( $key ) ??
new FlagData( $key, false, new FlagMetadata( ++$this->lastId ) );
$this->set( $key, $flag );
if ( $enabled !== null ) {
$flag->enabled = $enabled;
}
return $flag->updateAccess();
}
/** @inheritDoc */
public function set( string $key, FlagData $flag ): bool {
$this->flags[ $key ] = $flag;
return true;
}
/** @inheritDoc */
public function remove( string $key ): bool {
if ( ! isset( $this->flags[ $key ] ) ) {
return false;
}
unset( $this->flags[ $key ] );
return true;
}
/** @inheritDoc */
public function removeAll(): void {
$this->flags = [];
}
/** @inheritDoc */
public function getAll( array $options = [] ): array {
return array_values( $this->flags );
}
/** @inheritDoc */
public function count( array $options = [] ): int {
return count( $this->flags );
}
}