1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_modules\Mixer\PassedParams; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_modules\Interfaces\IMdTranslations; |
7
|
|
|
use kalanis\kw_modules\Interfaces\IModule; |
8
|
|
|
use kalanis\kw_modules\ModuleException; |
9
|
|
|
use kalanis\kw_modules\Traits\TMdLang; |
10
|
|
|
use ReflectionClass; |
11
|
|
|
use ReflectionException; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Factory |
16
|
|
|
* @package kalanis\kw_modules\Mixer\PassedParams |
17
|
|
|
* Which way the params will be transformed? |
18
|
|
|
*/ |
19
|
|
|
class Factory |
20
|
|
|
{ |
21
|
|
|
use TMdLang; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array<string, class-string<APassedParam>> |
|
|
|
|
25
|
|
|
*/ |
26
|
|
|
protected array $map = [ |
27
|
|
|
'http' => HttpQuery::class, |
28
|
|
|
'single' => SingleParam::class, |
29
|
|
|
]; |
30
|
|
|
|
31
|
22 |
|
public function __construct(?IMdTranslations $lang = null) |
32
|
|
|
{ |
33
|
22 |
|
$this->setMdLang($lang); |
34
|
22 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param IModule $module |
38
|
|
|
* @throws ModuleException |
39
|
|
|
* @return APassedParam |
40
|
|
|
*/ |
41
|
7 |
|
public function getClass(IModule $module): APassedParam |
42
|
|
|
{ |
43
|
7 |
|
if (method_exists($module, 'passParamsAs')) { |
44
|
3 |
|
$passAs = strval($module->passParamsAs()); |
45
|
|
|
try { |
46
|
3 |
|
return $this->getLoaded($passAs); |
47
|
2 |
|
} catch (ReflectionException $ex) { |
48
|
|
|
// nothing |
49
|
|
|
} |
50
|
|
|
try { |
51
|
2 |
|
return $this->getLoaded($this->fromMap($passAs)); |
52
|
1 |
|
} catch (ReflectionException $ex) { |
53
|
|
|
// nothing |
54
|
|
|
} |
55
|
1 |
|
throw new ModuleException($this->getMdLang()->mdNoModuleFound()); |
56
|
|
|
} |
57
|
|
|
try { |
58
|
4 |
|
return $this->getLoaded($this->fromMap('http')); |
59
|
1 |
|
} catch (ReflectionException $ex) { |
60
|
1 |
|
throw new ModuleException($ex->getMessage(), $ex->getCode(), $ex); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
6 |
|
protected function fromMap(string $passAs): ?string |
65
|
|
|
{ |
66
|
6 |
|
if (isset($this->map[$passAs])) { |
67
|
4 |
|
return $this->map[$passAs]; |
68
|
|
|
} |
69
|
2 |
|
return null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string|null $className |
74
|
|
|
* @throws ReflectionException |
75
|
|
|
* @return APassedParam |
76
|
|
|
*/ |
77
|
7 |
|
protected function getLoaded(?string $className): APassedParam |
78
|
|
|
{ |
79
|
7 |
|
if ($className) { |
80
|
|
|
/** @var class-string $className */ |
81
|
6 |
|
$ref = new ReflectionClass($className); |
82
|
5 |
|
$class = $ref->newInstance(); |
83
|
5 |
|
if ($class && $class instanceof APassedParam) { |
84
|
5 |
|
return $class; |
85
|
|
|
} |
86
|
|
|
} |
87
|
2 |
|
throw new ReflectionException($this->getMdLang()->mdNoModuleFound()); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|