1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\UploadPerPartes\Target\Local\ContentDecoders; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\UploadPerPartes\Traits\TLangInit; |
7
|
|
|
use kalanis\UploadPerPartes\UploadException; |
8
|
|
|
use kalanis\UploadPerPartes\Interfaces; |
9
|
|
|
use ReflectionClass; |
10
|
|
|
use ReflectionException; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Factory |
15
|
|
|
* @package kalanis\UploadPerPartes\Target\Local\ContentDecoders |
16
|
|
|
* Which decoder will be used for reconstruction of data passed from client |
17
|
|
|
*/ |
18
|
|
|
class Factory |
19
|
|
|
{ |
20
|
|
|
use TLangInit; |
21
|
|
|
|
22
|
|
|
public const FORMAT_BASE64 = 'base64'; |
23
|
|
|
public const FORMAT_HEX = 'hex'; |
24
|
|
|
public const FORMAT_RAW = 'raw'; |
25
|
|
|
|
26
|
|
|
/** @var array<string|int, class-string<Interfaces\IContentDecoder>> */ |
|
|
|
|
27
|
|
|
protected array $map = [ |
28
|
|
|
self::FORMAT_BASE64 => Base64::class, |
29
|
|
|
self::FORMAT_HEX => Hex::class, |
30
|
|
|
self::FORMAT_RAW => Raw::class, |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $method |
35
|
|
|
* @throws UploadException |
36
|
|
|
* @return Interfaces\IContentDecoder |
37
|
|
|
*/ |
38
|
9 |
|
public function getDecoder(string $method): Interfaces\IContentDecoder |
39
|
|
|
{ |
40
|
9 |
|
$variant = empty($method) ? self::FORMAT_BASE64 : $method; |
41
|
9 |
|
if (isset($this->map[strval($variant)])) { |
42
|
6 |
|
return $this->initDefined($this->map[strval($variant)]); |
43
|
|
|
} |
44
|
3 |
|
return $this->initDefined(strval($variant)); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param object $variant |
49
|
|
|
* @throws UploadException |
50
|
|
|
* @return Interfaces\IContentDecoder |
51
|
|
|
*/ |
52
|
7 |
|
protected function checkObject(object $variant): Interfaces\IContentDecoder |
53
|
|
|
{ |
54
|
7 |
|
if ($variant instanceof Interfaces\IContentDecoder) { |
55
|
6 |
|
return $variant; |
56
|
|
|
} |
57
|
1 |
|
throw new UploadException($this->getUppLang()->uppDecoderVariantIsWrong(get_class($variant))); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $variant |
62
|
|
|
* @throws UploadException |
63
|
|
|
* @return Interfaces\IContentDecoder |
64
|
|
|
*/ |
65
|
9 |
|
protected function initDefined(string $variant): Interfaces\IContentDecoder |
66
|
|
|
{ |
67
|
|
|
try { |
68
|
|
|
/** @var class-string<Interfaces\IContentDecoder> $variant */ |
69
|
9 |
|
$ref = new ReflectionClass($variant); |
70
|
8 |
|
if ($ref->isInstantiable()) { |
71
|
7 |
|
return $this->checkObject($ref->newInstance($this->getUppLang())); |
72
|
|
|
} |
73
|
1 |
|
throw new UploadException($this->getUppLang()->uppDecoderVariantIsWrong($variant)); |
74
|
3 |
|
} catch (ReflectionException $ex) { |
75
|
1 |
|
throw new UploadException($ex->getMessage(), $ex->getCode(), $ex); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|