Translug::translug()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace JellyBool\Translug;
4
5
use GuzzleHttp\Client;
6
7
/**
8
 * Class Translug.
9
 */
10
class Translug
11
{
12
    /**
13
     * @var array
14
     */
15
    protected $config = [];
16
17
    /**
18
     * Translug constructor.
19
     *
20
     * @param array $config
21
     */
22
    public function __construct(array $config = [])
23
    {
24
        $this->config = $config;
25
    }
26
27
    /**
28
     * @param array $config
29
     */
30
    public function setConfig(array $config)
31
    {
32
        $this->config = $config;
33
    }
34
35
    /**
36
     * @return array
37
     */
38
    public function getConfig()
39
    {
40
        return $this->config;
41
    }
42
43
    /**
44
     * @param $text
45
     *
46
     * @return mixed
47
     */
48
    public function translate($text)
49
    {
50
        $translator = new Translation(new Client(), $this->config);
51
52
        return $translator->translate($text);
53
    }
54
55
    /**
56
     * @param $text
57
     *
58
     * @return string
59
     */
60
    public function translug($text)
61
    {
62
        return $this->sluggable($this->translate($text));
63
    }
64
65
    /**
66
     * @param $title
67
     * @param string $separator
68
     *
69
     * @return string
70
     */
71
    private function sluggable($title, $separator = '-')
72
    {
73
        $flip = $separator == '-' ? '_' : '-';
74
75
        $title = preg_replace('!['.preg_quote($flip).']+!u', $separator, $title);
76
77
        $title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', mb_strtolower($title));
78
79
        $title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);
80
81
        return trim($title, $separator);
82
    }
83
}
84