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

elephox-dev / framework / 4836160616

pending completion
4836160616

push

github

Ricardo Boss
Updated dependencies

3635 of 5256 relevant lines covered (69.16%)

9.01 hits per line

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

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

4
namespace Elephox\Http;
5

6
use Elephox\OOR\Casing;
7
use InvalidArgumentException;
8
use JetBrains\PhpStorm\Immutable;
9
use JetBrains\PhpStorm\Pure;
10
use Psr\Http\Message\StreamInterface;
11
use Psr\Http\Message\UriInterface;
12
use RuntimeException;
13

14
#[Immutable]
15
class Request extends AbstractMessage implements Contract\Request
16
{
17
        #[Pure]
18
        public static function build(): Contract\RequestBuilder
19
        {
20
                return new RequestBuilder();
21
        }
22

23
        #[Pure]
24
        public function __construct(
25
                string $protocolVersion,
26
                Contract\HeaderMap $headers,
27
                StreamInterface $body,
28
                public readonly Contract\RequestMethod $method,
29
                public readonly Url $url,
30
        ) {
31
                parent::__construct($protocolVersion, $headers, $body);
32
        }
33

34
        #[Pure]
35
        public function with(): Contract\RequestBuilder
36
        {
37
                /** @psalm-suppress ImpureMethodCall */
38
                return new RequestBuilder(
39
                        $this->protocolVersion,
40
                        new HeaderMap($this->headers->toArray()),
41
                        $this->body,
42
                        $this->method,
43
                        $this->url,
44
                );
45
        }
46

47
        #[Pure]
48
        public function getRequestMethod(): Contract\RequestMethod
49
        {
50
                return $this->method;
51
        }
52

53
        #[Pure]
54
        public function getUrl(): Url
55
        {
56
                return $this->url;
57
        }
58

59
        #[Pure]
60
        public function getRequestTarget(): string
61
        {
62
                $target = $this->getUrl()->getPath();
63
                if ($target === '') {
64
                        $target = '/';
65
                }
66

67
                $query = $this->getUrl()->getQuery();
68
                if ($query !== '') {
69
                        $target .= '?' . $query;
70
                }
71

72
                return $target;
73
        }
74

75
        #[Pure]
76
        public function withRequestTarget(string $requestTarget): never
77
        {
78
                throw new RuntimeException(__METHOD__ . ' is not implemented');
79
        }
80

81
        #[Pure]
82
        public function withMethod(string $method): static
83
        {
84
                if ($method === '') {
85
                        throw new InvalidArgumentException('Expected non-empty-string, but got an empty string instead.');
86
                }
87

88
                $requestMethod = null;
89
                if (Casing::toUpper($method) === $method) {
90
                        /** @var \Elephox\Http\Contract\RequestMethod $requestMethod */
91
                        $requestMethod = RequestMethod::tryFrom($method);
92
                }
93

94
                $requestMethod ??= new CustomRequestMethod($method);
95

96
                /**
97
                 * @psalm-suppress ImpureMethodCall
98
                 *
99
                 * @var static
100
                 */
101
                return $this->with()->requestMethod($requestMethod)->get();
102
        }
103

104
        #[Pure]
105
        public function getUri(): UriInterface
106
        {
107
                return $this->url;
108
        }
109

110
        #[Pure]
111
        public function withUri(UriInterface $uri, bool $preserveHost = false): static
112
        {
113
                /**
114
                 * @psalm-suppress ImpureMethodCall
115
                 *
116
                 * @var static
117
                 */
118
                return $this->with()->requestUrl(Url::fromString((string) $uri), $preserveHost)->get();
119
        }
120

121
        #[Pure]
122
        public function getMethod(): string
123
        {
124
                return $this->getRequestMethod()->getValue();
125
        }
126
}
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