Completed
Push — master ( a6d8dc...680f8e )
by Jan-Petter
03:57
created

CharacterEncodingConvert::auto()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 12
rs 9.2
cc 4
eloc 9
nc 4
nop 0
1
<?php
2
namespace vipnytt\RobotsTxtParser\Parser;
3
4
/**
5
 * Class CharacterEncodingConvert
6
 *
7
 * @package vipnytt\RobotsTxtParser\Parser
8
 */
9
class CharacterEncodingConvert implements RobotsTxtInterface
10
{
11
    /**
12
     * String to convert
13
     * @var string
14
     */
15
    protected $string;
16
17
    /**
18
     * Source encoding
19
     * @var string
20
     */
21
    protected $source;
22
23
    /**
24
     * Destination encoding
25
     * @var string
26
     */
27
    protected $destination;
28
29
    /**
30
     * CharacterEncodingConvert constructor.
31
     *
32
     * @param string $string
33
     * @param string $source
34
     * @param string $destination
35
     */
36
    public function __construct($string, $source, $destination = self::ENCODING)
37
    {
38
        $this->string = $string;
39
        $this->source = $source;
40
        $this->destination = $destination;
41
    }
42
43
    /**
44
     * Auto mode
45
     *
46
     * @return string|false
47
     */
48
    public function auto()
49
    {
50
        if ($this->source == $this->destination) {
51
            return $this->string;
52
        } elseif (($iconv = $this->iconv()) !== false) {
53
            return $iconv;
54
        } elseif (($mbstring = $this->mbstring()) !== false) {
55
            return $mbstring;
56
        }
57
        $this->fallback();
58
        return false;
59
    }
60
61
    /**
62
     * iconv
63
     *
64
     * @param string $outSuffix
65
     * @return string|false
66
     */
67
    public function iconv($outSuffix = '//TRANSLIT//IGNORE')
68
    {
69
        try {
70
            $converted = iconv($this->source, $this->destination . $outSuffix, $this->string);
71
        } catch (\Exception $msg) {
72
            return false;
73
        }
74
        return $converted;
75
    }
76
77
    /**
78
     * mb_convert_encoding
79
     *
80
     * @param array|string|null $fromOverride
81
     * @return string|false
82
     */
83
    public function mbstring($fromOverride = null)
84
    {
85
        $from = $this->source;
86
        if ($fromOverride !== null) {
87
            $from = $fromOverride;
88
        }
89
        try {
90
            $converted = mb_convert_encoding($this->string, $this->destination, $from);
91
        } catch (\Exception $msg) {
92
            return false;
93
        }
94
        return $converted;
95
    }
96
97
    /**
98
     * mb_internal_encoding
99
     *
100
     * @return bool
101
     */
102
    protected function fallback()
103
    {
104
        try {
105
            mb_internal_encoding($this->destination);
106
        } catch (\Exception $msg) {
107
            mb_internal_encoding($this->source);
108
            return false;
109
        }
110
        return true;
111
    }
112
}
113