-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 910cd42
Showing
9 changed files
with
514 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
indent_size = 4 | ||
indent_style = space | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
* text=auto | ||
|
||
*.php diff=php | ||
*.html diff=html | ||
*.css diff=css | ||
*.md diff=markdown | ||
|
||
/.github export-ignore | ||
/tests export-ignore | ||
.editorconfig export-ignore | ||
.gitattributes export-ignore | ||
.gitignore export-ignore | ||
phpunit.xml export-ignore | ||
phpstan-tests.neon export-ignore | ||
phpstan.neon.dist export-ignore | ||
CODE_OF_CONDUCT.md export-ignore | ||
SECURITY.md export-ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/.idea | ||
/.vscode | ||
/vendor | ||
/.phpunit.cache | ||
composer.phar | ||
composer.lock | ||
.DS_Store | ||
Thumbs.db | ||
.phpunit.result.cache | ||
phpstan.neon | ||
phpstan-baseline.neon | ||
CODE_OF_CONDUCT.md | ||
SECURITY.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Simple filter kata kotor dengan regex |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "kamu/aman", | ||
"description": "Replace badword", | ||
"type": "library", | ||
"license": "MIT", | ||
"autoload": { | ||
"psr-4": { | ||
"Kamu\\": "src/" | ||
} | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "dewanakl", | ||
"email": "dewanakretarta29@gmail.com" | ||
} | ||
], | ||
"require": { | ||
"php": "^8.1", | ||
"ext-mbstring": "*" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^10.5" | ||
}, | ||
"config": { | ||
"optimize-autoloader": true, | ||
"preferred-install": "dist", | ||
"sort-packages": true | ||
}, | ||
"minimum-stability": "stable", | ||
"prefer-stable": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" beStrictAboutTestsThatDoNotTestAnything="false" colors="true" processIsolation="false" stopOnError="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false"> | ||
<testsuites> | ||
<testsuite name="Test Suite"> | ||
<directory suffix=".php">./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
<?php | ||
|
||
namespace Kamu; | ||
|
||
class Aman | ||
{ | ||
/** | ||
* Similar words. | ||
* | ||
* @var array $similar | ||
*/ | ||
private $similar = [ | ||
'a' => '(a|a\.|a\-|4|@|Á|á|À|Â|à|Â|â|Ä|ä|Ã|ã|Å|å|α|Δ|Λ|λ)', | ||
'b' => '(b|b\.|b\-|8|\|3|ß|Β|β)', | ||
'c' => '(c|c\.|c\-|Ç|ç|¢|€|<|\(|{|©)', | ||
'd' => '(d|d\.|d\-|∂|\|\)|Þ|þ|Ð|ð)', | ||
'e' => '(e|e\.|e\-|3|€|È|è|É|é|Ê|ê|∑)', | ||
'f' => '(f|f\.|f\-|ƒ)', | ||
'g' => '(g|g\.|g\-|6|9)', | ||
'h' => '(h|h\.|h\-|Η)', | ||
'i' => '(i|i\.|i\-|!|\||\]\[|]|1|∫|Ì|Í|Î|Ï|ì|í|î|ï)', | ||
'j' => '(j|j\.|j\-)', | ||
'k' => '(k|k\.|k\-|Κ|κ)', | ||
'l' => '(l|1\.|l\-|!|\||\]\[|]|£|∫|Ì|Í|Î|Ï)', | ||
'm' => '(m|m\.|m\-)', | ||
'n' => '(n|n\.|n\-|η|Ν|Π)', | ||
'o' => '(o|o\.|o\-|0|Ο|ο|Φ|¤|°|ø)', | ||
'p' => '(p|p\.|p\-|ρ|Ρ|¶|þ)', | ||
'q' => '(q|q\.|q\-)', | ||
'r' => '(r|r\.|r\-|®)', | ||
's' => '(s|s\.|s\-|5|\$|§)', | ||
't' => '(t|t\.|t\-|Τ|τ)', | ||
'u' => '(u|u\.|u\-|υ|µ)', | ||
'v' => '(v|v\.|v\-|υ|ν)', | ||
'w' => '(w|w\.|w\-|ω|ψ|Ψ)', | ||
'x' => '(x|x\.|x\-|Χ|χ)', | ||
'y' => '(y|y\.|y\-|¥|γ|ÿ|ý|Ÿ|Ý)', | ||
'z' => '(z|z\.|z\-|Ζ)', | ||
]; | ||
|
||
/** | ||
* List of words. | ||
* | ||
* @var array $lists | ||
*/ | ||
private $lists; | ||
|
||
/** | ||
* Singleton variable. | ||
* | ||
* @var Aman|null | ||
*/ | ||
public static $instance; | ||
|
||
/** | ||
* Init object. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->lists = []; | ||
$lists = (array) @require __DIR__ . '/db/lists.php'; | ||
|
||
foreach ($lists as $list) { | ||
$this->lists[] = $this->getFilterRegexp($list); | ||
} | ||
} | ||
|
||
/** | ||
* From list to regex pattern. | ||
* | ||
* @param string $string | ||
* @return string | ||
*/ | ||
private function getFilterRegexp(string $string): string | ||
{ | ||
$replace = str_ireplace(array_keys($this->similar), array_values($this->similar), $string); | ||
return '/' . $replace . '/iu'; | ||
} | ||
|
||
/** | ||
* Create object. | ||
* | ||
* @return Aman | ||
*/ | ||
public static function factory(): Aman | ||
{ | ||
if (!static::$instance) { | ||
static::$instance = new Aman(); | ||
} | ||
|
||
return static::$instance; | ||
} | ||
|
||
/** | ||
* Check if contains. | ||
* | ||
* @param string $string | ||
* @return bool | ||
*/ | ||
public function check(string $string): bool | ||
{ | ||
foreach ($this->lists as $pattern) { | ||
if (preg_match($pattern, $string)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Mask contains word. | ||
* | ||
* @param string $string | ||
* @param string $masking | ||
* @return string | ||
*/ | ||
public function masking(string $string, string $masking = '*'): string | ||
{ | ||
return strval(preg_replace_callback($this->lists, function (array $words) use ($masking): string { | ||
return str_repeat($masking, strlen($words[0])); | ||
}, $string)); | ||
} | ||
|
||
/** | ||
* Filter and remove if contains. | ||
* | ||
* @param string $string | ||
* @return string | ||
*/ | ||
public function filter(string $string): string | ||
{ | ||
return strval(preg_replace_callback($this->lists, function (): string { | ||
return ''; | ||
}, $string)); | ||
} | ||
|
||
/** | ||
* Get from string. | ||
* | ||
* @param string $string | ||
* @return array | ||
*/ | ||
public function words(string $string): array | ||
{ | ||
$lists = []; | ||
preg_replace_callback($this->lists, function (array $words) use (&$lists): void { | ||
$lists[] = $words[0]; | ||
}, $string); | ||
|
||
return array_reverse($lists); | ||
} | ||
} |
Oops, something went wrong.