Completed
Push — master ( 60656c...dd3387 )
by Byron
04:15 queued 02:12
created

GetLanguageNames::getRequestMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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\Language;
16
17
/**
18
 * Class GetLanguageNames
19
 * @package badams\MicrosoftTranslator\Methods
20
 */
21
class GetLanguageNames implements \badams\MicrosoftTranslator\ApiMethodInterface
22
{
23
    protected $locale;
24
25
    protected $languageCodes;
26
27
28
    public function __construct($locale, $languageCodes)
29
    {
30
        $this->locale = new Language($locale);
31
        $this->languageCodes = $languageCodes;
32
    }
33
34
    /**
35
     * @return string
36
     */
37
    public function getRequestMethod()
38
    {
39
        return 'POST';
40
    }
41
42
    /**
43
     * @return array
44
     */
45
    public function getRequestOptions()
46
    {
47
        return [
48
            'query' => ['locale' => $this->locale],
49
            'body' => $this->createBody(),
50
        ];
51
    }
52
53
    /**
54
     * @param \GuzzleHttp\Message\ResponseInterface $response
55
     * @return Language[]
56
     */
57
    public function processResponse(\GuzzleHttp\Message\ResponseInterface $response)
58
    {
59
        $xml = simplexml_load_string($response->getBody()->getContents());
60
61
        $languages = [];
62
63
        foreach ($xml->string as $language) {
64
            if (!empty($language)) {
65
                $languages[] = (string)$language;
66
            }
67
        }
68
69
        return $languages;
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    private function createBody()
76
    {
77
        $xml = new \DOMDocument();
78
        $array = $xml->createElementNS('http://schemas.microsoft.com/2003/10/Serialization/Arrays', 'ArrayOfstring');
79
80
        foreach ($this->languageCodes as $code) {
81
            $array->appendChild($xml->createElement('string', $code));
82
        }
83
84
        $xml->appendChild($array);
85
        return $xml->saveXML();
86
    }
87
}