-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathphp-functions.php
More file actions
191 lines (170 loc) · 5.38 KB
/
php-functions.php
File metadata and controls
191 lines (170 loc) · 5.38 KB
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
<?php
declare(strict_types=1);
namespace PhpSpellcheck;
use PhpSpellcheck\Exception\FilesystemException;
use PhpSpellcheck\Exception\JsonException;
use PhpSpellcheck\Exception\PcreException;
use PhpSpellcheck\Exception\PspellException;
use PSpell\Config;
use PSpell\Dictionary;
/**
* @template TFlags as int
*
* @param mixed $matches
* @param TFlags $flags
*
* @param-out (
* TFlags is 1
* ? array<list<string>>
* : (TFlags is 2
* ? list<array<string>>
* : (TFlags is 256|257
* ? array<list<array{string, int}>>
* : (TFlags is 258
* ? list<array<array{string, int}>>
* : (TFlags is 512|513
* ? array<list<?string>>
* : (TFlags is 514
* ? list<array<?string>>
* : (TFlags is 770
* ? list<array<array{?string, int}>>
* : (TFlags is 0 ? array<list<string>> : array<mixed>)
* )
* )
* )
* )
* )
* )
* ) $matches
*/
function preg_match_all(string $pattern, string $subject, &$matches = [], int $flags = 1, int $offset = 0): int
{
error_clear_last();
$safeResult = \preg_match_all($pattern, $subject, $matches, $flags, $offset);
if ($safeResult === false) {
throw PcreException::createFromPhpError();
}
return $safeResult;
}
/**
* @param string|string[] $pattern
* @param array<float|int|string>|string $replacement
* @param array<float|int|string>|string $subject
*
* @param-out 0|positive-int $count
*
* @return ($subject is array ? array<string> : string)
*/
function preg_replace(array|string $pattern, array|string $replacement, array|string $subject, int $limit = -1, ?int &$count = null): array|string
{
error_clear_last();
$result = \preg_replace($pattern, $replacement, $subject, $limit, $count);
if (preg_last_error() !== PREG_NO_ERROR || $result === null) {
throw PcreException::createFromPhpError();
}
return $result;
}
/**
* @template TFlags as int-mask<0, 256, 512>
*
* @param mixed $matches
* @param TFlags $flags
*
* @param-out (
* TFlags is 256
* ? array<array-key, array{string, 0|positive-int}|array{'', -1}>
* : (TFlags is 512
* ? array<array-key, string|null>
* : (TFlags is 768
* ? array<array-key, array{string, 0|positive-int}|array{null, -1}>
* : array<array-key, string>
* )
* )
* ) $matches
*
* @return 0|1
*/
function preg_match(string $pattern, string $subject, &$matches = [], int $flags = 0, int $offset = 0)
{
error_clear_last();
$safeResult = \preg_match($pattern, $subject, $matches, $flags, $offset);
if ($safeResult === false) {
throw PcreException::createFromPhpError();
}
return $safeResult;
}
/**
* @param ?resource $context
* @param int<0, max>|null $length
*/
function file_get_contents(string $filename, bool $use_include_path = false, $context = null, int $offset = 0, ?int $length = null): string
{
error_clear_last();
if ($length !== null) {
$safeResult = \file_get_contents($filename, $use_include_path, $context, $offset, $length);
} elseif ($offset !== 0) {
$safeResult = \file_get_contents($filename, $use_include_path, $context, $offset);
} elseif ($context !== null) {
$safeResult = \file_get_contents($filename, $use_include_path, $context);
} else {
$safeResult = \file_get_contents($filename, $use_include_path);
}
if ($safeResult === false) {
throw FilesystemException::createFromPhpError();
}
return $safeResult;
}
/**
* @param ?resource $context
*/
function file_put_contents(string $filename, mixed $data, int $flags = 0, $context = null): int
{
error_clear_last();
if ($context !== null) {
$safeResult = \file_put_contents($filename, $data, $flags, $context);
} else {
$safeResult = \file_put_contents($filename, $data, $flags);
}
if ($safeResult === false) {
throw FilesystemException::createFromPhpError();
}
return $safeResult;
}
function realpath(string $path): string
{
error_clear_last();
$safeResult = \realpath($path);
if ($safeResult === false) {
throw FilesystemException::createFromPhpError();
}
return $safeResult;
}
/**
* @param int<1, max> $flags
*/
function json_encode(mixed $value, int $flags = 0, int $depth = 512): string
{
error_clear_last();
$safeResult = \json_encode($value, $flags, $depth);
if ($safeResult === false) {
throw JsonException::createFromPhpError();
}
return $safeResult;
}
function json_decode(string $json, bool $assoc = false, int $depth = 512, int $flags = 0): mixed
{
$data = \json_decode($json, $assoc, $depth, $flags);
if (JSON_ERROR_NONE !== json_last_error()) {
throw JsonException::createFromPhpError();
}
return $data;
}
function pspell_new_config(Config $config): Dictionary
{
error_clear_last();
$result = \pspell_new_config($config);
if ($result === false) {
throw PspellException::createFromPhpError();
}
return $result;
}