Google\Cloud\Speech\SpeechClient::formatRequest PHP Метод

formatRequest() приватный Метод

Formats the request for the API.
private formatRequest ( resource | string | StorageObject $audio, array $options ) : array
$audio resource | string | Google\Cloud\Storage\StorageObject
$options array
Результат array
    private function formatRequest($audio, array $options)
    {
        $analyzedFileInfo = null;
        $fileFormat = null;
        $recognizeOptions = ['encoding', 'sampleRate', 'languageCode', 'maxAlternatives', 'profanityFilter', 'speechContext'];
        if ($audio instanceof StorageObject) {
            $objIdentity = $audio->identity();
            $options['audio']['uri'] = 'gs://' . $objIdentity['bucket'] . '/' . $objIdentity['object'];
            $fileFormat = pathinfo($options['audio']['uri'], PATHINFO_EXTENSION);
        } elseif (is_resource($audio)) {
            $options['audio']['content'] = base64_encode(stream_get_contents($audio));
            $fileFormat = pathinfo(stream_get_meta_data($audio)['uri'], PATHINFO_EXTENSION);
        } else {
            $options['audio']['content'] = base64_encode($audio);
        }
        if (isset($options['encoding'])) {
            $options['encoding'] = strtoupper($options['encoding']);
        } else {
            $analyzedFileInfo = $this->analyzeAudio($audio);
            $options['encoding'] = isset($analyzedFileInfo['fileformat']) ? $this->determineEncoding($analyzedFileInfo['fileformat']) : $this->determineEncoding($fileFormat);
        }
        if (isset($options['sampleRate'])) {
            $options['sampleRate'] = (int) $options['sampleRate'];
        } else {
            if (!$analyzedFileInfo) {
                $analyzedFileInfo = $this->analyzeAudio($audio);
            }
            $options['sampleRate'] = isset($analyzedFileInfo['audio']['sample_rate']) ? $analyzedFileInfo['audio']['sample_rate'] : $this->determineSampleRate($options['encoding']);
        }
        if (!$options['encoding']) {
            throw new \InvalidArgumentException('Unable to determine encoding. Please provide the value manually.');
        }
        if (!$options['sampleRate']) {
            throw new \InvalidArgumentException('Unable to determine sample rate. Please provide the value manually.');
        }
        foreach ($options as $option => $value) {
            if (in_array($option, $recognizeOptions)) {
                $options['config'][$option] = $value;
                unset($options[$option]);
            }
        }
        return $options;
    }