Passed
Branch master (d81822)
by Jelly
03:51 queued 01:29
created

Translug   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 82.35%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 63
ccs 14
cts 17
cp 0.8235
rs 10
wmc 6
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setConfig() 0 4 1
A translate() 0 6 1
A translug() 0 4 1
A sluggable() 0 12 2
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
}