YandexSpellCheckApi::createCheckTextsContext()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 2
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace GinoPane\PHPolyglot\API\Implementation\SpellCheck\Yandex;
4
5
use GinoPane\NanoRest\Request\RequestContext;
6
use GinoPane\NanoRest\Response\JsonResponseContext;
7
use GinoPane\NanoRest\Response\ResponseContextAbstract;
8
use GinoPane\PHPolyglot\Supplemental\Language\Language;
9
use GinoPane\NanoRest\Exceptions\RequestContextException;
10
use GinoPane\PHPolyglot\API\Response\SpellCheck\SpellCheckResponse;
11
use GinoPane\PHPolyglot\API\Implementation\SpellCheck\SpellCheckApiAbstract;
12
13
/**
14
 * Class YandexSpellCheckApi
15
 *
16
 * @link https://tech.yandex.ru/speller/doc/dg/concepts/About-docpage/
17
 *
18
 * @author Sergey <Gino Pane> Karavay
19
 */
20
class YandexSpellCheckApi extends SpellCheckApiAbstract
21
{
22
    /**
23
     * URL path for check texts action
24
     */
25
    const CHECK_TEXTS_API_PATH = 'checkTexts';
26
27
    /**
28
     * Main API endpoint
29
     *
30
     * @var string
31
     */
32
    protected $apiEndpoint = 'http://speller.yandex.net/services/spellservice.json';
33
34
    /**
35
     * Create request context for spell-check request
36
     *
37
     * @param array    $texts
38
     * @param Language $languageFrom
39
     *
40
     * @throws RequestContextException
41
     *
42
     * @return RequestContext
43
     */
44
    protected function createCheckTextsContext(
45
        array $texts,
46
        Language $languageFrom
47
    ): RequestContext {
48
        $requestContext = (new RequestContext(sprintf("%s/%s", $this->apiEndpoint, self::CHECK_TEXTS_API_PATH)))
49
            ->setRequestParameters(
50
                [
51
                    'lang' => $languageFrom->getCode()
52
                ]
53
            )
54
            ->setData(['text' => $texts])
55
            ->setMethod(RequestContext::METHOD_POST)
56
            ->setEncodeArraysUsingDuplication(true)
57
            ->setContentType(RequestContext::CONTENT_TYPE_FORM_URLENCODED)
58
            ->setResponseContextClass(JsonResponseContext::class);
59
60
        return $requestContext;
61
    }
62
63
    /**
64
     * Process response of spell-check request and prepare valid response
65
     *
66
     * @param ResponseContextAbstract $context
67
     *
68
     * @return SpellCheckResponse
69
     */
70
    protected function prepareCheckTextsResponse(ResponseContextAbstract $context): SpellCheckResponse
71
    {
72
        $response = new SpellCheckResponse();
73
74
        $corrections = [];
75
76
        foreach ($context->getArray() as $wordCorrections) {
77
            $corrected = [];
78
79
            foreach ($wordCorrections as $wordCorrection) {
80
                if (!empty($wordCorrection['s']) && !empty($wordCorrection['word'])) {
81
                    $corrected[$wordCorrection['word']] = (array)$wordCorrection['s'];
82
                }
83
            }
84
85
            $corrections[] = $corrected;
86
        }
87
88
        $response->setCorrections($corrections);
89
90
        return $response;
91
    }
92
}
93