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

Translug   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 68
ccs 19
cts 19
cp 1
rs 10
c 1
b 0
f 0
wmc 7
lcom 1
cbo 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setConfig() 0 4 1
A getConfig() 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 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
}