forked from phpfui/string-encoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMBStringDTO.php
More file actions
96 lines (83 loc) · 2.37 KB
/
Copy pathMBStringDTO.php
File metadata and controls
96 lines (83 loc) · 2.37 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
<?php
declare(strict_types=1);
namespace StringEncoder\DTO;
use StringEncoder\Contracts\DTO\EncodingDTOInterface;
use StringEncoder\Contracts\DTO\MBStringDTOInterface;
use StringEncoder\Contracts\OptionsInterface;
use StringEncoder\Discovery\ValidatorDiscovery;
use StringEncoder\Exceptions\InvalidEncodingException;
use StringEncoder\MB\Validator;
final class MBStringDTO implements MBStringDTOInterface
{
/**
* @var string
*/
private $string;
/**
* @var EncodingDTOInterface
*/
private $encodingDTO;
/**
* @var OptionsInterface
*/
private $options;
/**
* EncodingDTO constructor.
*
* @throws InvalidEncodingException
*/
private function __construct(
string $string,
OptionsInterface $options,
?EncodingDTOInterface $encodingDTO = null,
?Validator $validator = null
)
{
if ($validator === null) {
$validator = ValidatorDiscovery::find();
}
if ($encodingDTO === null) {
$encodingDTO = $options->getDefaultTargetEncoding();
}
if (!$validator->validateString($string, $encodingDTO)) {
throw new InvalidEncodingException('String "' . $string . '" is not the current encoding of "' . $encodingDTO->getEncoding() . '".');
}
$this->options = $options;
$this->encodingDTO = $encodingDTO;
$this->string = $string;
}
/**
* @throws InvalidEncodingException
*
* @internal
*/
public static function makeFromString(
string $string,
OptionsInterface $options,
?EncodingDTOInterface $encodingDTO = null,
?Validator $validator = null
): MBStringDTOInterface {
return new MBStringDTO($string, $options, $encodingDTO, $validator);
}
/**
* @throws InvalidEncodingException
*
* @internal
*/
public static function makeFromDTO(string $string, MBStringDTOInterface $MBStringDTO): MBStringDTOInterface
{
return new MBStringDTO($string, $MBStringDTO->getOptions(), $MBStringDTO->getEncodingDTO());
}
public function getString(): string
{
return $this->string;
}
public function getEncodingDTO(): EncodingDTOInterface
{
return $this->encodingDTO;
}
public function getOptions(): OptionsInterface
{
return $this->options;
}
}