forked from phpfui/string-encoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvert.php
More file actions
150 lines (126 loc) · 4.17 KB
/
Copy pathConvert.php
File metadata and controls
150 lines (126 loc) · 4.17 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
<?php
declare(strict_types=1);
namespace StringEncoder\MB;
use StringEncoder\Contracts\ConvertReadInterface;
use StringEncoder\Contracts\ConvertWriteInterface;
use StringEncoder\Contracts\DTO\EncodingDTOInterface;
use StringEncoder\Contracts\DTO\MBStringDTOInterface;
use StringEncoder\Contracts\OptionsInterface;
use StringEncoder\DTO\MBStringDTO;
use StringEncoder\Exceptions\ContentsFailedException;
use StringEncoder\Exceptions\ConvertNoValueException;
use StringEncoder\Exceptions\InvalidEncodingException;
use StringEncoder\MB\UTF8\Bom;
use StringEncoder\Options;
class Convert implements ConvertWriteInterface, ConvertReadInterface
{
/**
* @var ?EncodingDTOInterface
*/
private $sourceEncoding;
/**
* @var EncodingDTOInterface
*/
private $targetEncoding;
/**
* @var ?MBStringDTOInterface
*/
private $mbStringDTO;
/**
* @var OptionsInterface
*/
private $options;
/**
* @throws InvalidEncodingException
*/
public function __construct(
?EncodingDTOInterface $sourceEncoding = null,
?EncodingDTOInterface $targetEncoding = null,
?OptionsInterface $options = null
)
{
if ($options === null) {
$options = new Options();
}
$this->options = $options;
if ($targetEncoding === null) {
// apply default target encoding
$targetEncoding = $this->options->getDefaultTargetEncoding();
}
$this->sourceEncoding = $sourceEncoding;
$this->targetEncoding = $targetEncoding;
}
/**
* @throws InvalidEncodingException
*/
public function fromString(string $value): ConvertWriteInterface
{
$this->convert($value);
return $this;
}
/**
* @throws ConvertNoValueException
*/
public function toString(): string
{
if ($this->mbStringDTO === null) {
throw new ConvertNoValueException('No value set for call to convert to string.');
}
return $this->mbStringDTO->getString();
}
/**
* @throws ContentsFailedException
* @throws InvalidEncodingException
*/
public function fromFile(string $filePath): ConvertWriteInterface
{
$content = @\file_get_contents($filePath);
if ($content === false) {
throw new ContentsFailedException('file_get_contents failed and returned false when trying to read "' . $filePath . '".');
}
$this->convert($content);
return $this;
}
/**
* @throws ContentsFailedException
* @throws ConvertNoValueException
*/
public function toFile(string $filePath): void
{
if ($this->mbStringDTO === null) {
throw new ConvertNoValueException('No value set for call to convert to string.');
}
$string = $this->mbStringDTO->getString();
$status = @\file_put_contents($filePath, $string);
if ($status === false) {
throw new ContentsFailedException('file_put_contents failed and returned false when trying to write "' . $filePath . '".');
}
}
/**
* @throws ConvertNoValueException
*/
public function toDTO(): MBStringDTOInterface
{
if ($this->mbStringDTO === null) {
throw new ConvertNoValueException('No value set for call to convert to string.');
}
return $this->mbStringDTO;
}
/**
* @throws InvalidEncodingException
*/
public function convert(string $value): void
{
if ($this->sourceEncoding === null) {
$value = \mb_convert_encoding($value, $this->targetEncoding->getEncoding());
} elseif ($this->sourceEncoding->getEncoding() !== $this->targetEncoding->getEncoding()) {
$value = \mb_convert_encoding($value, $this->targetEncoding->getEncoding(), $this->sourceEncoding->getEncoding());
}
$this->mbStringDTO = MBStringDTO::makeFromString($value, $this->options, $this->targetEncoding);
if ($this->options->isRemoveUTF8BOM() &&
$this->targetEncoding->getEncoding() === 'UTF-8') {
$bom = new Bom();
$this->mbStringDTO = $bom->removeBOM($this->mbStringDTO);
}
}
}