KataKotor   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 64
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A check() 0 11 3
A masking() 0 22 4
1
<?php
2
3
namespace Deogw\KataKotorLaravel;
4
5
class KataKotor
6
{
7
    /**
8
     * @var array
9
     */
10
    protected $listKataKotor;
11
12
13
    public function __construct()
14
    {
15
        $this->listKataKotor = array_map('strtolower', config('kata-kotor'));
16
    }
17
18
    /**
19
     * Check apakah kata yang di input mengandung kata kotor
20
     *
21
     * @param  String  $kata
22
     *
23
     * @return boolean
24
     */
25
    public function check($kata)
26
    {
27
        $kata = explode(' ', $kata);
28
29
        foreach ($kata as $word) {
30
            if (in_array(strtolower($word), $this->listKataKotor)) {
31
                return true;
32
            }
33
        }
34
        return false;
35
    }
36
37
    /**
38
     * Mengganti huruf vocal dalam kata atau kalimat dengan karakter masking
39
     *
40
     * @param  string  $kata
41
     *
42
     * @param  string  $masking  (optional)
43
     *
44
     * @return string
45
     */
46
    public function masking($kata, $masking = '*')
47
    {
48
        $words = explode(' ', $kata);
49
        $bad_words = $this->listKataKotor;
50
        $new_words = [];
51
52
        foreach ($words as $word) {
53
            if (in_array(strtolower($word), $bad_words)) {
54
                $replaceString = str_ireplace(['a', 'i', 'u', 'e', 'o'], $masking, $word);
55
56
                if (!strpos($replaceString, $masking)) {
57
                    $new_words[] = substr_replace($word, $masking, -1);
58
                } else {
59
                    $new_words[] = $replaceString;
60
                }
61
62
            } else {
63
                $new_words[] = $word;
64
            }
65
        }
66
        return implode(' ', $new_words);
67
    }
68
}
69