-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathContext.php
More file actions
82 lines (65 loc) · 2.07 KB
/
Context.php
File metadata and controls
82 lines (65 loc) · 2.07 KB
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
<?php
declare(strict_types=1);
namespace Spiral\RoadRunner\GRPC;
/**
* @psalm-import-type TValues from ContextInterface
* @implements \IteratorAggregate<string, mixed>
* @implements \ArrayAccess<string, mixed>
*/
final class Context implements ContextInterface, \IteratorAggregate, \Countable, \ArrayAccess
{
/**
* @param TValues $values
*/
public function __construct(
private array $values,
) {}
public function withValue(string $key, mixed $value): ContextInterface
{
$ctx = clone $this;
$ctx->values[$key] = $value;
return $ctx;
}
public function getValue(string $key, mixed $default = null): mixed
{
return $this->values[$key] ?? $default;
}
public function getValues(): array
{
return $this->values;
}
public function offsetExists(mixed $offset): bool
{
\assert(\is_string($offset), 'Offset argument must be a type of string');
/**
* Note: PHP Opcode optimisation
* @see https://www.php.net/manual/pt_BR/internals2.opcodes.isset-isempty-var.php
*
* Priority use `ZEND_ISSET_ISEMPTY_VAR !0` opcode instead of `DO_FCALL 'array_key_exists'`.
*/
return isset($this->values[$offset]) || \array_key_exists($offset, $this->values);
}
public function offsetGet(mixed $offset): mixed
{
\assert(\is_string($offset), 'Offset argument must be a type of string');
return $this->values[$offset] ?? null;
}
public function offsetSet(mixed $offset, mixed $value): void
{
\assert(\is_string($offset), 'Offset argument must be a type of string');
$this->values[$offset] = $value;
}
public function offsetUnset(mixed $offset): void
{
\assert(\is_string($offset), 'Offset argument must be a type of string');
unset($this->values[$offset]);
}
public function getIterator(): \Traversable
{
return new \ArrayIterator($this->values);
}
public function count(): int
{
return \count($this->values);
}
}