1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace tkanstantsin\fileupload\config; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class Factory |
8
|
|
|
*/ |
9
|
|
|
class Factory |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @see Alias |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
public $defaultAliasConfig; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var Alias[] |
19
|
|
|
*/ |
20
|
|
|
protected $aliasArray = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Prepares aliases config in proper view. |
24
|
|
|
* @param array $defaultAliasConfig |
25
|
|
|
* @param array $aliasArray |
26
|
|
|
* @return array |
27
|
|
|
* @throws InvalidConfigException |
28
|
|
|
*/ |
29
|
|
|
public static function prepareAliases(array $defaultAliasConfig, array $aliasArray): array |
30
|
|
|
{ |
31
|
|
|
$factory = new static($defaultAliasConfig); |
32
|
|
|
foreach ($aliasArray as $name => $config) { |
33
|
|
|
$name = (string) $name; |
34
|
|
|
$aliasArray[$name] = $factory->createAlias($name, $config); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return $aliasArray; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param array $defaultAliasConfig |
42
|
|
|
* @return Factory |
43
|
|
|
*/ |
44
|
1 |
|
public static function build(array $defaultAliasConfig): self |
45
|
|
|
{ |
46
|
1 |
|
return new static($defaultAliasConfig); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Factory constructor. |
51
|
|
|
* @param array $defaultAliasConfig |
52
|
|
|
*/ |
53
|
1 |
|
public function __construct(array $defaultAliasConfig) |
54
|
|
|
{ |
55
|
1 |
|
$this->defaultAliasConfig = $defaultAliasConfig; |
56
|
1 |
|
$this->defaultAliasConfig['multiple'] = ($this->defaultAliasConfig['max_count'] ?? 0) > 1; |
57
|
1 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Add config |
61
|
|
|
* @param string $name |
62
|
|
|
* @param $config |
63
|
|
|
* @throws InvalidConfigException |
64
|
|
|
*/ |
65
|
1 |
|
public function add(string $name, $config): void |
66
|
|
|
{ |
67
|
1 |
|
$this->aliasArray[$name] = $this->createAlias($name, $config); |
68
|
1 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param array $configArray |
72
|
|
|
* @throws InvalidConfigException |
73
|
|
|
*/ |
74
|
1 |
|
public function addMultiple(array $configArray): void |
75
|
|
|
{ |
76
|
1 |
|
foreach ($configArray as $name => $config) { |
77
|
1 |
|
$this->add((string) $name, $config); |
78
|
|
|
} |
79
|
1 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $name |
83
|
|
|
* @return Alias |
84
|
|
|
* @throws \RuntimeException |
85
|
|
|
*/ |
86
|
1 |
|
public function getAliasConfig(string $name): Alias |
87
|
|
|
{ |
88
|
1 |
|
$aliasConfig = $this->aliasArray[$name] ?? null; |
89
|
1 |
|
if ($aliasConfig === null) { |
90
|
|
|
throw new \RuntimeException(sprintf('Alias with key `%s` not defined.', $name)); |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
return $aliasConfig; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string|int $name |
98
|
|
|
* @param array|string $config |
99
|
|
|
* @return Alias |
100
|
|
|
* @throws InvalidConfigException |
101
|
|
|
*/ |
102
|
1 |
|
protected function createAlias(string $name, $config): Alias |
103
|
|
|
{ |
104
|
1 |
|
if (!\is_string($config) && !\is_array($config)) { |
|
|
|
|
105
|
|
|
throw new InvalidConfigException('Invalid alias config for fileupload.'); |
106
|
|
|
} |
107
|
1 |
|
if (\is_string($config)) { // using default config |
108
|
|
|
$config = [ |
109
|
|
|
'class' => $config, |
110
|
|
|
]; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// add default options |
114
|
1 |
|
$config = array_replace($this->defaultAliasConfig, $config); |
115
|
1 |
|
$config = array_merge($config, [ |
116
|
1 |
|
'name' => $name, |
117
|
1 |
|
'directory' => $config['directory'] ?? $name, |
118
|
1 |
|
'multiple' => $config['maxCount'] > 1, |
119
|
|
|
]); |
120
|
|
|
|
121
|
1 |
|
if (!\in_array($config['hashMethod'], hash_algos(), true)) { |
122
|
|
|
throw new InvalidConfigException(sprintf('Hash method `%s` not found.', $config['hashMethod'])); |
123
|
|
|
} |
124
|
|
|
|
125
|
1 |
|
return new Alias($config); |
126
|
|
|
} |
127
|
|
|
} |