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