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, 'errorHandlerCallback'], 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
|
|
|
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
|
|
|
return $converted; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* iconv |
94
|
|
|
* @link http://php.net/manual/en/function.iconv.php |
95
|
|
|
* |
96
|
|
|
* @param string $outSuffix |
97
|
|
|
* @return string|false |
98
|
|
|
*/ |
99
|
|
|
public function iconv($outSuffix = '//TRANSLIT//IGNORE') |
100
|
|
|
{ |
101
|
|
|
try { |
102
|
|
|
$converted = iconv($this->encoding, self::ENCODING . $outSuffix, $this->string); |
103
|
|
|
} catch (\Exception $e) { |
104
|
|
|
return false; |
105
|
|
|
} |
106
|
|
|
return $converted; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* xml |
111
|
|
|
* @link http://php.net/manual/en/function.utf8-encode.php |
112
|
|
|
* |
113
|
|
|
* @return string|false |
114
|
|
|
*/ |
115
|
|
|
public function xml() |
116
|
|
|
{ |
117
|
|
|
if (strtoupper($this->encoding) !== 'ISO-8859-1') { |
118
|
|
|
return false; |
119
|
|
|
} |
120
|
|
|
try { |
121
|
|
|
$converted = utf8_encode($this->string); |
122
|
|
|
} catch (\Exception $e) { |
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
return $converted; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* mbstring |
130
|
|
|
* @link http://php.net/manual/en/function.mb-convert-encoding.php |
131
|
|
|
* |
132
|
|
|
* @param array|string|null $fromOverride |
133
|
|
|
* @return string|false |
134
|
|
|
*/ |
135
|
|
|
public function mbstring($fromOverride = null) |
136
|
|
|
{ |
137
|
|
|
try { |
138
|
|
|
$converted = mb_convert_encoding($this->string, self::ENCODING, $fromOverride === null ? $this->encoding : $fromOverride); |
139
|
|
|
} catch (\Exception $e) { |
140
|
|
|
return false; |
141
|
|
|
} |
142
|
|
|
return $converted; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Custom error handler |
147
|
|
|
* |
148
|
|
|
* @param int $errNo |
149
|
|
|
* @param string $errStr |
150
|
|
|
* @param string $errFile |
151
|
|
|
* @param string $errLine |
152
|
|
|
* @return bool |
153
|
|
|
*/ |
154
|
|
|
public function errorHandlerCallback($errNo, $errStr, $errFile, $errLine) |
155
|
|
|
{ |
156
|
|
|
$this->errors[microtime(true)] = "lvl: " . $errNo . " | msg:" . $errStr . " | file:" . $errFile . " | ln:" . $errLine; |
157
|
|
|
return true; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|