Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
R
retypos-adapter
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
1
Issues
1
List
Board
Labels
Milestones
CI / CD
CI / CD
Pipelines
Schedules
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
eterfund
retypos-adapter
Commits
39f6ad5b
Commit
39f6ad5b
authored
Aug 27, 2018
by
Георгий Попов
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add exception handling while fixing typo
parent
f24c5ecb
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
39 additions
and
18 deletions
+39
-18
TyposClientInterface.php
src/TyposClientInterface.php
+39
-18
No files found.
src/TyposClientInterface.php
View file @
39f6ad5b
...
...
@@ -39,56 +39,77 @@ abstract class TyposClientInterface
* Fixes a typo in an article from a $link url. Uses a context while
* fixing to determine a typo position.
*
* @param string $typo Typo to be fixed
* @param string $corrected Correct variant
* @param string $context Context of typo
* @param string $link Link where the typo exist
* @param string $typo Typo to be fixed
* @param string $corrected Correct variant
* @param string $context Context of typo
* @param string $link Link where the typo exist
*
* @return array Array contains error code and optional message
*/
public
function
fixTypo
(
string
$typo
,
string
$corrected
,
string
$context
,
string
$link
)
{
$response
=
[
"errorCode"
=>
200
,
"message"
=>
"success"
,
];
try
{
$article
=
$this
->
getArticleFromLink
(
$link
);
$this
->
replaceTypoInArticle
(
$typo
,
$corrected
,
$context
,
$article
);
$this
->
saveArticle
(
$article
);
}
catch
(
\Exception
$e
)
{
return
[
"status"
=>
"error"
,
"message"
=>
$e
->
getMessage
()];
}
catch
(
\Exception
$e
)
{
$response
[
"errorCode"
]
=
$e
->
getCode
();
$response
[
"message"
]
=
$e
->
getMessage
();
return
$response
;
}
return
[
"status"
=>
"success"
]
;
return
$response
;
}
/**
* This method replaces a given typo in article, using the context to a correct
* variant.
*
* @param string $typo
Typo to be replaced
* @param string $corrected
Correct variant
* @param string $context
Context where the typo found
* @param string $typo Typo to be replaced
* @param string $corrected Correct variant
* @param string $context Context where the typo found
* @param TyposArticle $article Article to fix the typo
*
* @throws \Exception
* 404 - Typo does not exist
* 405 - Context has been changed
*/
public
function
replaceTypoInArticle
(
string
$typo
,
string
$corrected
,
string
$context
,
TyposArticle
$article
)
{
public
function
replaceTypoInArticle
(
string
$typo
,
string
$corrected
,
string
$context
,
TyposArticle
$article
)
{
// Strip all tags from text
$text
=
strip_tags
(
$article
->
text
);
$context
=
preg_quote
(
$context
);
$typo
=
preg_quote
(
$typo
);
// Find all typos in text, capture an offset of each typo
$typos
=
[];
preg_match_all
(
"#
{
$typo
}
#"
,
$text
,
$typos
,
PREG_OFFSET_CAPTURE
);
$typos
=
$typos
[
0
];
if
(
!
isset
(
$typos
[
0
]))
{
// Check for already fixed typo
preg_match_all
(
"#
{
$corrected
}
#"
,
$text
,
$typos
,
PREG_OFFSET_CAPTURE
);
if
(
isset
(
$typos
[
0
]))
{
throw
new
\Exception
(
"Already fixed"
,
208
);
}
throw
new
\Exception
(
"Typo not found"
,
404
);
}
// Find a context in text, capture it offset
$contextMatch
=
[];
preg_match_all
(
"#
{
$context
}
#"
,
$text
,
$contextMatch
,
PREG_OFFSET_CAPTURE
);
$contextMatch
=
$contextMatch
[
0
];
// If a context was changed then report an error,
// cannot locate typo in a new context, must be
// fixed manually
if
(
!
isset
(
$contextMatch
[
0
]))
{
throw
new
\Exception
(
"
Failed to find the context in article"
);
throw
new
\Exception
(
"
Context not found"
,
405
);
}
$contextMatch
=
$contextMatch
[
0
];
$contextOffset
=
$contextMatch
[
0
][
1
];
// Find a concrete typo that we want to fix
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment