1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fecony\YandexSpeller; |
4
|
|
|
|
5
|
|
|
use Fecony\YandexSpeller\Http\HttpClient; |
6
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
7
|
|
|
use Illuminate\Http\JsonResponse; |
8
|
|
|
|
9
|
|
|
class YandexSpeller extends HttpClient |
10
|
|
|
{ |
11
|
|
|
/** @var string contains an API endpoint */ |
12
|
|
|
protected $uri; |
13
|
|
|
|
14
|
|
|
/** @var string|array contains passed in string or array of strings. */ |
15
|
|
|
protected $text; |
16
|
|
|
|
17
|
|
|
/** @var string|null may contain comma separated languages to override default config. */ |
18
|
|
|
protected $lang; |
19
|
|
|
|
20
|
|
|
/** @var int|null optional parameter value is the sum of the values of the required options. */ |
21
|
|
|
protected $options; |
22
|
|
|
|
23
|
|
|
/** @var string|null option specifies the default format of the text. */ |
24
|
|
|
protected $format; |
25
|
|
|
|
26
|
|
|
/** @var array query options used to determine API behaviour. */ |
27
|
|
|
protected $query; |
28
|
|
|
|
29
|
|
|
/** @var array contains supported options. */ |
30
|
|
|
protected $queryOptions = [ |
31
|
|
|
'lang', |
32
|
|
|
'options', |
33
|
|
|
'format', |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Checks spelling in the specified $text passage. |
38
|
|
|
* |
39
|
|
|
* $text variable may be an array of strings. |
40
|
|
|
* |
41
|
|
|
* @param $text |
42
|
|
|
* @param string|null $lang |
43
|
|
|
* @param int|null $options |
44
|
|
|
* @param string|null $format |
45
|
|
|
* @return JsonResponse|null |
46
|
|
|
*/ |
47
|
|
|
public function check($text, string $lang = null, int $options = null, string $format = null): ?JsonResponse |
48
|
|
|
{ |
49
|
|
|
$this->text = $text; |
50
|
|
|
$this->lang = $lang; |
51
|
|
|
$this->options = $options; |
52
|
|
|
$this->format = $format; |
53
|
|
|
$this->query = []; |
54
|
|
|
|
55
|
|
|
return $this->checkText(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return JsonResponse |
60
|
|
|
*/ |
61
|
|
|
private function checkText(): ?JsonResponse |
62
|
|
|
{ |
63
|
|
|
$query = $this->getQuery(); |
64
|
|
|
|
65
|
|
|
try { |
66
|
|
|
$response = $this->client->request('POST', $this->uri, [ |
67
|
|
|
'query' => $query, |
68
|
|
|
]); |
69
|
|
|
|
70
|
|
|
return response()->json([ |
71
|
|
|
'status' => $response->getStatusCode(), |
72
|
|
|
'data' => json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR), |
73
|
|
|
], $response->getStatusCode()); |
74
|
|
|
} catch (GuzzleException $e) { |
75
|
|
|
return response()->json([ |
76
|
|
|
'error' => $e->getMessage(), |
77
|
|
|
], 500); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
protected function getQuery(): array |
85
|
|
|
{ |
86
|
|
|
return $this->buildTextQuery()->buildQueryParams(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
|
|
protected function buildQueryParams(): array |
93
|
|
|
{ |
94
|
|
|
foreach ($this->queryOptions as $option) { |
95
|
|
|
$this->query[$option] = $this->{$option} ?: config('yandex-speller.'.$option); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $this->query; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return YandexSpeller |
103
|
|
|
*/ |
104
|
|
|
protected function buildTextQuery(): YandexSpeller |
105
|
|
|
{ |
106
|
|
|
if (is_array($this->text)) { |
107
|
|
|
$this->query['text'] = implode('&text=', $this->text); |
108
|
|
|
$this->uri = self::CHECK_TEXTS_ENDPOINT; |
109
|
|
|
} else { |
110
|
|
|
$this->query['text'] = $this->text; |
111
|
|
|
$this->uri = self::CHECK_TEXT_ENDPOINT; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|