1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\UploadPerPartes\Target\Local\FinalStorage\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\FinalStorage\KeyEncoders |
16
|
|
|
* Factory to get final file name on target 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
|
|
|
|
27
|
|
|
/** @var array<int|string, class-string<AEncoder>> */ |
|
|
|
|
28
|
|
|
protected array $map = [ |
29
|
|
|
self::FORMAT_NAME => Name::class, |
30
|
|
|
self::FORMAT_FULL => FullPath::class, |
31
|
|
|
self::FORMAT_SALTED_NAME => SaltedName::class, |
32
|
|
|
self::FORMAT_SALTED_FULL => SaltedFullPath::class, |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param Config $config |
37
|
|
|
* @throws UploadException |
38
|
|
|
* @return AEncoder |
39
|
|
|
*/ |
40
|
|
|
public function getKeyEncoder(Config $config): AEncoder |
41
|
|
|
{ |
42
|
|
|
if (is_object($config->finalEncoder)) { |
43
|
|
|
return $this->checkObject($config->finalEncoder); |
44
|
|
|
} |
45
|
|
|
if (isset($this->map[strval($config->finalEncoder)])) { |
46
|
|
|
return $this->initDefined($this->map[strval($config->finalEncoder)]); |
47
|
|
|
} |
48
|
|
|
if (is_string($config->finalEncoder)) { |
49
|
|
|
return $this->initDefined($config->finalEncoder); |
50
|
|
|
} |
51
|
|
|
throw new UploadException($this->getUppLang()->uppFinalEncoderVariantNotSet()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param object $variant |
56
|
|
|
* @throws UploadException |
57
|
|
|
* @return AEncoder |
58
|
|
|
*/ |
59
|
|
|
protected function checkObject(object $variant): AEncoder |
60
|
|
|
{ |
61
|
|
|
if ($variant instanceof AEncoder) { |
62
|
|
|
return $variant; |
63
|
|
|
} |
64
|
|
|
throw new UploadException($this->getUppLang()->uppFinalEncoderVariantIsWrong(get_class($variant))); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $variant |
69
|
|
|
* @throws UploadException |
70
|
|
|
* @return AEncoder |
71
|
|
|
*/ |
72
|
|
|
protected function initDefined(string $variant): AEncoder |
73
|
|
|
{ |
74
|
|
|
try { |
75
|
|
|
/** @var class-string<AEncoder> $variant */ |
76
|
|
|
$ref = new ReflectionClass($variant); |
77
|
|
|
if ($ref->isInstantiable()) { |
78
|
|
|
return $this->checkObject($ref->newInstance($this->getUppLang())); |
79
|
|
|
} |
80
|
|
|
throw new UploadException($this->getUppLang()->uppFinalEncoderVariantIsWrong($variant)); |
81
|
|
|
} catch (ReflectionException $ex) { |
82
|
|
|
throw new UploadException($ex->getMessage(), $ex->getCode(), $ex); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|