Completed
Push — master ( 58efe7...cda9ba )
by Byron
02:12
created

GetTranslationsArray::processResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * This file is part of the badams\MicrosoftTranslator library
4
 *
5
 * @license http://opensource.org/licenses/MIT
6
 * @link https://github.com/badams/microsoft-translator
7
 * @package badams/microsoft-translator
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace badams\MicrosoftTranslator\Methods;
14
15
use badams\MicrosoftTranslator\Exceptions\ArgumentException;
16
use badams\MicrosoftTranslator\Exceptions\UnsupportedLanguageException;
17
use badams\MicrosoftTranslator\Language;
18
use badams\MicrosoftTranslator\Responses\GetTranslationsResponse;
19
use badams\MicrosoftTranslator\TranslateOptions;
20
21
22
/**
23
 * Class GetTranslationsArray
24
 * @package badams\MicrosoftTranslator\Methods
25
 * @link https://msdn.microsoft.com/en-us/library/ff512418.aspx
26
 */
27
class GetTranslationsArray implements \badams\MicrosoftTranslator\ApiMethodInterface
28
{
29
    const MAX_TEXTS = 10;
30
31
    /**
32
     * @const Maximum allowable length of text
33
     */
34
    const TEXT_MAX_LENGTH = 10000;
35
36
    /**
37
     * @var array
38
     */
39
    protected $texts;
40
41
    /**
42
     * @var Language
43
     */
44
    protected $to;
45
46
    /**
47
     * @var Language|null
48
     */
49
    protected $from;
50
51
    /**
52
     * @var int
53
     */
54
    protected $maxTranslations;
55
56
    /**
57
     * @var TranslateOptions
58
     */
59
    protected $options;
60
61
    /**
62
     * GetTranslationsArray constructor.
63
     * @param $texts
64
     * @param $to
65
     * @param $from
66
     * @param int $maxTranslations
67
     * @param TranslateOptions|null $options
68
     * @throws ArgumentException
69
     * @throws UnsupportedLanguageException
70
     */
71 15
    public function __construct($texts, $to, $from, $maxTranslations = 5, TranslateOptions $options = null)
72
    {
73 15
        if (!is_array($texts)) {
74 3
            throw new ArgumentException('texts must be an array');
75
        }
76
77 12
        if (count($texts) > self::MAX_TEXTS) {
78 3
            throw new ArgumentException(sprintf('Amount of texts cannot exceed %s', self::MAX_TEXTS));
79
        }
80
81 9
        $textLengths = array_map('strlen', $texts);
82
83 9
        if (array_sum($textLengths) > self::TEXT_MAX_LENGTH) {
84 3
            throw new ArgumentException(sprintf('Total length of texts cannot exceed %s', self::TEXT_MAX_LENGTH));
85
        }
86
87 6
        $this->texts = $texts;
88 6
        $this->to = new Language($to);
89 6
        $this->from = new Language($from);
90 6
        $this->maxTranslations = $maxTranslations;
91 6
        $this->options = $options ? $options : new TranslateOptions();
92 6
    }
93
94
    /**
95
     * @return string
96
     */
97 3
    public function getRequestMethod()
98
    {
99 3
        return 'POST';
100
    }
101
102
    /**
103
     * @return array
104
     */
105 3
    public function getRequestOptions()
106
    {
107 3
        return ['body' => $this->createBodyXml()];
108
    }
109
110
    /**
111
     * @return string
112
     */
113 6
    protected function createBodyXml()
114
    {
115 6
        $xml = new \DOMDocument();
116 6
        $xml->formatOutput = true;
117 6
        $root = $xml->createElement('GetTranslationsArrayRequest');
118 6
        $xml->appendChild($root);
119
120 6
        $root->appendChild($xml->createElement('AppId'));
121 6
        $root->appendChild($xml->createElement('From', $this->from));
122
123 6
        $options = $xml->importNode($this->options->xml('Options')->childNodes->item(0), true);
124
125 6
        $root->appendChild($options);
126
127
128 6
        $texts = $xml->createElement('Texts');
129 6
        $root->appendChild($texts);
130
131 6
        foreach ($this->texts as $text) {
132 6
            $texts->appendChild(
133 6
                $xml->createElementNS('http://schemas.microsoft.com/2003/10/Serialization/Arrays', 'string', $text)
134 6
            );
135 6
        }
136
137 6
        $root->appendChild($xml->createElement('To', $this->to));
138 6
        $root->appendChild($xml->createElement('MaxTranslations', $this->maxTranslations));
139
140 6
        return $xml->saveXML();
141
    }
142
143
    /**
144
     * @param \GuzzleHttp\Message\ResponseInterface $response
145
     * @return GetTranslationsResponse[]
146
     */
147 3
    public function processResponse(\GuzzleHttp\Message\ResponseInterface $response)
148
    {
149 3
        $xml = simplexml_load_string($response->getBody()->getContents());
150
151 3
        $responses = [];
152
153 3
        foreach ($xml->GetTranslationsResponse as $node) {
154 3
            $responses[] = GetTranslationsResponse::fromXml($node);
155 3
        }
156
157 3
        return $responses;
158
    }
159
160
}