Commit 4c886e20 authored by Vadim's avatar Vadim

Fixed patterns of typo, corrected and context. Added abstract function clearText()

parent 418bb4e1
......@@ -2,8 +2,6 @@
/**
* Created by PhpStorm.
* User: ambulance
* Date: 25.05.18
* Time: 19:39
*/
namespace Etersoft\Typos;
......@@ -210,7 +208,7 @@ abstract class TyposClientInterface
* @param string $typo
* @param string $corrected
* @param string $context
* @param $text
* @param string $text
*
* @return string Text with typo replaced by corrected
* @throws \Exception If something goes wrong
......@@ -219,10 +217,8 @@ abstract class TyposClientInterface
"Context not found", 405
*/
private function replaceTypoInText(string $typo, string $corrected, string $context, string $text) {
// Strip all tags from text
// Also copy input string
// Copy input string
$originalText = $text;
$text = strip_tags($text);
/*
// BUG# 13121
$typo = str_replace("\xc2\xa0", " ", $typo);
......@@ -230,10 +226,18 @@ abstract class TyposClientInterface
$context = str_replace("\xc2\xa0", " ", $context);
$text = str_replace("\xc2\xa0", " ", $text);
*/
// Clear texts from tags, replaces special characters with standard ones
// $typo = $this->clearText($typo);
// $corrected = $this->clearText($corrected);
$context = $this->clearText($context);
$text = $this->clearText($text);
// Find all typos in text, capture an offset of each typo
if (!preg_match_all("#{$typo}#", $text, $typos, PREG_OFFSET_CAPTURE)) {
$pattern = preg_quote ($typo, '#');
if (!preg_match_all("#{$pattern}#u", $text, $typos, PREG_OFFSET_CAPTURE)) {
// Check for already fixed typo
if (preg_match("#{$corrected}#", $text, $typos, PREG_OFFSET_CAPTURE)) {
$pattern = preg_quote ($corrected, '#');
if (preg_match("#{$pattern}#u", $text, $typos, PREG_OFFSET_CAPTURE)) {
throw new \Exception("Already fixed", 208);
}
......@@ -243,7 +247,8 @@ abstract class TyposClientInterface
// Find a context in text, capture it offset
if (!preg_match("#{$context}#", $text, $contextMatch, PREG_OFFSET_CAPTURE)) {
$pattern = preg_quote ($context, '#');
if (!preg_match("#{$pattern}#u", $text, $contextMatch, PREG_OFFSET_CAPTURE)) {
// If a context was changed then report an error,
// cannot locate typo in a new context, must be
// fixed manually
......@@ -264,7 +269,8 @@ abstract class TyposClientInterface
// Fix a match with index = $indexOfTypo
$index = 0;
return preg_replace_callback("#{$typo}#",
$pattern = preg_quote ($typo, '#');
return preg_replace_callback("#{$pattern}#u",
function($match) use(&$index, $indexOfTypo, $corrected) {
$index++;
if (($index - 1) == $indexOfTypo) {
......@@ -276,4 +282,14 @@ abstract class TyposClientInterface
$originalText);
}
/**
* Clears text from tags, replaces special characters with standard ones.
* Removes whitespace characters from the beginning and end of the text.
* In a sense, the function is the inverse for wptexturize(); - WordPress function.
*
* @param string $text
*
* @return string Cleared text
*/
protected abstract function clearText(string $text);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment