1<?php
2
3namespace Safe;
4
5use Safe\Exceptions\PspellException;
6
7/**
8 *
9 *
10 * @param int $dictionary_link
11 * @param string $word The added word.
12 * @throws PspellException
13 *
14 */
15function pspell_add_to_personal(int $dictionary_link, string $word): void
16{
17    error_clear_last();
18    $result = \pspell_add_to_personal($dictionary_link, $word);
19    if ($result === false) {
20        throw PspellException::createFromPhpError();
21    }
22}
23
24
25/**
26 *
27 *
28 * @param int $dictionary_link
29 * @param string $word The added word.
30 * @throws PspellException
31 *
32 */
33function pspell_add_to_session(int $dictionary_link, string $word): void
34{
35    error_clear_last();
36    $result = \pspell_add_to_session($dictionary_link, $word);
37    if ($result === false) {
38        throw PspellException::createFromPhpError();
39    }
40}
41
42
43/**
44 *
45 *
46 * @param int $dictionary_link
47 * @throws PspellException
48 *
49 */
50function pspell_clear_session(int $dictionary_link): void
51{
52    error_clear_last();
53    $result = \pspell_clear_session($dictionary_link);
54    if ($result === false) {
55        throw PspellException::createFromPhpError();
56    }
57}
58
59
60/**
61 * Create a config used to open a dictionary.
62 *
63 * pspell_config_create has a very similar syntax to
64 * pspell_new. In fact, using
65 * pspell_config_create immediately followed by
66 * pspell_new_config will produce the exact same result.
67 * However, after creating a new config, you can also use
68 * pspell_config_* functions before calling
69 * pspell_new_config to take advantage of some
70 * advanced functionality.
71 *
72 * For more information and examples, check out inline manual pspell
73 * website:http://aspell.net/.
74 *
75 * @param string $language The language parameter is the language code which consists of the
76 * two letter ISO 639 language code and an optional two letter ISO
77 * 3166 country code after a dash or underscore.
78 * @param string $spelling The spelling parameter is the requested spelling for languages
79 * with more than one spelling such as English. Known values are
80 * 'american', 'british', and 'canadian'.
81 * @param string $jargon The jargon parameter contains extra information to distinguish
82 * two different words lists that have the same language and
83 * spelling parameters.
84 * @param string $encoding The encoding parameter is the encoding that words are expected to
85 * be in.  Valid values are 'utf-8', 'iso8859-*', 'koi8-r',
86 * 'viscii', 'cp1252', 'machine unsigned 16', 'machine unsigned
87 * 32'. This parameter is largely untested, so be careful when
88 * using.
89 * @return int Returns a pspell config identifier.
90 * @throws PspellException
91 *
92 */
93function pspell_config_create(string $language, string $spelling = null, string $jargon = null, string $encoding = null): int
94{
95    error_clear_last();
96    if ($encoding !== null) {
97        $result = \pspell_config_create($language, $spelling, $jargon, $encoding);
98    } elseif ($jargon !== null) {
99        $result = \pspell_config_create($language, $spelling, $jargon);
100    } elseif ($spelling !== null) {
101        $result = \pspell_config_create($language, $spelling);
102    } else {
103        $result = \pspell_config_create($language);
104    }
105    if ($result === false) {
106        throw PspellException::createFromPhpError();
107    }
108    return $result;
109}
110
111
112/**
113 * This function is
114 * currently not documented; only its argument list is available.
115 *
116 *
117 * @param int $conf
118 * @param string $directory
119 * @throws PspellException
120 *
121 */
122function pspell_config_data_dir(int $conf, string $directory): void
123{
124    error_clear_last();
125    $result = \pspell_config_data_dir($conf, $directory);
126    if ($result === false) {
127        throw PspellException::createFromPhpError();
128    }
129}
130
131
132/**
133 * This function is
134 * currently not documented; only its argument list is available.
135 *
136 *
137 * @param int $conf
138 * @param string $directory
139 * @throws PspellException
140 *
141 */
142function pspell_config_dict_dir(int $conf, string $directory): void
143{
144    error_clear_last();
145    $result = \pspell_config_dict_dir($conf, $directory);
146    if ($result === false) {
147        throw PspellException::createFromPhpError();
148    }
149}
150
151
152/**
153 *
154 *
155 * @param int $dictionary_link
156 * @param int $n Words less than n characters will be skipped.
157 * @throws PspellException
158 *
159 */
160function pspell_config_ignore(int $dictionary_link, int $n): void
161{
162    error_clear_last();
163    $result = \pspell_config_ignore($dictionary_link, $n);
164    if ($result === false) {
165        throw PspellException::createFromPhpError();
166    }
167}
168
169
170/**
171 *
172 *
173 * @param int $dictionary_link
174 * @param int $mode The mode parameter is the mode in which spellchecker will work.
175 * There are several modes available:
176 *
177 *
178 *
179 * PSPELL_FAST - Fast mode (least number of
180 * suggestions)
181 *
182 *
183 *
184 *
185 * PSPELL_NORMAL - Normal mode (more suggestions)
186 *
187 *
188 *
189 *
190 * PSPELL_BAD_SPELLERS - Slow mode (a lot of
191 * suggestions)
192 *
193 *
194 *
195 * @throws PspellException
196 *
197 */
198function pspell_config_mode(int $dictionary_link, int $mode): void
199{
200    error_clear_last();
201    $result = \pspell_config_mode($dictionary_link, $mode);
202    if ($result === false) {
203        throw PspellException::createFromPhpError();
204    }
205}
206
207
208/**
209 * Set a file that contains personal wordlist. The personal wordlist will be
210 * loaded and used in addition to the standard one after you call
211 * pspell_new_config. The file is also the file where
212 * pspell_save_wordlist will save personal wordlist to.
213 *
214 * pspell_config_personal should be used on a config
215 * before calling pspell_new_config.
216 *
217 * @param int $dictionary_link
218 * @param string $file The personal wordlist. If the file does not exist, it will be created.
219 * The file should be writable by whoever PHP runs as (e.g. nobody).
220 * @throws PspellException
221 *
222 */
223function pspell_config_personal(int $dictionary_link, string $file): void
224{
225    error_clear_last();
226    $result = \pspell_config_personal($dictionary_link, $file);
227    if ($result === false) {
228        throw PspellException::createFromPhpError();
229    }
230}
231
232
233/**
234 * Set a file that contains replacement pairs.
235 *
236 * The replacement pairs improve the quality of the spellchecker. When a word
237 * is misspelled, and a proper suggestion was not found in the list,
238 * pspell_store_replacement can be used to store a
239 * replacement pair and then pspell_save_wordlist to
240 * save the wordlist along with the replacement pairs.
241 *
242 * pspell_config_repl should be used on a config
243 * before calling pspell_new_config.
244 *
245 * @param int $dictionary_link
246 * @param string $file The file should be writable by whoever PHP runs as (e.g. nobody).
247 * @throws PspellException
248 *
249 */
250function pspell_config_repl(int $dictionary_link, string $file): void
251{
252    error_clear_last();
253    $result = \pspell_config_repl($dictionary_link, $file);
254    if ($result === false) {
255        throw PspellException::createFromPhpError();
256    }
257}
258
259
260/**
261 * This function determines whether run-together words will be treated as
262 * legal compounds.  That is, "thecat" will be a legal compound, although
263 * there should be a space between the two words. Changing this setting only
264 * affects the results returned by pspell_check;
265 * pspell_suggest will still return suggestions.
266 *
267 * pspell_config_runtogether should be used on a config
268 * before calling pspell_new_config.
269 *
270 * @param int $dictionary_link
271 * @param bool $flag TRUE if run-together words should be treated as legal compounds,
272 * FALSE otherwise.
273 * @throws PspellException
274 *
275 */
276function pspell_config_runtogether(int $dictionary_link, bool $flag): void
277{
278    error_clear_last();
279    $result = \pspell_config_runtogether($dictionary_link, $flag);
280    if ($result === false) {
281        throw PspellException::createFromPhpError();
282    }
283}
284
285
286/**
287 * pspell_config_save_repl determines whether
288 * pspell_save_wordlist will save the replacement pairs
289 * along with the wordlist. Usually there is no need to use this function
290 * because if pspell_config_repl is used, the
291 * replacement pairs will be saved by
292 * pspell_save_wordlist anyway, and if it is not,
293 * the replacement pairs will not be saved.
294 *
295 * pspell_config_save_repl should be used on a config
296 * before calling pspell_new_config.
297 *
298 * @param int $dictionary_link
299 * @param bool $flag TRUE if replacement pairs should be saved, FALSE otherwise.
300 * @throws PspellException
301 *
302 */
303function pspell_config_save_repl(int $dictionary_link, bool $flag): void
304{
305    error_clear_last();
306    $result = \pspell_config_save_repl($dictionary_link, $flag);
307    if ($result === false) {
308        throw PspellException::createFromPhpError();
309    }
310}
311
312
313/**
314 *
315 *
316 * @param int $config The config parameter is the one returned by
317 * pspell_config_create when the config was created.
318 * @return int Returns a dictionary link identifier on success.
319 * @throws PspellException
320 *
321 */
322function pspell_new_config(int $config): int
323{
324    error_clear_last();
325    $result = \pspell_new_config($config);
326    if ($result === false) {
327        throw PspellException::createFromPhpError();
328    }
329    return $result;
330}
331
332
333/**
334 * pspell_new opens up a new dictionary and
335 * returns the dictionary link identifier for use in other pspell
336 * functions.
337 *
338 * For more information and examples, check out inline manual pspell
339 * website:http://aspell.net/.
340 *
341 * @param string $language The language parameter is the language code which consists of the
342 * two letter ISO 639 language code and an optional two letter ISO
343 * 3166 country code after a dash or underscore.
344 * @param string $spelling The spelling parameter is the requested spelling for languages
345 * with more than one spelling such as English. Known values are
346 * 'american', 'british', and 'canadian'.
347 * @param string $jargon The jargon parameter contains extra information to distinguish
348 * two different words lists that have the same language and
349 * spelling parameters.
350 * @param string $encoding The encoding parameter is the encoding that words are expected to
351 * be in.  Valid values are 'utf-8', 'iso8859-*', 'koi8-r',
352 * 'viscii', 'cp1252', 'machine unsigned 16', 'machine unsigned
353 * 32'. This parameter is largely untested, so be careful when
354 * using.
355 * @param int $mode The mode parameter is the mode in which spellchecker will work.
356 * There are several modes available:
357 *
358 *
359 *
360 * PSPELL_FAST - Fast mode (least number of
361 * suggestions)
362 *
363 *
364 *
365 *
366 * PSPELL_NORMAL - Normal mode (more suggestions)
367 *
368 *
369 *
370 *
371 * PSPELL_BAD_SPELLERS - Slow mode (a lot of
372 * suggestions)
373 *
374 *
375 *
376 *
377 * PSPELL_RUN_TOGETHER - Consider run-together words
378 * as legal compounds.  That is, "thecat" will be a legal compound,
379 * although there should be a space between the two words. Changing this
380 * setting only affects the results returned by
381 * pspell_check; pspell_suggest
382 * will still return suggestions.
383 *
384 *
385 *
386 * Mode is a bitmask constructed from different constants listed above.
387 * However, PSPELL_FAST,
388 * PSPELL_NORMAL and
389 * PSPELL_BAD_SPELLERS are mutually exclusive, so you
390 * should select only one of them.
391 * @return int Returns the dictionary link identifier on success.
392 * @throws PspellException
393 *
394 */
395function pspell_new(string $language, string $spelling = null, string $jargon = null, string $encoding = null, int $mode = 0): int
396{
397    error_clear_last();
398    if ($mode !== 0) {
399        $result = \pspell_new($language, $spelling, $jargon, $encoding, $mode);
400    } elseif ($encoding !== null) {
401        $result = \pspell_new($language, $spelling, $jargon, $encoding);
402    } elseif ($jargon !== null) {
403        $result = \pspell_new($language, $spelling, $jargon);
404    } elseif ($spelling !== null) {
405        $result = \pspell_new($language, $spelling);
406    } else {
407        $result = \pspell_new($language);
408    }
409    if ($result === false) {
410        throw PspellException::createFromPhpError();
411    }
412    return $result;
413}
414
415
416/**
417 *
418 *
419 * @param int $dictionary_link A dictionary link identifier opened with
420 * pspell_new_personal.
421 * @throws PspellException
422 *
423 */
424function pspell_save_wordlist(int $dictionary_link): void
425{
426    error_clear_last();
427    $result = \pspell_save_wordlist($dictionary_link);
428    if ($result === false) {
429        throw PspellException::createFromPhpError();
430    }
431}
432
433
434/**
435 *
436 *
437 * @param int $dictionary_link A dictionary link identifier, opened with
438 * pspell_new_personal
439 * @param string $misspelled The misspelled word.
440 * @param string $correct The fixed spelling for the misspelled word.
441 * @throws PspellException
442 *
443 */
444function pspell_store_replacement(int $dictionary_link, string $misspelled, string $correct): void
445{
446    error_clear_last();
447    $result = \pspell_store_replacement($dictionary_link, $misspelled, $correct);
448    if ($result === false) {
449        throw PspellException::createFromPhpError();
450    }
451}
452