StrAccentsMacroServiceProvider   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 2
dl 0
loc 38
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 4 1
A removeAccents() 0 8 1
1
<?php
2
3
namespace ChinLeung\StrAccentsMacro;
4
5
use Illuminate\Support\ServiceProvider;
6
use Illuminate\Support\Str;
7
8
class StrAccentsMacroServiceProvider extends ServiceProvider
9
{
10
    const CHARACTERS = [
11
        'à' => 'a', 'â' => 'a', 'ä' => 'a',
12
        'ç' => 'c',
13
        'é' => 'e', 'è' => 'e', 'ê' => 'e', 'ë' => 'e',
14
        'î' => 'i', 'ï' => 'i',
15
        'ô' => 'o', 'ö' => 'o',
16
        'À' => 'A', 'Â' => 'A', 'Ä' => 'A',
17
        'Ç' => 'C',
18
        'É' => 'E', 'È' => 'E', 'Ê' => 'E', 'Ë' => 'E',
19
        'Î' => 'I', 'Ï' => 'I',
20
        'Ô' => 'O', 'Ö' => 'O',
21
    ];
22
23
    /**
24
     * Bootstrap the application services.
25
     */
26
    public function boot()
27
    {
28
        Str::macro('removeAccents', [$this, 'removeAccents']);
29
    }
30
31
    /**
32
     * Remove the accented characters from a string.
33
     *
34
     * @param  string  $value
35
     * @return string
36
     */
37
    public function removeAccents(string $value) : string
38
    {
39
        return str_replace(
40
            array_keys(static::CHARACTERS),
41
            array_values(static::CHARACTERS),
42
            $value
43
        );
44
    }
45
}
46