1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright 2016 Jan Eichhorn <[email protected]> |
4
|
|
|
* |
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
6
|
|
|
* you may not use this file except in compliance with the License. |
7
|
|
|
* You may obtain a copy of the License at |
8
|
|
|
* |
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
10
|
|
|
* |
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14
|
|
|
* See the License for the specific language governing permissions and |
15
|
|
|
* limitations under the License. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace ApaiIO\Operations; |
19
|
|
|
|
20
|
|
|
class Batch implements OperationInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var OperationInterface[] |
24
|
|
|
*/ |
25
|
|
|
private $operations = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $operationName; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Batch constructor. |
34
|
|
|
* |
35
|
|
|
* @param OperationInterface[] |
36
|
|
|
*/ |
37
|
1 |
|
public function __construct(array $operations = []) |
38
|
|
|
{ |
39
|
1 |
|
foreach ($operations as $operation) { |
40
|
1 |
|
$this->addOperation($operation); |
41
|
|
|
} |
42
|
1 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Adds a single operation. |
46
|
|
|
* Note that only operations with the same operation name can be added. |
47
|
|
|
* First operation which is added will be the reference and the instance will let you only add |
48
|
|
|
* other operations with the same operation name. |
49
|
|
|
* |
50
|
|
|
* @param OperationInterface $operation |
51
|
|
|
* |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
1 |
|
public function addOperation(OperationInterface $operation) |
55
|
|
|
{ |
56
|
1 |
|
if (null === $this->operationName) { |
57
|
1 |
|
$this->operationName = $operation->getName(); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
if ($this->operationName !== $operation->getName()) { |
61
|
1 |
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
$this->operations[] = $operation; |
65
|
1 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
1 |
|
public function getName() |
71
|
|
|
{ |
72
|
1 |
|
return $this->operationName; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
1 |
|
public function getOperationParameter() |
79
|
|
|
{ |
80
|
1 |
|
$parameter = []; |
81
|
1 |
|
$index = 1; |
82
|
1 |
|
foreach ($this->operations as $operation) { |
83
|
1 |
|
foreach ($operation->getOperationParameter() as $key => $value) { |
84
|
1 |
|
$keyName = sprintf('%s.%s.%s', $this->operationName, $index, $key); |
85
|
1 |
|
$parameter[$keyName] = $value; |
86
|
|
|
} |
87
|
1 |
|
$index++; |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
return $parameter; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|