src/ChrisKonnertz/DeepLy/DeepLy.php
protected function requestTranslation($text, $to = self::LANG_EN, $from = self::LANG_AUTO)
    {
        $this->translationBag = null;

        if (! is_string($text) and ! is_array($text)) {
            throw new \InvalidArgumentException('The $text argument has to be a string or an array');
        }
        if (is_array($text)) {
            foreach ($text as $index => $sentence) {
                if (! is_string($sentence)) {
                    throw new \InvalidArgumentException(
                        'The $text argument has to be a string or an array of strings. '.
                        'The '.(++$index).'. item of the array is not a string.'
                    );
                }
                // TODO Ensure that the limit is per sentence
                if ($this->validateTextLength and mb_strlen($sentence) > self::MAX_TRANSLATION_TEXT_LEN) {
                    throw new \InvalidArgumentException(
                        'The '.(++$index).'. sentence exceeds the maximum of '.self::MAX_TRANSLATION_TEXT_LEN.' chars'
                    );
                }
            }
        } else {
            if ($this->validateTextLength and mb_strlen($text) > self::MAX_TRANSLATION_TEXT_LEN) {
                throw new \InvalidArgumentException(
                    'The text exceeds the maximum of '.self::MAX_TRANSLATION_TEXT_LEN.' chars'
                );
            }
        }
        if (! is_string($to)) {
            throw new \InvalidArgumentException('The $to argument has to be a string');
        }
        if (! in_array($to, self::LANG_CODES)) {
            throw new \InvalidArgumentException('The $to argument has to be a valid language code');
        }
        if ($to === self::LANG_AUTO) {
            throw new \InvalidArgumentException('The $to argument cannot be "'.self::LANG_AUTO.'"');
        }
        if (! is_string($from)) {
            throw new \InvalidArgumentException('The $from argument has to be a string');
        }
        if (! in_array($from, self::LANG_CODES)) {
            throw new \InvalidArgumentException('The $from argument has to a valid language code');
        }

        // Note that this array will be converted to a data structure of arrays AND objects later on
        $params = [
            'lang' => [
                'source_lang_user_selected' => $from, // Attention: source_lang does not work!
                'target_lang' => $to,
            ]
        ];

        if (is_array($text)) {
            $lines = $text;
            $sentences = [];

            foreach ($text as $sentence) {
                $sentences[] = [$sentence];
            }
        } else {
            // We try to auto-detect which is the right line break
            $lineBreak = "\r\n";
            if (strpos($text, $lineBreak) === false and strpos($text, "\n") !== false) {
                $lineBreak = "\n";
            }

            $lines = explode($lineBreak, $text);

            $sentencesBag = $this->requestSplitText($lines, $from);
            $sentences = $sentencesBag->getAllSentencesGrouped();
        }

        $params['jobs'] = [];
        $lineBreaks = [];
        foreach ($lines as $index => $line) {
            if ($index > 0) {
                $lineBreaks[] = sizeof($params['jobs']);
            }

            foreach ($sentences[$index] as $sentence) {
                $params['jobs'][] =  [
                    'kind' => 'default',
                    'raw_en_sentence' => $sentence,
                ];
            }
        }

        // The API call might throw an exception but we do not want to catch it,
        // instead the caller of this method has to catch it.
        $rawResponseData = $this->httpClient->callApi(self::API_BASE_URL, $params, self::METHOD_TRANSLATE);

        $responseContent = $this->protocol->processResponseData($rawResponseData);

        $translationBag = new TranslationBag($responseContent, $lineBreaks);

        $this->translationBag = $translationBag;

        return $translationBag;
    }
protected function requestTranslation($text, $to = self::LANG_EN, $from = self::LANG_AUTO)
    {
        $this->translationBag = null;

        if (! is_string($text) and ! is_array($text)) {
            throw new \InvalidArgumentException('The $text argument has to be a string or an array');
        }
        if (is_array($text)) {
            foreach ($text as $index => $sentence) {
                if (! is_string($sentence)) {
                    throw new \InvalidArgumentException(
                        'The $text argument has to be a string or an array of strings. '.
                        'The '.(++$index).'. item of the array is not a string.'
                    );
                }
                // TODO Ensure that the limit is per sentence
                if ($this->validateTextLength and mb_strlen($sentence) > self::MAX_TRANSLATION_TEXT_LEN) {
                    throw new \InvalidArgumentException(
                        'The '.(++$index).'. sentence exceeds the maximum of '.self::MAX_TRANSLATION_TEXT_LEN.' chars'
                    );
                }
            }
        } else {
            if ($this->validateTextLength and mb_strlen($text) > self::MAX_TRANSLATION_TEXT_LEN) {
                throw new \InvalidArgumentException(
                    'The text exceeds the maximum of '.self::MAX_TRANSLATION_TEXT_LEN.' chars'
                );
            }
        }
        if (! is_string($to)) {
            throw new \InvalidArgumentException('The $to argument has to be a string');
        }
        if (! in_array($to, self::LANG_CODES)) {
            throw new \InvalidArgumentException('The $to argument has to be a valid language code');
        }
        if ($to === self::LANG_AUTO) {
            throw new \InvalidArgumentException('The $to argument cannot be "'.self::LANG_AUTO.'"');
        }
        if (! is_string($from)) {
            throw new \InvalidArgumentException('The $from argument has to be a string');
        }
        if (! in_array($from, self::LANG_CODES)) {
            throw new \InvalidArgumentException('The $from argument has to a valid language code');
        }

        // Note that this array will be converted to a data structure of arrays AND objects later on
        $params = [
            'lang' => [
                'source_lang_user_selected' => $from, // Attention: source_lang does not work!
                'target_lang' => $to,
            ]
        ];

        if (is_array($text)) {
            $lines = $text;
            $sentences = [];

            foreach ($text as $sentence) {
                $sentences[] = [$sentence];
            }
        } else {
            // We try to auto-detect which is the right line break
            $lineBreak = "\r\n";
            if (strpos($text, $lineBreak) === false and strpos($text, "\n") !== false) {
                $lineBreak = "\n";
            }

            $lines = explode($lineBreak, $text);

            $sentencesBag = $this->requestSplitText($lines, $from);
            $sentences = $sentencesBag->getAllSentencesGrouped();
        }

        $params['jobs'] = [];
        $lineBreaks = [];
        foreach ($lines as $index => $line) {
            if ($index > 0) {
                $lineBreaks[] = sizeof($params['jobs']);
            }

            foreach ($sentences[$index] as $sentence) {
                $params['jobs'][] =  [
                    'kind' => 'default',
                    'raw_en_sentence' => $sentence,
                ];
            }
        }

        // The API call might throw an exception but we do not want to catch it,
        // instead the caller of this method has to catch it.
        $rawResponseData = $this->httpClient->callApi(self::API_BASE_URL, $params, self::METHOD_TRANSLATE);

        $responseContent = $this->protocol->processResponseData($rawResponseData);

        $translationBag = new TranslationBag($responseContent, $lineBreaks);

        $this->translationBag = $translationBag;

        return $translationBag;
    }
Requests a translation from the API. Returns a TranslationBag object.
ATTENTION: The target language parameter is followed by the source language parameter!
This method might throw an exception so you should wrap it in a try-catch-block.
You may use the translate() method if you want to get the result as a string.

@param string|string[] $text           The text to translate. A single string or an array of sentences (strings)
@param string          $to             Optional: The target language, a self::LANG_<code> constant
@param string|null     $from           Optional: The source language, a self::LANG_<code> constant
@return TranslationBag
@throws \Exception