[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/php-ai-client/src/Providers/Http/DTO/ -> ApiKeyRequestAuthentication.php (source)

   1  <?php
   2  
   3  declare (strict_types=1);
   4  namespace WordPress\AiClient\Providers\Http\DTO;
   5  
   6  use WordPress\AiClient\Common\AbstractDataTransferObject;
   7  use WordPress\AiClient\Providers\Http\Contracts\RequestAuthenticationInterface;
   8  /**
   9   * Class for HTTP request authentication using an API key.
  10   *
  11   * @since 0.1.0
  12   *
  13   * @phpstan-type ApiKeyRequestAuthenticationArrayShape array{
  14   *     apiKey: string
  15   * }
  16   *
  17   * @extends AbstractDataTransferObject<ApiKeyRequestAuthenticationArrayShape>
  18   */
  19  class ApiKeyRequestAuthentication extends AbstractDataTransferObject implements RequestAuthenticationInterface
  20  {
  21      public const KEY_API_KEY = 'apiKey';
  22      /**
  23       * @var string The API key used for authentication.
  24       */
  25      protected string $apiKey;
  26      /**
  27       * Constructor.
  28       *
  29       * @since 0.1.0
  30       *
  31       * @param string $apiKey The API key used for authentication.
  32       */
  33      public function __construct(string $apiKey)
  34      {
  35          $this->apiKey = $apiKey;
  36      }
  37      /**
  38       * {@inheritDoc}
  39       *
  40       * @since 0.1.0
  41       */
  42      public function authenticateRequest(\WordPress\AiClient\Providers\Http\DTO\Request $request): \WordPress\AiClient\Providers\Http\DTO\Request
  43      {
  44          // Add the API key to the request headers.
  45          return $request->withHeader('Authorization', 'Bearer ' . $this->apiKey);
  46      }
  47      /**
  48       * Gets the API key.
  49       *
  50       * @since 0.1.0
  51       *
  52       * @return string The API key.
  53       */
  54      public function getApiKey(): string
  55      {
  56          return $this->apiKey;
  57      }
  58      /**
  59       * {@inheritDoc}
  60       *
  61       * @since 0.1.0
  62       *
  63       * @since 0.1.0
  64       *
  65       * @return ApiKeyRequestAuthenticationArrayShape
  66       */
  67      public function toArray(): array
  68      {
  69          return [self::KEY_API_KEY => $this->apiKey];
  70      }
  71      /**
  72       * {@inheritDoc}
  73       *
  74       * @since 0.1.0
  75       *
  76       * @since 0.1.0
  77       */
  78      public static function fromArray(array $array): self
  79      {
  80          static::validateFromArrayData($array, [self::KEY_API_KEY]);
  81          return new self($array[self::KEY_API_KEY]);
  82      }
  83      /**
  84       * {@inheritDoc}
  85       *
  86       * @since 0.1.0
  87       */
  88      public static function getJsonSchema(): array
  89      {
  90          return ['type' => 'object', 'properties' => [self::KEY_API_KEY => ['type' => 'string', 'title' => 'API Key', 'description' => 'The API key used for authentication.']], 'required' => [self::KEY_API_KEY]];
  91      }
  92  }


Generated : Sat Jun 13 09:38:55 2026 Cross-referenced by PHPXref