Skip to content

Commit

Permalink
Merge 89bcc35 into 5d16090
Browse files Browse the repository at this point in the history
  • Loading branch information
Synida committed Nov 17, 2020
2 parents 5d16090 + 89bcc35 commit 17d5fd8
Showing 1 changed file with 83 additions and 68 deletions.
151 changes: 83 additions & 68 deletions src/HunSpellPhpWrapper/DictionaryEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,26 +133,23 @@ public function delete($path)
*/
public function addWord($path, $word)
{
$dictionaryContent = file_get_contents($path);

$ext = pathinfo($path, PATHINFO_EXTENSION);

// Check empty or invalid word
if ($this->isInvalidWord($word)) {
$this->message = 'This word is invalid';
return false;
}

if (strpos($dictionaryContent, $word) !== false) {
$this->message = 'The word already exists in the database';
return false;
}
$ext = pathinfo($path, PATHINFO_EXTENSION);

$dictionaryContent = preg_replace('/(\r\n)|\r|\n/', PHP_EOL, $dictionaryContent);
$words = explode(PHP_EOL, $dictionaryContent);
// Returns with the words of the dictionary or template found on the specified path.
$words = $this->getDictionaryWords($path);

if (isset($words[0]) && is_numeric($words[0])) {
unset($words[0]);
foreach ($words as $currentWord) {
$wordParts = explode('/', $currentWord);
if (strtolower($wordParts[0]) === strtolower($word)) {
$this->message = 'The word already exists in the database';
return false;
}
}

$words[] = $word;
Expand Down Expand Up @@ -202,43 +199,41 @@ protected function isInvalidWord($word)
*/
public function deleteWord($path, $word)
{
$dictionaryContent = file_get_contents($path);

$ext = pathinfo($path, PATHINFO_EXTENSION);

if (strpos($dictionaryContent, $word) !== false) {
$dictionaryContent = preg_replace('/(\r\n)|\r|\n/', PHP_EOL, $dictionaryContent);
$words = explode(PHP_EOL, $dictionaryContent);
// Returns with the words of the dictionary or template found on the specified path.
$words = $this->getDictionaryWords($path);

if (isset($words[0]) && is_numeric($words[0])) {
unset($words[0]);
}
$found = false;
foreach ($words as $key => $currentWord) {
$wordParts = explode('/', $currentWord);

foreach ($words as $wordKey => $currentWord) {
if ($word === $currentWord) {
unset($words[$wordKey]);
break;
}
if (strtolower($wordParts[0]) === strtolower($word)) {
$found = true;
unset($words[$key]);
break;
}
}

$words = array_filter(
$words,
function ($value) {
return !is_null($value) && $value !== '' && $value !== PHP_EOL;
}
);
if (!$found) {
$this->message = "The defined dictionary({$path}) does not contain this word({$word})";
return false;
}

if ($ext !== static::TEMPLATE_EXTENSION) {
array_unshift($words, count($words));
$words = array_filter(
$words,
function ($value) {
return !is_null($value) && $value !== '' && $value !== PHP_EOL;
}
);

file_put_contents($path, ltrim(implode(PHP_EOL, $words), PHP_EOL));

return true;
if ($ext !== static::TEMPLATE_EXTENSION) {
array_unshift($words, count($words));
}

$this->message = "The defined dictionary({$path}) does not contain this word({$word})";
return false;
file_put_contents($path, ltrim(implode(PHP_EOL, $words), PHP_EOL));

return true;
}

/**
Expand All @@ -258,48 +253,47 @@ public function editWord($path, $word, $modifiedWord)
return false;
}

$dictionaryContent = file_get_contents($path);

$ext = pathinfo($path, PATHINFO_EXTENSION);

if (strpos($dictionaryContent, $word) !== false) {
$dictionaryContent = preg_replace('/(\r\n)|\r|\n/', PHP_EOL, $dictionaryContent);
$words = explode(PHP_EOL, $dictionaryContent);
// Returns with the words of the dictionary or template found on the specified path.
$words = $this->getDictionaryWords($path);

if (isset($words[0]) && is_numeric($words[0])) {
unset($words[0]);
}
$found = false;
foreach ($words as $key => $currentWord) {
$wordParts = explode('/', $currentWord);

if (in_array($modifiedWord, $words)) {
$this->message = 'This word is already in the dictionary';
return false;
if (strtolower($wordParts[0]) === strtolower($word)) {
$found = true;
break;
}
}

foreach ($words as $wordKey => $currentWord) {
if ($word === $currentWord) {
$words[$wordKey] = $modifiedWord;
break;
}
}
if (!$found || !isset($key)) {
$this->message = "The defined dictionary({$path}) does not contain this word({$word})";
return false;
}

$words = array_filter(
$words,
function ($value) {
return !is_null($value) && $value !== '' && $value !== PHP_EOL;
}
);
if (in_array($modifiedWord, $words)) {
$this->message = 'This word is already in the dictionary';
return false;
}

if ($ext !== static::TEMPLATE_EXTENSION) {
array_unshift($words, count($words));
}
$words[$key] = $modifiedWord;

file_put_contents($path, ltrim(implode(PHP_EOL, $words), PHP_EOL));
$words = array_filter(
$words,
function ($value) {
return !is_null($value) && $value !== '' && $value !== PHP_EOL;
}
);

return true;
if ($ext !== static::TEMPLATE_EXTENSION) {
array_unshift($words, count($words));
}

$this->message = "The defined dictionary({$path}) does not contain this word({$word})";
return false;
file_put_contents($path, ltrim(implode(PHP_EOL, $words), PHP_EOL));

return true;
}

/**
Expand Down Expand Up @@ -333,4 +327,25 @@ public function getMessage()
{
return $this->message;
}

/**
* Returns with the words of the dictionary or template found on the specified path.
*
* @param string $path
* @return array
* @author Synida Pry
*/
protected function getDictionaryWords($path)
{
$dictionaryContent = file_get_contents($path);

$dictionaryContent = preg_replace('/(\r\n)|\r|\n/', PHP_EOL, $dictionaryContent);
$words = explode(PHP_EOL, $dictionaryContent);

if (isset($words[0]) && is_numeric($words[0])) {
unset($words[0]);
}

return $words;
}
}

0 comments on commit 17d5fd8

Please sign in to comment.