forked from pavlokomarov/roach-php-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequest.php
More file actions
117 lines (95 loc) · 2.7 KB
/
Request.php
File metadata and controls
117 lines (95 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
declare(strict_types=1);
/**
* Copyright (c) 2021 Kai Sassnowski
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/roach-php/roach
*/
namespace RoachPHP\Http;
use Closure;
use Generator;
use GuzzleHttp\Psr7\Request as GuzzleRequest;
use Psr\Http\Message\RequestInterface;
use RoachPHP\Spider\ParseResult;
use RoachPHP\Support\Droppable;
use RoachPHP\Support\DroppableInterface;
use RoachPHP\Support\HasMetaData;
final class Request implements DroppableInterface
{
use HasMetaData;
use Droppable;
/**
* @var Closure(Response): Generator<ParseResult>
*/
private Closure $parseCallback;
private RequestInterface $psrRequest;
/**
* An array of Guzzle request options.
* See https://docs.guzzlephp.org/en/stable/request-options.html
*/
private array $options;
/**
* @param callable(Response): Generator<ParseResult> $parseMethod
*/
public function __construct(string $method, string $uri, callable $parseMethod, array $options = [])
{
$this->options = $options;
$this->psrRequest = new GuzzleRequest($method, $uri);
$this->parseCallback = Closure::fromCallable($parseMethod);
}
public function getUri(): string
{
return (string) $this->psrRequest->getUri();
}
public function hasHeader(string $name): bool
{
return $this->psrRequest->hasHeader($name);
}
public function getHeader(string $name): array
{
return $this->psrRequest->getHeader($name);
}
public function getPath(): string
{
return $this->psrRequest->getUri()->getPath();
}
/**
* @param string|string[] $value
*/
public function addHeader(string $name, mixed $value): self
{
/** @var GuzzleRequest $request */
$request = $this->psrRequest->withHeader($name, $value);
$clone = clone $this;
$clone->psrRequest = $request;
return $clone;
}
public function getOptions(): array
{
return $this->options;
}
public function addOption(string $option, mixed $value): self
{
$this->options[$option] = $value;
return $this;
}
/**
* @param Closure(RequestInterface): RequestInterface $callback
*/
public function withPsrRequest(Closure $callback): self
{
$this->psrRequest = $callback($this->psrRequest);
return $this;
}
public function callback(Response $response): Generator
{
return ($this->parseCallback)($response);
}
public function getPsrRequest(): RequestInterface
{
return $this->psrRequest;
}
}