-
Notifications
You must be signed in to change notification settings - Fork 0
/
MB.php
executable file
·147 lines (135 loc) · 3.71 KB
/
MB.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
<?php
/**
* This is a Multibyte library
* It is tho recommended to install mb on the server if is missing
*/
namespace MaplePHP\DTO;
use ErrorException;
class MB
{
private string $value;
private Iconv $iconv;
private bool $disableVanilla = false;
/**
* The input string value
*
* @param string $value
*/
public function __construct(string $value)
{
$this->value = $value;
$this->iconv = new Iconv($this->value);
}
/**
* Get value as string
*
* @return string
*/
public function __toString(): string
{
return (string)$this->getValue();
}
/**
* Get value
* @return string|false
*/
public function getValue(): string|false
{
return $this->value;
}
/**
* Will disable vanilla iconv function, used mostly for testing.
*
* @param bool $disable
* @return void
*/
public function disableVanilla(bool $disable): void
{
$this->disableVanilla = $disable;
$this->iconv->disableVanilla($disable);
}
/**
* mb_convert_encoding polyfill (immutable)
*
* @param string $fromEncoding
* @param string $toEncoding
* @return $this
* @throws ErrorException
*/
public function encode(string $fromEncoding, string $toEncoding): self
{
$inst = clone $this;
if(function_exists('mb_convert_encoding') && !$inst->disableVanilla) {
$inst->value = mb_convert_encoding($inst->value, $toEncoding, $fromEncoding);
} else {
$inst->iconv = $inst->iconv->encode($fromEncoding, $toEncoding);
$inst->value = $inst->iconv->getValue();
}
return $inst;
}
/**
* mb_strlen polyfill
*
* @param string|null $encoding
* @return int|false
* @throws ErrorException
*/
public function strlen(?string $encoding = null): int|false
{
if(function_exists('mb_strlen') && !$this->disableVanilla) {
return mb_strlen($this->value, $encoding);
}
return $this->iconv->strlen($encoding);
}
/**
* mb_substr polyfill
*
* @param int $start
* @param int|null $length
* @param string|null $encoding
* @return self
* @throws ErrorException
*/
public function substr(int $start, ?int $length = null, ?string $encoding = null): self
{
$inst = clone $this;
if(function_exists('mb_substr') && !$inst->disableVanilla) {
$inst->value = mb_substr($inst->value, $start, $length, $encoding);
} else {
$this->iconv = $this->iconv->substr($start, $length, $encoding);
$inst->value = $this->iconv->getValue();
}
return $inst;
}
/**
* mb_strpos polyfill
*
* @param string $needle
* @param int $offset
* @param string|null $encoding
* @return false|int
* @throws ErrorException
*/
public function strpos(string $needle, int $offset = 0, ?string $encoding = null): false|int
{
if(function_exists('mb_strpos') && !$this->disableVanilla) {
return mb_strpos($this->value, $needle, $offset, $encoding);
}
return $this->iconv->strpos($needle, $offset, $encoding);
}
/**
* mb_strrpos polyfill
*
* @param string $needle
* @param string|null $encoding
* @return false|int
* @throws ErrorException
*/
public function strrpos(string $needle, ?string $encoding = null): false|int
{
if(function_exists('mb_strrpos') && !$this->disableVanilla) {
return mb_strrpos($this->value, $needle, 0, $encoding);
}
return $this->iconv->strrpos($needle, $encoding);
}
}