WordPress\AiClient\Providers

ProviderRegistry::createDefaultProviderRequestAuthenticationprivateWP 0.1.0

Creates a default request authentication instance for a provider.

Метод класса: ProviderRegistry{}

Хуков нет.

Возвращает

?RequestAuthenticationInterface. The default request authentication instance, or null if not required or if no credential data can be found.

Использование

// private - только в коде основоного (родительского) класса
$result = $this->createDefaultProviderRequestAuthentication( $className ): ?RequestAuthenticationInterface;
$className(class-string) (обязательный)
The provider class name.

Список изменений

С версии 0.1.0 Введена.

Код ProviderRegistry::createDefaultProviderRequestAuthentication() WP 7.0

private function createDefaultProviderRequestAuthentication(string $className): ?RequestAuthenticationInterface
{
    $providerMetadata = $className::metadata();
    $providerId = $providerMetadata->getId();
    $authenticationMethod = $providerMetadata->getAuthenticationMethod();
    if ($authenticationMethod === null) {
        return null;
    }
    $authenticationClass = $authenticationMethod->getImplementationClass();
    if ($authenticationClass === null) {
        return null;
    }
    $authenticationSchema = $authenticationClass::getJsonSchema();
    // Iterate over all JSON schema object properties to try to determine the necessary authentication data.
    $authenticationData = [];
    if (isset($authenticationSchema['properties']) && is_array($authenticationSchema['properties'])) {
        /** @var array<string, mixed> $details */
        foreach ($authenticationSchema['properties'] as $property => $details) {
            $envVarName = $this->getEnvVarName($providerId, $property);
            // Try to get the value from environment variable or constant.
            $envValue = getenv($envVarName);
            if ($envValue === \false) {
                if (!defined($envVarName)) {
                    continue;
                    // Skip if neither environment variable nor constant is defined.
                }
                $envValue = constant($envVarName);
                if (!is_scalar($envValue)) {
                    continue;
                }
            }
            if (isset($details['type'])) {
                switch ($details['type']) {
                    case 'boolean':
                        $authenticationData[$property] = filter_var($envValue, \FILTER_VALIDATE_BOOLEAN);
                        break;
                    case 'number':
                        $authenticationData[$property] = (int) $envValue;
                        break;
                    case 'string':
                    default:
                        $authenticationData[$property] = (string) $envValue;
                }
            } else {
                // Default to string if no type is specified.
                $authenticationData[$property] = (string) $envValue;
            }
        }
        // If any required fields are missing, return null to avoid immediate errors.
        if (isset($authenticationSchema['required']) && is_array($authenticationSchema['required'])) {
            /** @var list<string> $requiredProperties */
            $requiredProperties = $authenticationSchema['required'];
            if (array_diff_key(array_flip($requiredProperties), $authenticationData)) {
                return null;
            }
        }
    }
    /** @var RequestAuthenticationInterface */
    /** @var array<string, mixed> $authenticationData */
    return $authenticationClass::fromArray($authenticationData);
}