Completed
Push — master ( b9920c...064526 )
by Jan-Petter
03:06
created

EncodingConverter::intl()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 11
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 11
loc 11
rs 9.4285
cc 2
eloc 8
nc 3
nop 0
1
<?php
2
namespace vipnytt\RobotsTxtParser\Client\Encoding;
3
4
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
5
6
/**
7
 * Class EncodingConverter
8
 *
9
 * @package vipnytt\RobotsTxtParser\Client\Encoding
10
 */
11
class EncodingConverter implements RobotsTxtInterface
12
{
13
    /**
14
     * String to convert
15
     * @var string
16
     */
17
    private $string;
18
19
    /**
20
     * String encoding
21
     * @var string
22
     */
23
    private $encoding;
24
25
    /**
26
     * CharacterEncodingConvert constructor.
27
     *
28
     * @param string $string
29
     * @param string $encoding
30
     */
31
    public function __construct($string, $encoding)
32
    {
33
        $this->string = $string;
34
        $this->encoding = $encoding;
35
    }
36
37
    /**
38
     * Auto mode
39
     *
40
     * @return string|false
41
     */
42
    public function auto()
43
    {
44
        if ($this->encoding == self::ENCODING) {
45
            return $this->string;
46
        } elseif (
47
            extension_loaded('intl') &&
48
            ($intl = $this->intl()) !== false
49
        ) {
50
            return $intl;
51
        } elseif (
52
            extension_loaded('iconv') &&
53
            ($iconv = $this->iconv()) !== false
54
        ) {
55
            return $iconv;
56
        } elseif (($mbstring = $this->mbstring()) !== false) {
57
            return $mbstring;
58
        }
59
        return false;
60
    }
61
62
    /**
63
     * intl
64
     * @link http://php.net/manual/en/uconverter.convert.php
65
     *
66
     * @return string|false
67
     */
68 View Code Duplication
    public function intl()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
    {
70
        try {
71
            $uConverter = new \UConverter(self::ENCODING, $this->encoding);
72
            $converted = $uConverter->convert($this->string);
73
        } catch (\Exception $e) {
74
            return false;
75
        }
76
        mb_internal_encoding(self::ENCODING);
77
        return $converted;
78
    }
79
80
    /**
81
     * iconv
82
     * @link http://php.net/manual/en/function.iconv.php
83
     *
84
     * @param string $outSuffix
85
     * @return string|false
86
     */
87 View Code Duplication
    public function iconv($outSuffix = '//TRANSLIT//IGNORE')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
    {
89
        try {
90
            $converted = iconv($this->encoding, self::ENCODING . $outSuffix, $this->string);
91
        } catch (\Exception $e) {
92
            return false;
93
        }
94
        mb_internal_encoding(self::ENCODING);
95
        return $converted;
96
    }
97
98
    /**
99
     * mbstring
100
     * @link http://php.net/manual/en/function.mb-convert-encoding.php
101
     *
102
     * @param array|string|null $fromOverride
103
     * @return string|false
104
     */
105
    public function mbstring($fromOverride = null)
106
    {
107
        try {
108
            $converted = mb_convert_encoding($this->string, self::ENCODING, $fromOverride === null ? $this->encoding : $fromOverride);
109
        } catch (\Exception $e) {
110
            return false;
111
        }
112
        mb_internal_encoding(self::ENCODING);
113
        return $converted;
114
    }
115
}
116