1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Oliverde8\Component\PhpEtl\Builder\Factories; |
6
|
|
|
|
7
|
|
|
use Oliverde8\Component\PhpEtl\ChainOperation\ChainOperationInterface; |
8
|
|
|
use Oliverde8\Component\PhpEtl\Exception\ChainBuilderValidationException; |
9
|
|
|
use Symfony\Component\Validator\Constraint; |
10
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
11
|
|
|
use Symfony\Component\Validator\Validation; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class AbstractFactory |
15
|
|
|
* |
16
|
|
|
* @author de Cramer Oliver<[email protected]> |
17
|
|
|
* @copyright 2018 Oliverde8 |
18
|
|
|
* @package Oliverde8\Component\PhpEtl\Builder\Factories |
19
|
|
|
*/ |
20
|
|
|
abstract class AbstractFactory |
21
|
|
|
{ |
22
|
|
|
/** @var string The operation type */ |
23
|
|
|
protected $operation; |
24
|
|
|
|
25
|
|
|
/** @var string Class to built */ |
26
|
|
|
protected $class; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* AbstractFactory constructor. |
30
|
|
|
* |
31
|
|
|
* @param string $operation |
32
|
|
|
* @param string $class |
33
|
|
|
*/ |
34
|
|
|
public function __construct(string $operation, string $class) |
35
|
|
|
{ |
36
|
|
|
$this->operation = $operation; |
37
|
|
|
$this->class = $class; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Validate and Build an operation of a certain type with the options. |
42
|
|
|
* |
43
|
|
|
* @param string $operation |
44
|
|
|
* @param array $options |
45
|
|
|
* |
46
|
|
|
* @return ChainOperationInterface |
47
|
|
|
* @throws ChainBuilderValidationException |
48
|
|
|
*/ |
49
|
|
|
public function getOperation(string $operation, array $options): ChainOperationInterface |
50
|
|
|
{ |
51
|
|
|
$this->validateOptions($operation, $options); |
52
|
|
|
return $this->build($operation, $options); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Build an operation of a certain type with the options. |
57
|
|
|
* |
58
|
|
|
* @param String $operation |
59
|
|
|
* @param array $options |
60
|
|
|
* |
61
|
|
|
* @return ChainOperationInterface |
62
|
|
|
*/ |
63
|
|
|
abstract protected function build(string $operation, array $options): ChainOperationInterface; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Configure validation. |
67
|
|
|
* |
68
|
|
|
* @return Constraint |
69
|
|
|
*/ |
70
|
|
|
protected function configureValidator(): Constraint |
71
|
|
|
{ |
72
|
|
|
return new Assert\Collection([]); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param mixed ...$arguments |
77
|
|
|
* @return ChainOperationInterface |
78
|
|
|
*/ |
79
|
|
|
protected function create(...$arguments): ChainOperationInterface |
80
|
|
|
{ |
81
|
|
|
$class = $this->class; |
82
|
|
|
return new $class(...$arguments); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Validate the options. |
87
|
|
|
* |
88
|
|
|
* @param string $operation |
89
|
|
|
* @param array $options |
90
|
|
|
* @throws ChainBuilderValidationException |
91
|
|
|
*/ |
92
|
|
|
protected function validateOptions(string $operation, array $options): void |
93
|
|
|
{ |
94
|
|
|
$constraints = $this->configureValidator(); |
95
|
|
|
$violations = Validation::createValidator()->validate($options, $constraints); |
96
|
|
|
|
97
|
|
|
if ($violations->count() != 0) { |
98
|
|
|
throw new ChainBuilderValidationException($operation, iterator_to_array($violations)); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Check if the factory supports this operation declaration. |
104
|
|
|
* |
105
|
|
|
* @param string $operation |
106
|
|
|
* @param array $options |
107
|
|
|
* |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
|
|
public function supports(string $operation, array $options) |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
return $this->operation == $operation; |
113
|
|
|
} |
114
|
|
|
} |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.