-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSecret.php
104 lines (86 loc) · 2.09 KB
/
Secret.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
declare(strict_types=1);
/*
* @author Aaron Scherer <[email protected]>
* @date 2019
* @license https://opensource.org/licenses/MIT
*/
namespace Secretary;
use Secretary\Exception\ValueNotSupportedException;
/**
* @implements \ArrayAccess<string, mixed>
*/
class Secret implements \ArrayAccess
{
private string $key;
private array|string $value;
private ?array $metadata;
public function __construct(string $key, array|string $value, ?array $metadata = null)
{
$this->key = $key;
$this->value = $value;
$this->metadata = $metadata;
}
public function getKey(): string
{
return $this->key;
}
/**
* @return array|string
*/
public function getValue()
{
return $this->value;
}
public function getMetadata(): array
{
return $this->metadata ?? [];
}
/**
*/
public function offsetExists($offset): bool
{
return is_array($this->value) && array_key_exists($offset, $this->value);
}
/**
*
* @throws ValueNotSupportedException
*/
public function offsetGet($offset): mixed
{
if (!is_array($this->value)) {
throw new ValueNotSupportedException($this->key);
}
return $this->value[$offset];
}
/**
* @throws \Exception
*/
public function offsetSet(mixed $offset, mixed $value): void
{
throw new \Exception('Secrets are immutable');
}
/**
* @throws \Exception
*/
public function offsetUnset(mixed $offset): void
{
throw new \Exception('Secrets are immutable');
}
/**
* Returns a new instance of this secret with the value changed.
*
* @param array|string $value
*/
public function withValue($value): self
{
return new self($this->key, $value, $this->metadata);
}
/**
* Returns a new instance of this secret with the metadata changed.
*/
public function withMetadata(array $metadata): self
{
return new self($this->key, $this->value, $metadata);
}
}