forked from paquettg/string-encoder
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEncoder.php
More file actions
89 lines (74 loc) · 2.34 KB
/
Copy pathEncoder.php
File metadata and controls
89 lines (74 loc) · 2.34 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
<?php
declare(strict_types=1);
namespace StringEncoder\Proxy;
use StringEncoder\Contracts\ConvertReadInterface;
use StringEncoder\Contracts\EncoderInterface;
use StringEncoder\Contracts\OptionsInterface;
use StringEncoder\Contracts\ProxyEncoderInterface;
use StringEncoder\Encoder as EncoderImplementation;
use StringEncoder\Exceptions\InvalidEncodingException;
final class Encoder implements ProxyEncoderInterface
{
/**
* @var EncoderImplementation
*/
private static $encoder = null;
/**
* @throws InvalidEncodingException
*/
public static function mountFromEncoding(string $targetEncoding, ?string $sourceEncoding = null): void
{
$encoder = new EncoderImplementation();
$encoder->setTargetEncoding($targetEncoding);
if ($sourceEncoding !== null) {
$encoder->setSourceEncoding($sourceEncoding);
}
self::mount('Encoder', $encoder);
}
/**
* Call this to mount the static facade. The facade allows you to use
* this object as a $className.
*/
public static function mount(string $className = 'Encoder', ?EncoderImplementation $encoder = null): bool
{
if (!\class_exists($className)) {
\class_alias(__CLASS__, $className);
}
if ($encoder instanceof EncoderImplementation) {
self::$encoder = $encoder;
}
return true;
}
public static function getMountedEncoder(): ?EncoderImplementation
{
return self::$encoder;
}
public static function unload(): void
{
self::$encoder = null;
}
public static function setOptions(OptionsInterface $options): EncoderInterface
{
return self::$encoder->setOptions($options);
}
public static function getTargetEncoding(): ?string
{
return self::$encoder->getTargetEncoding();
}
public static function setTargetEncoding(string $encoding): void
{
self::$encoder->setTargetEncoding($encoding);
}
public static function getSourceEncoding(): ?string
{
return self::$encoder->getSourceEncoding();
}
public static function setSourceEncoding(string $encoding): void
{
self::$encoder->setSourceEncoding($encoding);
}
public static function convert(): ConvertReadInterface
{
return self::$encoder->convert();
}
}