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