11<?php
22
33/**
4- * Spiral Framework .
4+ * This file is part of RoadRunner GRPC package .
55 *
6- * @ license MIT
7- * @author Anton Titov (Wolfy-J)
6+ * For the full copyright and license information, please view the LICENSE
7+ * file that was distributed with this source code.
88 */
99
1010declare (strict_types=1 );
1111
1212namespace Spiral \GRPC ;
1313
14- final class Context implements ContextInterface
14+ /**
15+ * @template-implements \IteratorAggregate<string, mixed>
16+ * @template-implements \ArrayAccess<string, mixed>
17+ */
18+ final class Context implements ContextInterface, \IteratorAggregate, \Countable, \ArrayAccess
1519{
16- /** @var array */
20+ /**
21+ * @var array<string, mixed>
22+ */
1723 private $ values ;
1824
1925 /**
20- * @param array $values
26+ * @param array<string, mixed> $values
2127 */
2228 public function __construct (array $ values )
2329 {
2430 $ this ->values = $ values ;
2531 }
2632
2733 /**
28- * @inheritdoc
34+ * {@inheritDoc}
2935 */
3036 public function withValue (string $ key , $ value ): ContextInterface
3137 {
@@ -36,18 +42,82 @@ public function withValue(string $key, $value): ContextInterface
3642 }
3743
3844 /**
39- * @inheritdoc
45+ * {@inheritDoc}
46+ * @param mixed|null $default
4047 */
41- public function getValue (string $ key )
48+ public function getValue (string $ key, $ default = null )
4249 {
43- return $ this ->values [$ key ] ?? null ;
50+ return $ this ->values [$ key ] ?? $ default ;
4451 }
4552
4653 /**
47- * @inheritdoc
54+ * {@inheritDoc}
4855 */
4956 public function getValues (): array
5057 {
5158 return $ this ->values ;
5259 }
60+
61+ /**
62+ * {@inheritDoc}
63+ */
64+ public function offsetExists ($ offset ): bool
65+ {
66+ assert (\is_string ($ offset ), 'Offset argument must be a type of string ' );
67+
68+ /**
69+ * Note: PHP Opcode optimisation
70+ * @see https://www.php.net/manual/pt_BR/internals2.opcodes.isset-isempty-var.php
71+ *
72+ * Priority use `ZEND_ISSET_ISEMPTY_VAR !0` opcode instead of `DO_FCALL 'array_key_exists'`.
73+ */
74+ return isset ($ this ->values [$ offset ]) || \array_key_exists ($ offset , $ this ->values );
75+ }
76+
77+ /**
78+ * {@inheritDoc}
79+ */
80+ public function offsetGet ($ offset )
81+ {
82+ assert (\is_string ($ offset ), 'Offset argument must be a type of string ' );
83+
84+ return $ this ->values [$ offset ] ?? null ;
85+ }
86+
87+ /**
88+ * {@inheritDoc}
89+ */
90+ public function offsetSet ($ offset , $ value ): void
91+ {
92+ assert (\is_string ($ offset ), 'Offset argument must be a type of string ' );
93+
94+ $ this ->values [$ offset ] = $ value ;
95+ }
96+
97+ /**
98+ * {@inheritDoc}
99+ */
100+ public function offsetUnset ($ offset ): void
101+ {
102+ assert (\is_string ($ offset ), 'Offset argument must be a type of string ' );
103+
104+ unset($ this ->values [$ offset ]);
105+ }
106+
107+
108+ /**
109+ * {@inheritDoc}
110+ */
111+ public function getIterator (): \Traversable
112+ {
113+ return new \ArrayIterator ($ this ->values );
114+ }
115+
116+ /**
117+ * {@inheritDoc}
118+ */
119+ public function count (): int
120+ {
121+ return \count ($ this ->values );
122+ }
53123}
0 commit comments