forked from paquettg/string-encoder
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRegex.php
More file actions
66 lines (57 loc) · 1.71 KB
/
Copy pathRegex.php
File metadata and controls
66 lines (57 loc) · 1.71 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
<?php
declare(strict_types=1);
namespace StringEncoder\MB;
use StringEncoder\Contracts\DTO\MBStringDTOInterface;
use StringEncoder\Contracts\MB\RegexInterface;
use StringEncoder\DTO\EncodingDTO;
use StringEncoder\DTO\MBStringDTO;
use StringEncoder\Exceptions\InvalidEncodingException;
class Regex implements RegexInterface
{
/**
* @throws InvalidEncodingException
*/
public function setEncoding(string $encoding): void
{
$encodingDTO = EncodingDTO::makeFromString($encoding);
\mb_regex_encoding($encodingDTO->getEncoding());
}
public function getEncoding(): string
{
return \mb_regex_encoding();
}
/**
* @throws InvalidEncodingException
*/
public function replace(
string $pattern,
string $replace,
MBStringDTOInterface $MBStringDTO,
bool $ignoreCase = false
): MBStringDTOInterface {
if ($ignoreCase) {
$value = \mb_eregi_replace($pattern, $replace, $MBStringDTO->getString());
} else {
$value = \mb_ereg_replace($pattern, $replace, $MBStringDTO->getString());
}
return MBStringDTO::makeFromDTO($value, $MBStringDTO);
}
/**
* @param string[] $patterns
* @param string[] $replaces
*
* @throws InvalidEncodingException
*/
public function replaceMultiple(
array $patterns,
array $replaces,
MBStringDTOInterface $MBStringDTO,
bool $ignoreCase = false
): MBStringDTOInterface {
foreach ($patterns as $key => $pattern) {
$replace = $replaces[$key];
$MBStringDTO = $this->replace($pattern, $replace, $MBStringDTO, $ignoreCase);
}
return $MBStringDTO;
}
}