1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nayjest\Querying\Handler; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
use Nayjest\Querying\Handler\PostProcess\AddFieldHandler; |
8
|
|
|
use Nayjest\Querying\Operation\AddFieldOperation; |
9
|
|
|
use Nayjest\Querying\Operation\CloneOperation; |
10
|
|
|
use Nayjest\Querying\Operation\CustomOperation; |
11
|
|
|
use Nayjest\Querying\Operation\DummyOperation; |
12
|
|
|
use Nayjest\Querying\Operation\InitializeRowsOperation; |
13
|
|
|
use Nayjest\Querying\Operation\OperationInterface; |
14
|
|
|
|
15
|
|
|
abstract class AbstractFactory |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @return array |
19
|
|
|
*/ |
20
|
|
|
abstract protected function getRegisteredHandlers(); |
21
|
|
|
|
22
|
11 |
|
protected function getDefaultHandlers() |
23
|
|
|
{ |
24
|
|
|
return [ |
25
|
11 |
|
DummyOperation::class => DummyHandler::class, |
26
|
11 |
|
InitializeRowsOperation::class => InitializeRowsHandler::class, |
27
|
11 |
|
AddFieldOperation::class => AddFieldHandler::class, |
28
|
11 |
|
CustomOperation::class => CustomHandler::class, |
29
|
11 |
|
CloneOperation::class => CloneHandler::class, |
30
|
11 |
|
]; |
31
|
|
|
} |
32
|
|
|
|
33
|
11 |
|
final protected function getHandlers() |
34
|
|
|
{ |
35
|
11 |
|
return array_merge( |
36
|
11 |
|
$this->getDefaultHandlers(), |
37
|
11 |
|
$this->getRegisteredHandlers() |
38
|
11 |
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
11 |
|
public function makeHandler(OperationInterface $operation) |
42
|
|
|
{ |
43
|
11 |
|
$operationClass = get_class($operation); |
44
|
11 |
|
$classes = $this->getHandlers(); |
45
|
11 |
|
if (array_key_exists($operationClass, $classes)) { |
46
|
11 |
|
$handlerClass = $classes[$operationClass]; |
47
|
11 |
|
return new $handlerClass($operation); |
48
|
|
|
} |
49
|
|
|
throw new InvalidArgumentException("No handler for '$operationClass' operation."); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|