1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mcustiel\PowerRoute\Common\Factories; |
4
|
|
|
|
5
|
|
|
use Mcustiel\Creature\LazyCreator; |
6
|
|
|
use Mcustiel\PowerRoute\Actions\GoToAction; |
7
|
|
|
use Mcustiel\PowerRoute\Common\Conditions\ClassArgumentObject; |
8
|
|
|
use Mcustiel\PowerRoute\PowerRoute; |
9
|
|
|
|
10
|
|
|
class ActionFactory extends Mapping |
11
|
|
|
{ |
12
|
3 |
|
public function __construct(array $mapping) |
13
|
|
|
{ |
14
|
3 |
|
parent::__construct(array_merge(['goto' => new LazyCreator(GoToAction::class)], $mapping)); |
15
|
3 |
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param array $config |
19
|
|
|
* |
20
|
|
|
* @return \Mcustiel\PowerRoute\Common\Conditions\ClassArgumentObject[] |
21
|
|
|
*/ |
22
|
3 |
|
public function createFromConfig(array $config, PowerRoute $executor) |
23
|
|
|
{ |
24
|
3 |
|
$actions = []; |
25
|
|
|
|
26
|
3 |
|
foreach ($config as $actionData) { |
27
|
3 |
|
$actions[] = $this->createActionFromConfig($actionData, $executor); |
28
|
3 |
|
} |
29
|
|
|
|
30
|
3 |
|
return $actions; |
31
|
|
|
} |
32
|
|
|
|
33
|
3 |
|
private function createActionFromConfig($actionData, $executor) |
34
|
|
|
{ |
35
|
3 |
|
$class = key($actionData); |
36
|
3 |
|
$this->checkMappingIsValid($class); |
37
|
|
|
|
38
|
3 |
|
return new ClassArgumentObject( |
39
|
3 |
|
$this->mapping[$class]->getInstance(), |
40
|
3 |
|
$this->getConstructorArgument($executor, $actionData[$class], $class) |
41
|
3 |
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
3 |
|
private function getConstructorArgument($executor, $argument, $id) |
45
|
|
|
{ |
46
|
3 |
|
if ($id === 'goto') { |
47
|
2 |
|
$classArgument = new \stdClass(); |
48
|
2 |
|
$classArgument->route = $argument; |
49
|
2 |
|
$classArgument->executor = $executor; |
50
|
2 |
|
} else { |
51
|
3 |
|
$classArgument = $argument; |
52
|
|
|
} |
53
|
|
|
|
54
|
3 |
|
return $classArgument; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|