| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 4 |
|
| 5 |
namespace Yoast\WP\SEO\AI\HTTP_Request\Domain; |
| 6 |
|
| 7 |
use InvalidArgumentException; |
| 8 |
|
| 9 |
/** |
| 10 |
* Class Request |
| 11 |
* Represents a request to the AI Generator API. |
| 12 |
*/ |
| 13 |
class Request { |
| 14 |
|
| 15 |
public const METHOD_GET = 'GET'; |
| 16 |
public const METHOD_POST = 'POST'; |
| 17 |
public const METHOD_DELETE = 'DELETE'; |
| 18 |
|
| 19 |
private const ALLOWED_METHODS = [ self::METHOD_GET, self::METHOD_POST, self::METHOD_DELETE ]; |
| 20 |
|
| 21 |
/** |
| 22 |
* The action path for the request. |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
private $action_path; |
| 27 |
|
| 28 |
/** |
| 29 |
* The body of the request. |
| 30 |
* |
| 31 |
* @var array<string> |
| 32 |
*/ |
| 33 |
private $body; |
| 34 |
|
| 35 |
/** |
| 36 |
* The headers for the request. |
| 37 |
* |
| 38 |
* @var array<string> |
| 39 |
*/ |
| 40 |
private $headers; |
| 41 |
|
| 42 |
/** |
| 43 |
* The HTTP method for the request. |
| 44 |
* |
| 45 |
* @var string |
| 46 |
*/ |
| 47 |
private $http_method; |
| 48 |
|
| 49 |
/** |
| 50 |
* Constructor for the Request class. |
| 51 |
* |
| 52 |
* @param string $action_path The action path for the request. |
| 53 |
* @param array<string> $body The body of the request. |
| 54 |
* @param array<string> $headers The headers for the request. |
| 55 |
* @param string $http_method The HTTP method for the request. One of the METHOD_* constants. Defaults to POST. |
| 56 |
* |
| 57 |
* @throws InvalidArgumentException When $http_method is not one of the supported METHOD_* constants. |
| 58 |
*/ |
| 59 |
public function __construct( string $action_path, array $body = [], array $headers = [], string $http_method = self::METHOD_POST ) { |
| 60 |
if ( ! \in_array( $http_method, self::ALLOWED_METHODS, true ) ) { |
| 61 |
// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- false positive. |
| 62 |
throw new InvalidArgumentException( "Unsupported HTTP method: $http_method" ); |
| 63 |
} |
| 64 |
|
| 65 |
$this->action_path = $action_path; |
| 66 |
$this->body = $body; |
| 67 |
$this->headers = $headers; |
| 68 |
$this->http_method = $http_method; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Get the action path for the request. |
| 73 |
* |
| 74 |
* @return string The action path for the request. |
| 75 |
*/ |
| 76 |
public function get_action_path(): string { |
| 77 |
return $this->action_path; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Get the body of the request. |
| 82 |
* |
| 83 |
* Returns null for an empty body: an empty PHP array is ambiguous once JSON-encoded (`[]` vs `{}`), |
| 84 |
* so an empty body is omitted from the request entirely rather than sent as an empty array, which |
| 85 |
* the AI service rejects. |
| 86 |
* |
| 87 |
* @return array<string>|null The body of the request, or null when there is no body to send. |
| 88 |
*/ |
| 89 |
public function get_body(): ?array { |
| 90 |
return ( $this->body === [] ) ? null : $this->body; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Get the headers for the request. |
| 95 |
* |
| 96 |
* @return array<string> The headers for the request. |
| 97 |
*/ |
| 98 |
public function get_headers(): array { |
| 99 |
return $this->headers; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Get the HTTP method for the request. |
| 104 |
* |
| 105 |
* @return string One of the METHOD_* constants. |
| 106 |
*/ |
| 107 |
public function get_http_method(): string { |
| 108 |
return $this->http_method; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Returns a copy of the request with the given headers merged in. |
| 113 |
* |
| 114 |
* @param array<string> $headers The headers to add. |
| 115 |
* |
| 116 |
* @return self The new request. |
| 117 |
*/ |
| 118 |
public function with_added_headers( array $headers ): self { |
| 119 |
return new self( |
| 120 |
$this->action_path, |
| 121 |
$this->body, |
| 122 |
\array_merge( $this->headers, $headers ), |
| 123 |
$this->http_method, |
| 124 |
); |
| 125 |
} |
| 126 |
} |
| 127 |
|