|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\UploadPerPartes\Target\Local\TemporaryStorage\KeyEncoders; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\UploadPerPartes\Traits\TLangInit; |
|
7
|
|
|
use kalanis\UploadPerPartes\Uploader\Config; |
|
8
|
|
|
use kalanis\UploadPerPartes\UploadException; |
|
9
|
|
|
use ReflectionClass; |
|
10
|
|
|
use ReflectionException; |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class Factory |
|
15
|
|
|
* @package kalanis\UploadPerPartes\Target\Local\TemporaryStorage\KeyEncoders |
|
16
|
|
|
* Factory to get file name on temporary storage |
|
17
|
|
|
*/ |
|
18
|
|
|
class Factory |
|
19
|
|
|
{ |
|
20
|
|
|
use TLangInit; |
|
21
|
|
|
|
|
22
|
|
|
public const FORMAT_NAME = '1'; |
|
23
|
|
|
public const FORMAT_FULL = '2'; |
|
24
|
|
|
public const FORMAT_SALTED_NAME = '3'; |
|
25
|
|
|
public const FORMAT_SALTED_FULL = '4'; |
|
26
|
|
|
public const FORMAT_RANDOM = '5'; |
|
27
|
|
|
|
|
28
|
|
|
/** @var array<int|string, class-string<AEncoder>> */ |
|
|
|
|
|
|
29
|
|
|
protected array $map = [ |
|
30
|
|
|
self::FORMAT_NAME => Name::class, |
|
31
|
|
|
self::FORMAT_FULL => FullPath::class, |
|
32
|
|
|
self::FORMAT_SALTED_NAME => SaltedName::class, |
|
33
|
|
|
self::FORMAT_SALTED_FULL => SaltedFullPath::class, |
|
34
|
|
|
self::FORMAT_RANDOM => Random::class, |
|
35
|
|
|
]; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param Config $config |
|
39
|
|
|
* @throws UploadException |
|
40
|
|
|
* @return AEncoder |
|
41
|
|
|
*/ |
|
42
|
27 |
|
public function getKeyEncoder(Config $config): AEncoder |
|
43
|
|
|
{ |
|
44
|
27 |
|
if (is_object($config->temporaryEncoder)) { |
|
45
|
21 |
|
return $this->checkObject($config->temporaryEncoder); |
|
46
|
|
|
} |
|
47
|
6 |
|
if (isset($this->map[strval($config->temporaryEncoder)])) { |
|
48
|
3 |
|
return $this->initDefined($this->map[strval($config->temporaryEncoder)]); |
|
49
|
|
|
} |
|
50
|
3 |
|
if (is_string($config->temporaryEncoder)) { |
|
51
|
2 |
|
return $this->initDefined($config->temporaryEncoder); |
|
52
|
|
|
} |
|
53
|
1 |
|
throw new UploadException($this->getUppLang()->uppTempEncoderVariantNotSet()); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param object $variant |
|
58
|
|
|
* @throws UploadException |
|
59
|
|
|
* @return AEncoder |
|
60
|
|
|
*/ |
|
61
|
24 |
|
protected function checkObject(object $variant): AEncoder |
|
62
|
|
|
{ |
|
63
|
24 |
|
if ($variant instanceof AEncoder) { |
|
64
|
23 |
|
return $variant; |
|
65
|
|
|
} |
|
66
|
1 |
|
throw new UploadException($this->getUppLang()->uppTempEncoderVariantIsWrong(get_class($variant))); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string $variant |
|
71
|
|
|
* @throws UploadException |
|
72
|
|
|
* @return AEncoder |
|
73
|
|
|
*/ |
|
74
|
5 |
|
protected function initDefined(string $variant): AEncoder |
|
75
|
|
|
{ |
|
76
|
|
|
try { |
|
77
|
|
|
/** @var class-string<AEncoder> $variant */ |
|
78
|
5 |
|
$ref = new ReflectionClass($variant); |
|
79
|
4 |
|
if ($ref->isInstantiable()) { |
|
80
|
3 |
|
return $this->checkObject($ref->newInstance($this->getUppLang())); |
|
81
|
|
|
} |
|
82
|
1 |
|
throw new UploadException($this->getUppLang()->uppTempEncoderVariantIsWrong($variant)); |
|
83
|
2 |
|
} catch (ReflectionException $ex) { |
|
84
|
1 |
|
throw new UploadException($ex->getMessage(), $ex->getCode(), $ex); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|