1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\UploadPerPartes\Target\Local\DrivingFile\DataEncoders; |
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\DrivingFile\DataEncoders |
16
|
|
|
* Drive file format - Factory to get formats |
17
|
|
|
* |
18
|
|
|
* How to pack and unpack data in info file on server |
19
|
|
|
*/ |
20
|
|
|
class Factory |
21
|
|
|
{ |
22
|
|
|
use TLangInit; |
23
|
|
|
|
24
|
|
|
public const FORMAT_TEXT = '1'; |
25
|
|
|
public const FORMAT_JSON = '2'; |
26
|
|
|
public const FORMAT_LINE = '3'; |
27
|
|
|
public const FORMAT_SERIAL = '4'; |
28
|
|
|
|
29
|
|
|
/** @var array<string|int, class-string<AEncoder>> */ |
|
|
|
|
30
|
|
|
protected array $map = [ |
31
|
|
|
self::FORMAT_TEXT => Text::class, |
32
|
|
|
self::FORMAT_JSON => Json::class, |
33
|
|
|
self::FORMAT_LINE => Line::class, |
34
|
|
|
self::FORMAT_SERIAL => Serialize::class, |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param Config $config |
39
|
|
|
* @throws UploadException |
40
|
|
|
* @return AEncoder |
41
|
|
|
*/ |
42
|
27 |
|
public function getDataEncoder(Config $config): AEncoder |
43
|
|
|
{ |
44
|
27 |
|
if (is_object($config->dataEncoder)) { |
45
|
19 |
|
return $this->checkObject($config->dataEncoder); |
46
|
|
|
} |
47
|
8 |
|
if (isset($this->map[$config->dataEncoder])) { |
48
|
3 |
|
return $this->initDefined($this->map[$config->dataEncoder]); |
49
|
|
|
} |
50
|
5 |
|
if (is_string($config->dataEncoder)) { |
51
|
4 |
|
return $this->initDefined($config->dataEncoder); |
52
|
|
|
} |
53
|
1 |
|
throw new UploadException($this->getUppLang()->uppDataEncoderVariantNotSet()); |
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()->uppDataEncoderVariantIsWrong(get_class($variant))); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $variant |
71
|
|
|
* @throws UploadException |
72
|
|
|
* @return AEncoder |
73
|
|
|
*/ |
74
|
7 |
|
protected function initDefined(string $variant): AEncoder |
75
|
|
|
{ |
76
|
|
|
try { |
77
|
|
|
/** @var class-string<AEncoder> $variant */ |
78
|
7 |
|
$ref = new ReflectionClass($variant); |
79
|
6 |
|
if ($ref->isInstantiable()) { |
80
|
5 |
|
return $this->checkObject($ref->newInstance($this->getUppLang())); |
81
|
|
|
} |
82
|
1 |
|
throw new UploadException($this->getUppLang()->uppDataEncoderVariantIsWrong($variant)); |
83
|
2 |
|
} catch (ReflectionException $ex) { |
84
|
1 |
|
throw new UploadException($ex->getMessage(), $ex->getCode(), $ex); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|