Passed
Push — master ( beb49a...b157da )
by Gino
02:22
created

PHPolyglot::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 4
nop 2
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace GinoPane\PHPolyglot;
4
5
use GinoPane\PHPolyglot\API\Factory\ApiFactoryAbstract;
6
use GinoPane\PHPolyglot\API\Response\TTS\TtsResponse;
7
use GinoPane\PHPolyglot\Supplemental\GetApiInstancesTrait;
8
use GinoPane\PHPolyglot\Supplemental\Language\Language;
9
use GinoPane\PHPolyglot\API\Supplemental\TTS\TtsAudioFormat;
10
use GinoPane\PHPolyglot\API\Response\Translate\TranslateResponse;
11
use GinoPane\PHPolyglot\API\Response\Dictionary\DictionaryResponse;
12
use GinoPane\PHPolyglot\API\Response\SpellCheck\SpellCheckResponse;
13
14
define(__NAMESPACE__ . '\ROOT_DIRECTORY', dirname(__FILE__));
15
16
/**
17
 * PHPolyglot
18
 * Easily translate, do spell check and speak-out texts in different languages
19
 *
20
 * @author Sergey <Gino Pane> Karavay
21
 */
22
class PHPolyglot
23
{
24
    use GetApiInstancesTrait;
25
26
    /**
27
     * PHPolyglot constructor
28
     *
29
     * You can pass configuration arrays manually if you want to dynamically override default behaviour
30
     *
31
     * @param array|null $config
32
     * @param array|null $env
33
     */
34
    public function __construct(array $config = null, array $env = null)
35
    {
36
        if (!is_null($config)) {
37
            ApiFactoryAbstract::setConfig($config);
38
        }
39
40
        if (!is_null($env)) {
41
            ApiFactoryAbstract::setEnv($env);
42
        }
43
    }
44
45
    /**
46
     * @param string $text
47
     * @param string $languageTo
48
     * @param string $languageFrom
49
     *
50
     * @return TranslateResponse
51
     */
52
    public function translate(string $text, string $languageTo, string $languageFrom = ''): TranslateResponse
53
    {
54
        return $this->getTranslateApi()->translate($text, new Language($languageTo), new Language($languageFrom));
55
    }
56
57
    /**
58
     * @param array  $text
59
     * @param string $languageTo
60
     * @param string $languageFrom
61
     *
62
     * @return TranslateResponse
63
     */
64
    public function translateBulk(array $text, string $languageTo, string $languageFrom = ''): TranslateResponse
65
    {
66
        return $this->getTranslateApi()->translateBulk($text, new Language($languageTo), new Language($languageFrom));
67
    }
68
69
    /**
70
     * The most common use of `lookup` is look up of the word in the same language, that's
71
     * why the first language parameter of `lookup` method is language-from, language-to is optional,
72
     * which differs from the language parameters order for translation
73
     *
74
     * @param string $text
75
     * @param string $languageFrom
76
     * @param string $languageTo
77
     *
78
     * @return DictionaryResponse
79
     */
80
    public function lookup(string $text, string $languageFrom, string $languageTo = ''): DictionaryResponse
81
    {
82
        if ($languageTo) {
83
            $response = $this->getDictionaryApi()->getTranslateAlternatives(
84
                $text, //@codeCoverageIgnore
85
                new Language($languageTo),
86
                new Language($languageFrom)
87
            );
88
        } else {
89
            $response = $this->getDictionaryApi()->getTextAlternatives($text, new Language($languageFrom));
90
        }
91
92
        return $response;
93
    }
94
95
    /**
96
     * @param string $text
97
     * @param string $languageFrom
98
     * @param string $audioFormat
99
     * @param array  $additionalData
100
     *
101
     * @return TtsResponse
102
     */
103
    public function speak(
104
        string $text,
105
        string $languageFrom,
106
        string $audioFormat = TtsAudioFormat::AUDIO_MP3,
107
        array $additionalData = []
108
    ): TtsResponse {
109
        $languageFrom = new Language($languageFrom);
110
111
        return $this
112
            ->getTtsApi($additionalData)
113
            ->textToSpeech($text, new Language($languageFrom), new TtsAudioFormat($audioFormat), $additionalData);
114
    }
115
116
    /**
117
     * @param string $text
118
     * @param string $languageFrom
119
     *
120
     * @return SpellCheckResponse
121
     */
122
    public function spellCheck(string $text, string $languageFrom = ''): SpellCheckResponse
123
    {
124
        return $this->getSpellCheckApi()->checkTexts([$text], new Language($languageFrom));
125
    }
126
127
    /**
128
     * @param array  $texts
129
     * @param string $languageFrom
130
     *
131
     * @return SpellCheckResponse
132
     */
133
    public function spellCheckBulk(array $texts, string $languageFrom = ''): SpellCheckResponse
134
    {
135
        return $this->getSpellCheckApi()->checkTexts($texts, new Language($languageFrom));
136
    }
137
}
138