Passed
Push — master ( d81822...eb030a )
by Jelly
02:24
created

Translug::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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