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
|
|
|
use ErrorHandlerTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* String to convert |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $string; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* String encoding |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $encoding; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* EncodingHandler constructor. |
38
|
|
|
* |
39
|
|
|
* @param string $string |
40
|
|
|
* @param string $encoding |
41
|
|
|
*/ |
42
|
|
|
public function __construct($string, $encoding) |
43
|
|
|
{ |
44
|
|
|
$this->string = $string; |
45
|
|
|
$this->encoding = $encoding; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Auto mode |
50
|
|
|
* |
51
|
|
|
* @return string|false |
52
|
|
|
*/ |
53
|
|
|
public function auto() |
54
|
|
|
{ |
55
|
|
|
if (strtoupper($this->encoding) === self::ENCODING) { |
56
|
|
|
return $this->string; |
57
|
|
|
} |
58
|
|
|
set_error_handler([$this, 'errorHandlerCallback'], E_NOTICE | E_WARNING); |
59
|
|
|
foreach ([ |
60
|
|
|
'intl', |
61
|
|
|
'iconv', |
62
|
|
|
'xml', |
63
|
|
|
'mbstring', |
64
|
|
|
] as $extension) { |
65
|
|
|
$last = end($this->errorLog); |
66
|
|
|
if (extension_loaded($extension) && |
67
|
|
|
($result = call_user_func([$this, $extension])) !== false && |
68
|
|
|
$last === end($this->errorLog) |
69
|
|
|
) { |
70
|
|
|
restore_error_handler(); |
71
|
|
|
return $result; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
restore_error_handler(); |
75
|
|
|
return false; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* intl |
80
|
|
|
* @link http://php.net/manual/en/uconverter.convert.php |
81
|
|
|
* |
82
|
|
|
* @return string|false |
83
|
|
|
*/ |
84
|
|
|
public function intl() |
85
|
|
|
{ |
86
|
|
|
try { |
87
|
|
|
$uConverter = new \UConverter(self::ENCODING, $this->encoding); |
88
|
|
|
$converted = $uConverter->convert($this->string); |
89
|
|
|
} catch (\Exception $e) { |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
return $converted; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* iconv |
97
|
|
|
* @link http://php.net/manual/en/function.iconv.php |
98
|
|
|
* |
99
|
|
|
* @param string $outSuffix |
100
|
|
|
* @return string|false |
101
|
|
|
*/ |
102
|
|
|
public function iconv($outSuffix = '//TRANSLIT//IGNORE') |
103
|
|
|
{ |
104
|
|
|
try { |
105
|
|
|
$converted = iconv($this->encoding, self::ENCODING . $outSuffix, $this->string); |
106
|
|
|
} catch (\Exception $e) { |
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
return $converted; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* xml |
114
|
|
|
* @link http://php.net/manual/en/function.utf8-encode.php |
115
|
|
|
* |
116
|
|
|
* @return string|false |
117
|
|
|
*/ |
118
|
|
|
public function xml() |
119
|
|
|
{ |
120
|
|
|
if (strtoupper($this->encoding) !== 'ISO-8859-1') { |
121
|
|
|
return false; |
122
|
|
|
} |
123
|
|
|
try { |
124
|
|
|
$converted = utf8_encode($this->string); |
125
|
|
|
} catch (\Exception $e) { |
126
|
|
|
return false; |
127
|
|
|
} |
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
|
|
|
return $converted; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|