Response::getLang()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
/**
4
 * @Author Chiarillo Massimo
5
 */
6
7
namespace Yandex\TranslatorBundle\Http;
8
9
class Response
10
{
11
    private $decodedBody;
12
13
    /**
14
     * @param String $body
15
     */
16
    public function __construct($body)
17
    {
18
        $this->decodedBody = json_decode($body, true);
19
    }
20
21
    public function getText()
22
    {
23
        $text = $this->getDecodedBodyValue('text');
24
25
        if (!is_array($text)) {
26
            return null;
27
        }
28
29
        return reset($text);
30
    }
31
32
    protected function getLang()
33
    {
34
        $lang = $this->getDecodedBodyValue('lang');
35
36
        if (!$lang) {
37
            return false;
38
        }
39
40
        return explode('-', $lang);
41
    }
42
43
    public function getFrom()
44
    {
45
        if (!$lang = $this->getLang()) {
46
            return null;
47
        }
48
49
        return reset($lang);
50
    }
51
52
    public function getTo()
53
    {
54
        if (!$lang = $this->getLang()) {
55
            return null;
56
        }
57
58
        return end($lang);
59
    }
60
61
    public function getCode()
62
    {
63
        return $this->getDecodedBodyValue('code');
64
    }
65
66
    private function getDecodedBodyValue($key)
67
    {
68
        if (!array_key_exists($key, $this->decodedBody)) {
69
            return null;
70
        }
71
72
        return $this->decodedBody[$key];
73
    }
74
}
75