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