1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace BEAR\Resource; |
||
6 | |||
7 | use Override; |
||
8 | use Ray\Di\InjectorInterface; |
||
9 | use Ray\WebContextParam\Annotation\AbstractWebContextParam; |
||
10 | |||
11 | use function assert; |
||
12 | use function is_string; |
||
13 | |||
14 | final class AssistedWebContextParam implements ParamInterface |
||
15 | { |
||
16 | /** |
||
17 | * $GLOBALS for testing |
||
18 | * |
||
19 | * @var array<string, array<string, mixed>> |
||
20 | */ |
||
21 | private static array $globals = []; |
||
22 | |||
23 | public function __construct( |
||
24 | private readonly AbstractWebContextParam $webContextParam, |
||
25 | private readonly ParamInterface $defaultParam, |
||
26 | ) { |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * {@inheritDoc} |
||
31 | */ |
||
32 | #[Override] |
||
33 | public function __invoke(string $varName, array $query, InjectorInterface $injector) |
||
34 | { |
||
35 | $superGlobals = static::$globals ?: $GLOBALS; |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
36 | /** @var array<string, array<string, string>> $superGlobals */ |
||
37 | $webContextParam = $this->webContextParam; |
||
38 | assert(is_string($webContextParam::GLOBAL_KEY)); |
||
39 | /** @psalm-suppress MixedArrayOffset */ |
||
40 | $phpWebContext = $superGlobals[$webContextParam::GLOBAL_KEY]; |
||
41 | |||
42 | return $phpWebContext[$this->webContextParam->key] ?? ($this->defaultParam)($varName, $query, $injector); |
||
43 | } |
||
44 | |||
45 | /** @param array<string, array<string, mixed>> $globals */ |
||
46 | public static function setSuperGlobalsOnlyForTestingPurpose(array $globals): void |
||
47 | { |
||
48 | self::$globals = $globals; |
||
49 | } |
||
50 | } |
||
51 |