-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncoding.php
216 lines (162 loc) · 5.23 KB
/
Encoding.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
declare(strict_types=1);
/*
* This file is part of the QuidPHP package <https://quidphp.com>
* Author: Pierre-Philippe Emond <emondpph@gmail.com>
* License: https://github.com/quidphp/base/blob/master/LICENSE
*/
namespace Quid\Base;
// encoding
// class which contains methods related to string encoding (manages mb overload)
final class Encoding extends Root
{
// config
protected static array $config = [
'charset'=>null, // charset
'charsetMb'=>['UTF-8'], // charset qui retourne vrai à la méthode isCharsetMb
'mb'=>false // active les fonctions multibyte
];
// is
// retourne vrai si la chaîne est valide pour un encodage spécifique
final public static function is($value,?string $charset=null):bool
{
return (is_string($value))? mb_check_encoding($value,self::getCharset($charset)):false;
}
// isMb
// retourne vrai si la valeur est string et multibyte
final public static function isMb($value):bool
{
return is_string($value) && strlen($value) !== mb_strlen($value,self::$config['charset']);
}
// isMbs
// retourne vrai si une des chaînes est multibyte
final public static function isMbs(...$values):bool
{
return Arr::some($values,fn($value) => self::isMb($value));
}
// isCharsetMb
// retourne vrai si le charset est multibyte
final public static function isCharsetMb($value):bool
{
return is_string($value) && Arr::in($value,self::$config['charsetMb'],false);
}
// exists
// retourne vrai si l'encodage existe
final public static function exists(string $value):bool
{
return in_array($value,mb_list_encodings(),true);
}
// get
// retourne l'encodage de la chaîne
final public static function get(string $value,$list=null,bool $strict=false):?string
{
return mb_detect_encoding($value,$list,$strict);
}
// set
// change l'encodage de la chaîne
final public static function set(string $return,?string $charset=null,?string $from=null)
{
$charset = self::getCharset($charset);
$from = self::getCharset($from);
if($charset !== $from)
$return = mb_convert_encoding($return,$charset,$from);
return $return;
}
// scrub
// permet de remplacer les caractères illégaux en ?
final public static function scrub(string $return,?string $charset=null):string
{
return mb_scrub($return,self::getCharset($charset));
}
// getInternal
// retourne l'encoding interne de l'extension mbstring
final public static function getInternal():string
{
return self::$config['charset'] = mb_internal_encoding();
}
// setInternal
// change l'encoding interne de l'extension mbstring
final public static function setInternal(string $charset):bool
{
$return = mb_internal_encoding($charset);
if($return === true)
self::$config['charset'] = $charset;
return $return;
}
// getCharset
// retourne le charset actuel ou celui donné en argument si c'est une string
final public static function getCharset(?string $charset=null):string
{
return (is_string($charset))? $charset:self::$config['charset'];
}
// setCharset
// change le charset actuel, change aussi dans la classe str
final public static function setCharset(string $value):void
{
self::$config['charset'] = $value;
Str::setCharset($value);
}
// getMb
// retourne si multibyte est true ou false
final public static function getMb($mb=null,$value=null):bool
{
$return = false;
if(is_bool($mb))
$return = $mb;
elseif(is_bool(self::$config['mb']))
$return = self::$config['mb'];
elseif(is_string($value))
$return = self::isMb($value);
return $return;
}
// getMbs
// retourne si multibyte est true ou false
// si une des string contient un caractère multibyte, alors retourne true
final public static function getMbs($mb=null,...$values):bool
{
$return = false;
if(is_bool($mb))
$return = $mb;
elseif(is_bool(self::$config['mb']))
$return = self::$config['mb'];
elseif(!empty($values))
$return = self::isMbs(...$values);
return $return;
}
// setMb
// change la valeur par défaut de mb
final public static function setMb($mb):void
{
if(is_string($mb))
$mb = self::isMb($mb);
if(is_bool($mb) || $mb === null)
self::$config['mb'] = $mb;
}
// toUtf8
// alias pour utf8_encode
final public static function toUtf8(string $value):string
{
return utf8_encode($value);
}
// fromUtf8
// alias pour utf8_decode
final public static function fromUtf8(string $value):string
{
return utf8_decode($value);
}
// info
// retourne les informations sur l'extension mbstring
final public static function info():array
{
return mb_get_info();
}
// all
// affiche tous les encodages supportés
final public static function all():array
{
return mb_list_encodings();
}
}
// init
Encoding::setCharset('UTF-8');
?>