1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
namespace Zewa\HTTP; |
4
|
|
|
|
5
|
|
|
use Zewa\Interfaces\HTTP\GlobalInterface; |
6
|
|
|
use Zewa\Container; |
7
|
|
|
use Zewa\Security; |
8
|
|
|
|
9
|
|
|
abstract class SuperGlobal implements GlobalInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var Container |
13
|
|
|
*/ |
14
|
|
|
protected $container; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Security |
18
|
|
|
*/ |
19
|
|
|
protected $security; |
20
|
|
|
|
21
|
32 |
|
public function __construct(Container $container, Security $security) |
22
|
|
|
{ |
23
|
32 |
|
$this->container = $container; |
24
|
32 |
|
$this->security = $security; |
25
|
32 |
|
} |
26
|
|
|
|
27
|
4 |
|
public function set(string $key, $value) |
28
|
|
|
{ |
29
|
4 |
|
$global = $this->getGlobalName(); |
30
|
4 |
|
$container = $this->container->get($global); |
31
|
4 |
|
$container[$key] = $value; |
32
|
4 |
|
$this->container->set($global, $container); |
33
|
4 |
|
} |
34
|
|
|
|
35
|
6 |
|
public function fetch(string $key = null, $default = null) |
36
|
|
|
{ |
37
|
6 |
|
$global = $this->getGlobalName(); |
38
|
6 |
|
if ($key === null) { |
39
|
1 |
|
return empty($this->container->get($global)) === true ? null : $this->container->get($global); |
40
|
|
|
} |
41
|
|
|
|
42
|
5 |
|
return $this->container->has($global) ? $this->container->get($global)[$key] ?? $default : null; |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
public function remove(string $key) |
46
|
|
|
{ |
47
|
1 |
|
$global = $this->getGlobalName(); |
48
|
1 |
|
if ($this->container->has($global)) { |
49
|
1 |
|
$this->processRemoval($key); |
50
|
|
|
} |
51
|
1 |
|
} |
52
|
|
|
|
53
|
32 |
|
private function getGlobalName() : string |
54
|
|
|
{ |
55
|
32 |
|
return (new \ReflectionClass($this))->getShortName(); |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
private function processRemoval(string $key) |
59
|
|
|
{ |
60
|
1 |
|
$global = $this->getGlobalName(); |
61
|
1 |
|
$container = $this->container->get($global); |
62
|
1 |
|
if (isset($container[$key])) { |
63
|
1 |
|
unset($container[$key]); |
64
|
1 |
|
$this->container->set($global, $container); |
65
|
|
|
} |
66
|
1 |
|
} |
67
|
|
|
|
68
|
32 |
|
protected function registerGlobal($value) |
69
|
|
|
{ |
70
|
32 |
|
$value = $this->security->normalize($value); |
71
|
32 |
|
$this->container->set($this->getGlobalName(), $value); |
72
|
32 |
|
} |
73
|
|
|
} |
74
|
|
|
|