1
|
|
|
<?php |
2
|
|
|
namespace SoliDry\Extension; |
3
|
|
|
|
4
|
|
|
use SoliDry\Helpers\ConfigHelper; |
5
|
|
|
use SoliDry\Helpers\MigrationsHelper; |
6
|
|
|
use SoliDry\Types\ConfigInterface; |
7
|
|
|
use SoliDry\Types\PhpInterface; |
8
|
|
|
|
9
|
|
|
class SpellCheck |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var mixed|null |
13
|
|
|
*/ |
14
|
|
|
private $entity = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var int|string|null |
18
|
|
|
*/ |
19
|
|
|
private $field = null; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var bool |
23
|
|
|
*/ |
24
|
|
|
private bool $isEnabled = false; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var mixed|string |
28
|
|
|
*/ |
29
|
|
|
private $language = ConfigInterface::DEFAULT_LANGUAGE; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var false|int|null |
33
|
|
|
*/ |
34
|
|
|
private $speLink = null; |
35
|
|
|
/** |
36
|
|
|
* SpellCheck constructor. |
37
|
|
|
* @param string $entity |
38
|
|
|
*/ |
39
|
|
|
public function __construct(string $entity) |
40
|
|
|
{ |
41
|
|
|
$this->entity = ConfigHelper::getNestedParam(ConfigInterface::SPELL_CHECK, MigrationsHelper::getTableName($entity)); |
42
|
|
|
$this->field = key($this->entity); |
|
|
|
|
43
|
|
|
$this->isEnabled = empty($this->entity[$this->field][ConfigInterface::ENABLED]) ? false : true; |
44
|
|
|
$this->language = empty($this->entity[$this->field][ConfigInterface::LANGUAGE]) ? ConfigInterface::DEFAULT_LANGUAGE |
45
|
|
|
: $this->entity[$this->field][ConfigInterface::LANGUAGE]; |
46
|
|
|
if($this->isEnabled === true) { |
47
|
|
|
$this->speLink = pspell_new($this->language, '', '', '', PSPELL_FAST); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Gets the "dirty" text and returns array of failed spell checked words |
53
|
|
|
* if there are any |
54
|
|
|
* @param string $text |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
|
|
public function check(string $text) |
58
|
|
|
{ |
59
|
|
|
$failed = []; |
60
|
|
|
$cleanText = $this->cleanText($text); |
61
|
|
|
$words = explode(PhpInterface::SPACE, $cleanText); |
62
|
|
|
foreach($words as $k => $word) { |
63
|
|
|
$pass = pspell_check($this->speLink, $word); |
|
|
|
|
64
|
|
|
if($pass === false) { |
65
|
|
|
$failed[] = $word; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
return $failed; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $text |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
private function cleanText(string $text) |
76
|
|
|
{ |
77
|
|
|
return str_replace([',', '.', '-', '- ', ':', '"'], '', $text); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return boolean |
82
|
|
|
*/ |
83
|
|
|
public function isEnabled() |
84
|
|
|
{ |
85
|
|
|
return $this->isEnabled; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function getLanguage() |
92
|
|
|
{ |
93
|
|
|
return $this->language; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return mixed|null |
98
|
|
|
*/ |
99
|
|
|
public function getField() |
100
|
|
|
{ |
101
|
|
|
return $this->field; |
102
|
|
|
} |
103
|
|
|
} |