|
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
|
|
|
public function __construct(array $operations = []) |
|
38
|
|
|
{ |
|
39
|
|
|
foreach ($operations as $operation) { |
|
40
|
|
|
$this->addOperation($operation); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
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
|
|
|
public function addOperation(OperationInterface $operation) |
|
55
|
|
|
{ |
|
56
|
|
|
if (null === $this->operationName) { |
|
57
|
|
|
$this->operationName = $operation->getName(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if ($this->operationName !== $operation->getName()) { |
|
61
|
|
|
return; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$this->operations[] = $operation; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* {@inheritdoc} |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getName() |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->operationName; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* {@inheritdoc} |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getOperationParameter() |
|
79
|
|
|
{ |
|
80
|
|
|
$parameter = []; |
|
81
|
|
|
$index = 1; |
|
82
|
|
|
foreach ($this->operations as $operation) { |
|
83
|
|
|
foreach ($operation->getOperationParameter() as $key => $value) { |
|
84
|
|
|
$keyName = sprintf('%s.%s.%s', $this->operationName, $index, $key); |
|
85
|
|
|
$parameter[$keyName] = $value; |
|
86
|
|
|
} |
|
87
|
|
|
$index++; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $parameter; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|