1
|
|
|
<?php |
2
|
|
|
namespace vipnytt\RobotsTxtParser\Handler; |
3
|
|
|
|
4
|
|
|
use vipnytt\RobotsTxtParser\RobotsTxtInterface; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class EncodingHandler |
8
|
|
|
* |
9
|
|
|
* @package vipnytt\RobotsTxtParser\Handler |
10
|
|
|
*/ |
11
|
|
|
class EncodingHandler implements RobotsTxtInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Errors |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected $errors = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* String to convert |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $string; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* String encoding |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $encoding; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* EncodingHandler constructor. |
33
|
|
|
* |
34
|
|
|
* @param string $string |
35
|
|
|
* @param string $encoding |
36
|
|
|
*/ |
37
|
|
|
public function __construct($string, $encoding) |
38
|
|
|
{ |
39
|
|
|
$this->string = $string; |
40
|
|
|
$this->encoding = $encoding; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Auto mode |
45
|
|
|
* |
46
|
|
|
* @return string|false |
47
|
|
|
*/ |
48
|
|
|
public function auto() |
49
|
|
|
{ |
50
|
|
|
if (strtoupper($this->encoding) === self::ENCODING) { |
51
|
|
|
return $this->string; |
52
|
|
|
} |
53
|
|
|
set_error_handler([$this, 'customErrorHandler'], E_NOTICE | E_WARNING); |
54
|
|
|
foreach ([ |
55
|
|
|
'intl', |
56
|
|
|
'iconv', |
57
|
|
|
'xml', |
58
|
|
|
'mbstring', |
59
|
|
|
] as $extension |
60
|
|
|
) { |
61
|
|
|
$last = end($this->errors); |
62
|
|
|
if ( |
63
|
|
|
extension_loaded($extension) && |
64
|
|
|
($result = call_user_func([$this, $extension])) !== false && |
65
|
|
|
$last === end($this->errors) |
66
|
|
|
) { |
67
|
|
|
restore_error_handler(); |
68
|
|
|
return $result; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
restore_error_handler(); |
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* intl |
77
|
|
|
* @link http://php.net/manual/en/uconverter.convert.php |
78
|
|
|
* |
79
|
|
|
* @return string|false |
80
|
|
|
*/ |
81
|
|
View Code Duplication |
public function intl() |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
try { |
84
|
|
|
$uConverter = new \UConverter(self::ENCODING, $this->encoding); |
85
|
|
|
$converted = $uConverter->convert($this->string); |
86
|
|
|
} catch (\Exception $e) { |
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
mb_internal_encoding(self::ENCODING); |
90
|
|
|
return $converted; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* iconv |
95
|
|
|
* @link http://php.net/manual/en/function.iconv.php |
96
|
|
|
* |
97
|
|
|
* @param string $outSuffix |
98
|
|
|
* @return string|false |
99
|
|
|
*/ |
100
|
|
View Code Duplication |
public function iconv($outSuffix = '//TRANSLIT//IGNORE') |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
try { |
103
|
|
|
$converted = iconv($this->encoding, self::ENCODING . $outSuffix, $this->string); |
104
|
|
|
} catch (\Exception $e) { |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
mb_internal_encoding(self::ENCODING); |
108
|
|
|
return $converted; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* xml |
113
|
|
|
* @link http://php.net/manual/en/function.utf8-encode.php |
114
|
|
|
* |
115
|
|
|
* @return string|false |
116
|
|
|
*/ |
117
|
|
View Code Duplication |
public function xml() |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
if (strtoupper($this->encoding) !== 'ISO-8859-1') { |
120
|
|
|
return false; |
121
|
|
|
} |
122
|
|
|
try { |
123
|
|
|
$converted = utf8_encode($this->string); |
124
|
|
|
} catch (\Exception $e) { |
125
|
|
|
return false; |
126
|
|
|
} |
127
|
|
|
mb_internal_encoding(self::ENCODING); |
128
|
|
|
return $converted; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* mbstring |
133
|
|
|
* @link http://php.net/manual/en/function.mb-convert-encoding.php |
134
|
|
|
* |
135
|
|
|
* @param array|string|null $fromOverride |
136
|
|
|
* @return string|false |
137
|
|
|
*/ |
138
|
|
|
public function mbstring($fromOverride = null) |
139
|
|
|
{ |
140
|
|
|
try { |
141
|
|
|
$converted = mb_convert_encoding($this->string, self::ENCODING, $fromOverride === null ? $this->encoding : $fromOverride); |
142
|
|
|
} catch (\Exception $e) { |
143
|
|
|
return false; |
144
|
|
|
} |
145
|
|
|
mb_internal_encoding(self::ENCODING); |
146
|
|
|
return $converted; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Custom error handler |
151
|
|
|
* |
152
|
|
|
* @param int $errNo |
153
|
|
|
* @param string $errStr |
154
|
|
|
* @param string $errFile |
155
|
|
|
* @param string $errLine |
156
|
|
|
* @return bool |
157
|
|
|
*/ |
158
|
|
|
protected function customErrorHandler($errNo, $errStr, $errFile, $errLine) |
159
|
|
|
{ |
160
|
|
|
switch ($errNo) { |
161
|
|
|
case E_NOTICE: |
162
|
|
|
case E_WARNING: |
163
|
|
|
$this->errors[] = "lvl: " . $errNo . " | msg:" . $errStr . " | file:" . $errFile . " | ln:" . $errLine; |
164
|
|
|
return true; |
165
|
|
|
} |
166
|
|
|
return false; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
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.