1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* vipnytt/RobotsTxtParser |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/VIPnytt/RobotsTxtParser |
6
|
|
|
* @license https://github.com/VIPnytt/RobotsTxtParser/blob/master/LICENSE The MIT License (MIT) |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace vipnytt\RobotsTxtParser\Handler; |
10
|
|
|
|
11
|
|
|
use vipnytt\RobotsTxtParser\RobotsTxtInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class EncodingHandler |
15
|
|
|
* |
16
|
|
|
* @link http://www.ietf.org/rfc/rfc3986.txt |
17
|
|
|
* |
18
|
|
|
* @package vipnytt\RobotsTxtParser\Handler |
19
|
|
|
*/ |
20
|
|
|
class EncodingHandler implements RobotsTxtInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* String to convert |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $string; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* String encoding |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $encoding; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* EncodingHandler constructor. |
36
|
|
|
* |
37
|
|
|
* @param string $string |
38
|
|
|
* @param string $encoding |
39
|
|
|
*/ |
40
|
|
|
public function __construct($string, $encoding) |
41
|
|
|
{ |
42
|
|
|
$this->string = $string; |
43
|
|
|
$this->encoding = $encoding; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Auto mode |
48
|
|
|
* |
49
|
|
|
* @return string|false |
50
|
|
|
*/ |
51
|
|
|
public function auto() |
52
|
|
|
{ |
53
|
|
|
if (strtoupper($this->encoding) === self::ENCODING) { |
54
|
|
|
return $this->string; |
55
|
|
|
} |
56
|
|
|
$errorHandler = new ErrorHandler(); |
57
|
|
|
set_error_handler([$errorHandler, 'callback'], E_NOTICE | E_WARNING); |
58
|
|
|
foreach ([ |
59
|
|
|
'intl', |
60
|
|
|
'iconv', |
61
|
|
|
'xml', |
62
|
|
|
'mbstring', |
63
|
|
|
] as $extension) { |
64
|
|
|
$last = $errorHandler->getLast(); |
65
|
|
|
if (extension_loaded($extension) && |
66
|
|
|
($result = call_user_func([$this, $extension])) !== false && |
67
|
|
|
$last === $errorHandler->getLast() |
68
|
|
|
) { |
69
|
|
|
restore_error_handler(); |
70
|
|
|
return $result; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
restore_error_handler(); |
74
|
|
|
return false; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* intl |
79
|
|
|
* @link http://php.net/manual/en/uconverter.convert.php |
80
|
|
|
* |
81
|
|
|
* @return string|false |
82
|
|
|
*/ |
83
|
|
|
public function intl() |
84
|
|
|
{ |
85
|
|
|
try { |
86
|
|
|
$uConverter = new \UConverter(self::ENCODING, $this->encoding); |
87
|
|
|
$converted = $uConverter->convert($this->string, false); |
88
|
|
|
} catch (\Exception $e) { |
89
|
|
|
return false; |
90
|
|
|
} |
91
|
|
|
return $converted; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* iconv |
96
|
|
|
* @link http://php.net/manual/en/function.iconv.php |
97
|
|
|
* |
98
|
|
|
* @param string $outSuffix |
99
|
|
|
* @return string|false |
100
|
|
|
*/ |
101
|
|
|
public function iconv($outSuffix = '//TRANSLIT//IGNORE') |
102
|
|
|
{ |
103
|
|
|
try { |
104
|
|
|
$converted = iconv($this->encoding, self::ENCODING . $outSuffix, $this->string); |
105
|
|
|
} catch (\Exception $e) { |
106
|
|
|
return false; |
107
|
|
|
} |
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
|
|
|
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
|
|
|
return $converted; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* mbstring |
132
|
|
|
* @link http://php.net/manual/en/function.mb-convert-encoding.php |
133
|
|
|
* |
134
|
|
|
* @param array|string|null $fromOverride |
135
|
|
|
* @return string|false |
136
|
|
|
*/ |
137
|
|
|
public function mbstring($fromOverride = null) |
138
|
|
|
{ |
139
|
|
|
try { |
140
|
|
|
$converted = mb_convert_encoding($this->string, self::ENCODING, $fromOverride === null ? $this->encoding : $fromOverride); |
141
|
|
|
} catch (\Throwable $e) { |
142
|
|
|
return false; |
143
|
|
|
} |
144
|
|
|
return $converted; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|