GetTranslationsArray::getRequestOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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
 * Class GetTranslationsArray
23
 * @package badams\MicrosoftTranslator\Methods
24
 * @link https://msdn.microsoft.com/en-us/library/ff512418.aspx
25
 */
26
class GetTranslationsArray implements \badams\MicrosoftTranslator\ApiMethodInterface
27
{
28
    const MAX_TEXTS = 10;
29
30
    /**
31
     * @const Maximum allowable length of text
32
     */
33
    const TEXT_MAX_LENGTH = 10000;
34
35
    /**
36
     * @var array
37
     */
38
    protected $texts;
39
40
    /**
41
     * @var Language
42
     */
43
    protected $to;
44
45
    /**
46
     * @var Language|null
47
     */
48
    protected $from;
49
50
    /**
51
     * @var int
52
     */
53
    protected $maxTranslations;
54
55
    /**
56
     * @var TranslateOptions
57
     */
58
    protected $options;
59
60
    /**
61
     * GetTranslationsArray constructor.
62
     * @param $texts
63
     * @param $to
64
     * @param $from
65
     * @param int $maxTranslations
66
     * @param TranslateOptions|null $options
67
     * @throws ArgumentException
68
     * @throws UnsupportedLanguageException
69
     */
70 15
    public function __construct($texts, $to, $from, $maxTranslations = 5, TranslateOptions $options = null)
71
    {
72 15
        $this->texts = $texts;
73 15
        $this->to = new Language($to);
74 15
        $this->from = new Language($from);
75 15
        $this->maxTranslations = $maxTranslations;
76 15
        $this->options = $options ? $options : new TranslateOptions();
77
78 15
        $this->assertTextsIsArray();
79 12
        $this->assertTextsCount();
80 9
        $this->assertTextsLength();
81 6
    }
82
83
    /**
84
     * @throws ArgumentException
85
     */
86 15
    protected function assertTextsIsArray()
87
    {
88 15
        if (!is_array($this->texts)) {
89 3
            throw new ArgumentException('texts must be an array');
90
        }
91 12
    }
92
93
    /**
94
     * @throws ArgumentException
95
     */
96 12
    protected function assertTextsCount()
97
    {
98 12
        if (count($this->texts) > self::MAX_TEXTS) {
99 3
            throw new ArgumentException(sprintf('Amount of texts cannot exceed %s', self::MAX_TEXTS));
100
        }
101 9
    }
102
103
    /**
104
     * @throws ArgumentException
105
     */
106 9
    protected function assertTextsLength()
107
    {
108 9
        $textLengths = array_map('strlen', $this->texts);
109
110 9
        if (array_sum($textLengths) > self::TEXT_MAX_LENGTH) {
111 3
            throw new ArgumentException(sprintf('Total length of texts cannot exceed %s', self::TEXT_MAX_LENGTH));
112
        }
113 6
    }
114
115
    /**
116
     * @return string
117
     */
118 3
    public function getRequestMethod()
119
    {
120 3
        return 'POST';
121
    }
122
123
    /**
124
     * @return array
125
     */
126 3
    public function getRequestOptions()
127
    {
128 3
        return ['body' => $this->createBodyXml()];
129
    }
130
131
    /**
132
     * @return string
133
     */
134 6
    protected function createBodyXml()
135
    {
136 6
        $xml = new \DOMDocument();
137 6
        $xml->formatOutput = true;
138 6
        $root = $xml->createElement('GetTranslationsArrayRequest');
139 6
        $xml->appendChild($root);
140
141 6
        $root->appendChild($xml->createElement('AppId'));
142 6
        $root->appendChild($xml->createElement('From', $this->from));
143
144 6
        $this->appendOptionsNode($xml, $root);
145 6
        $this->appendTextsNode($xml, $root);
146
147 6
        $root->appendChild($xml->createElement('To', $this->to));
148 6
        $root->appendChild($xml->createElement('MaxTranslations', $this->maxTranslations));
149
150 6
        return $xml->saveXML();
151
    }
152
153
    /**
154
     * @param \DOMDocument $xml
155
     * @param \DOMNode $root
156
     */
157 6
    protected function appendTextsNode(\DOMDocument $xml, \DOMNode $root)
158
    {
159 6
        $texts = $xml->createElement('Texts');
160 6
        $root->appendChild($texts);
161
162 6
        foreach ($this->texts as $text) {
163 6
            $texts->appendChild(
164 6
                $xml->createElementNS('http://schemas.microsoft.com/2003/10/Serialization/Arrays', 'string', $text)
165 6
            );
166 6
        }
167 6
    }
168
169
    /**
170
     * @param \DOMDocument $xml
171
     * @param \DOMNode $root
172
     */
173 6
    protected function appendOptionsNode(\DOMDocument $xml, \DOMNode $root)
174
    {
175 6
        $node = $this->options->xml('Options')->childNodes->item(0);
176 6
        $options = $xml->importNode($node, true);
177 6
        $root->appendChild($options);
178 6
    }
179
180
    /**
181
     * @param \GuzzleHttp\Message\ResponseInterface $response
182
     * @return GetTranslationsResponse[]
183
     */
184 3
    public function processResponse(\GuzzleHttp\Message\ResponseInterface $response)
185
    {
186 3
        $xml = simplexml_load_string($response->getBody()->getContents());
187
188 3
        $responses = [];
189
190 3
        foreach ($xml->GetTranslationsResponse as $node) {
191 3
            $responses[] = GetTranslationsResponse::fromXml($node);
192 3
        }
193
194 3
        return $responses;
195
    }
196
}
197