Completed
Branch master (d81822)
by Jelly
05:48 queued 03:37
created

Translug::setConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
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 4
    public function __construct(array $config = [])
24
    {
25 4
        $this->config = $config;
26 4
    }
27
28
    /**
29
     * @param array $config
30
     */
31
    public function setConfig(array $config)
32
    {
33
        $this->config = $config;
34
    }
35
36
    /**
37
     * @param $text
38
     * @return mixed
39
     */
40 2
    public function translate($text)
41
    {
42 2
        $translator = new Translation(new Client(), $this->config);
43
44 2
        return $translator->translate($text);
45
    }
46
47
    /**
48
     * @param $text
49
     * @return string
50
     */
51 1
    public function translug($text)
52
    {
53 1
        return $this->sluggable($this->translate($text));
54
    }
55
56
    /**
57
     * @param $title
58
     * @param string $separator
59
     * @return string
60
     */
61 1
    private function sluggable($title, $separator = '-')
62
    {
63 1
        $flip = $separator == '-' ? '_' : '-';
64
65 1
        $title = preg_replace('![' . preg_quote($flip) . ']+!u', $separator, $title);
66
67 1
        $title = preg_replace('![^' . preg_quote($separator) . '\pL\pN\s]+!u', '', mb_strtolower($title));
68
69 1
        $title = preg_replace('![' . preg_quote($separator) . '\s]+!u', $separator, $title);
70
71 1
        return trim($title, $separator);
72
    }
73
}