• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

elephox-dev / framework / 4877852653

pending completion
4877852653

push

github

Ricardo Boss
WIP

38 of 38 new or added lines in 6 files covered. (100.0%)

3863 of 5835 relevant lines covered (66.2%)

8.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.68
/modules/Http/src/ParameterMap.php
1
<?php
2
declare(strict_types=1);
3

4
namespace Elephox\Http;
5

6
use Elephox\Collection\ArrayMap;
7
use Elephox\Collection\OffsetNotAllowedException;
8
use Elephox\Collection\OffsetNotFoundException;
9
use Elephox\OOR\Casing;
10

11
/**
12
 * @extends ArrayMap<string, string|list<string>>
13
 */
14
class HeaderMap extends ArrayMap implements Contract\HeaderMap
15
{
16
        /**
17
         * @param array<string, string|list<string>>|null $server
18
         */
19
        public static function fromGlobals(?array $server = null): Contract\HeaderMap
20
        {
21
                $server ??= $_SERVER;
22

23
                $map = new self();
24

25
                /**
26
                 * @var string|list<string> $value
27
                 */
28
                foreach ($server as $name => $value) {
29
                        if (!str_starts_with($name, 'HTTP_')) {
30
                                continue;
31
                        }
32

33
                        $name = Casing::toHttpHeader(substr($name, 5));
34

35
                        $map->put($name, is_array($value) ? $value : [$value]);
36
                }
37

38
                return $map;
39
        }
40

41
        public static function compareHeaderNames(string $a, string $b): bool
42
        {
43
                return strcasecmp($a, $b) === 0;
44
        }
45

46
        public function containsKey(mixed $key, ?callable $comparer = null): bool
47
        {
48
                $validKey = $this->validateKey($key);
49

50
                /** @var null|callable(string, string): bool $comparer */
51
                $comparer ??= self::compareHeaderNames(...);
52

53
                return parent::containsKey($validKey, $comparer);
54
        }
55

56
        protected function validateKey(mixed $key): string
57
        {
58
                if ($key instanceof HeaderName) {
59
                        return $key->value;
60
                }
61

62
                if (is_string($key)) {
63
                        return $key;
64
                }
65

66
                throw new OffsetNotAllowedException($key);
67
        }
68

69
        public function get(mixed $key): array
70
        {
71
                $validKey = $this->validateKey($key);
72

73
                foreach ($this->items as $k => $v) {
74
                        if (self::compareHeaderNames($k, $validKey)) {
75
                                return $v;
76
                        }
77
                }
78

79
                throw new OffsetNotFoundException($key);
80
        }
81

82
        public function put(mixed $key, mixed $value): bool
83
        {
84
                $validKey = $this->validateKey($key);
85

86
                $existed = $this->has($validKey);
87
                if ($existed) {
88
                        foreach (array_keys($this->items) as $k) {
89
                                if (self::compareHeaderNames($k, $validKey)) {
90
                                        $this->items[$k] = $value;
91
                                }
92
                        }
93
                } else {
94
                        $this->items[$validKey] = $value;
95
                }
96

97
                return $existed;
98
        }
99

100
        public function has(mixed $key): bool
101
        {
102
                $validKey = $this->validateKey($key);
103

104
                if (parent::has($validKey)) {
105
                        return true;
106
                }
107

108
                return $this->containsKey($validKey);
109
        }
110

111
        public function remove(mixed $key): bool
112
        {
113
                $validKey = $this->validateKey($key);
114

115
                if (!$this->has($validKey)) {
116
                        return false;
117
                }
118

119
                $anyUnset = false;
120
                foreach (array_keys($this->items) as $k) {
121
                        if (self::compareHeaderNames($k, $validKey)) {
122
                                unset($this->items[$k]);
123

124
                                $anyUnset = true;
125
                        }
126
                }
127

128
                return $anyUnset;
129
        }
130
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc