1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\UploadPerPartes\Target\Local\DrivingFile\DataModifiers; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\UploadPerPartes\Traits\TLangInit; |
7
|
|
|
use kalanis\UploadPerPartes\UploadException; |
8
|
|
|
use ReflectionClass; |
9
|
|
|
use ReflectionException; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class EncodeFactory |
14
|
|
|
* @package kalanis\UploadPerPartes\Target\Local\DrivingFile\DataModifiers |
15
|
|
|
* Select correct type of pack form for passing data there and back |
16
|
|
|
*/ |
17
|
|
|
class Factory |
18
|
|
|
{ |
19
|
|
|
use TLangInit; |
20
|
|
|
|
21
|
|
|
public const VARIANT_CLEAR = 1; |
22
|
|
|
public const VARIANT_BASE64 = 2; |
23
|
|
|
public const VARIANT_HEX = 3; |
24
|
|
|
|
25
|
|
|
/** @var array<string|int, class-string<AModifier>> */ |
|
|
|
|
26
|
|
|
protected array $map = [ |
27
|
|
|
self::VARIANT_CLEAR => Clear::class, |
28
|
|
|
self::VARIANT_BASE64 => Base64::class, |
29
|
|
|
self::VARIANT_HEX => Hex::class, |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param int|class-string<AModifier>|object|string|null $variant |
|
|
|
|
34
|
|
|
* @throws UploadException |
35
|
|
|
* @return AModifier |
36
|
|
|
*/ |
37
|
27 |
|
public function getDataModifier($variant): AModifier |
38
|
|
|
{ |
39
|
27 |
|
if (is_object($variant)) { |
40
|
19 |
|
return $this->checkObject($variant); |
41
|
|
|
} |
42
|
8 |
|
if (isset($this->map[strval($variant)])) { |
43
|
3 |
|
return $this->initDefined($this->map[strval($variant)]); |
44
|
|
|
} |
45
|
5 |
|
if (is_string($variant)) { |
46
|
4 |
|
return $this->initDefined($variant); |
47
|
|
|
} |
48
|
1 |
|
throw new UploadException($this->getUppLang()->uppDataModifierVariantNotSet()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param object $variant |
53
|
|
|
* @throws UploadException |
54
|
|
|
* @return AModifier |
55
|
|
|
*/ |
56
|
24 |
|
protected function checkObject(object $variant): AModifier |
57
|
|
|
{ |
58
|
24 |
|
if ($variant instanceof AModifier) { |
59
|
23 |
|
return $variant; |
60
|
|
|
} |
61
|
1 |
|
throw new UploadException($this->getUppLang()->uppDataModifierVariantIsWrong(get_class($variant))); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $variant |
66
|
|
|
* @throws UploadException |
67
|
|
|
* @return AModifier |
68
|
|
|
*/ |
69
|
7 |
|
protected function initDefined(string $variant): AModifier |
70
|
|
|
{ |
71
|
|
|
try { |
72
|
|
|
/** @var class-string<AModifier> $variant */ |
73
|
7 |
|
$ref = new ReflectionClass($variant); |
74
|
6 |
|
if ($ref->isInstantiable()) { |
75
|
5 |
|
return $this->checkObject($ref->newInstance($this->getUppLang())); |
76
|
|
|
} |
77
|
1 |
|
throw new UploadException($this->getUppLang()->uppDataModifierVariantIsWrong($variant)); |
78
|
2 |
|
} catch (ReflectionException $ex) { |
79
|
1 |
|
throw new UploadException($ex->getMessage(), $ex->getCode(), $ex); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|