1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Gerrie package. |
4
|
|
|
* |
5
|
|
|
* (c) Andreas Grunwald <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Gerrie\Transformer; |
12
|
|
|
|
13
|
|
|
class DebugTransformDecorator extends BaseTransformer |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var TransformerInterface |
18
|
|
|
*/ |
19
|
|
|
private $transformer; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Supported index names returned by the Gerrie API. |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $supportedKeys = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param TransformerInterface $transformer |
30
|
|
|
*/ |
31
|
|
|
public function __construct(TransformerInterface $transformer) |
32
|
|
|
{ |
33
|
|
|
$this->transformer = $transformer; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Set the data to transform |
38
|
|
|
* |
39
|
|
|
* @param array $data |
40
|
|
|
* @return void |
41
|
|
|
*/ |
42
|
|
|
public function setData(array $data) |
43
|
|
|
{ |
44
|
|
|
$this->transformer->setData($data); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Returns the data to transform |
49
|
|
|
* |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
|
public function getData() |
53
|
|
|
{ |
54
|
|
|
return $this->transformer->getData(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Checks if the current data transformer is responsible to transform the given data. |
59
|
|
|
* |
60
|
|
|
* @return boolean |
61
|
|
|
*/ |
62
|
|
|
public function isResponsible() |
63
|
|
|
{ |
64
|
|
|
return $this->transformer->isResponsible(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Transforms the data into one unique format |
69
|
|
|
* |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
public function transform() |
73
|
|
|
{ |
74
|
|
|
$originalData = $this->getData(); |
75
|
|
|
$transformedData = $this->transformer->transform(); |
76
|
|
|
|
77
|
|
|
$restData = $this->unsetKeys($originalData, $this->transformer->getSupportedKeys()); |
78
|
|
|
$this->checkIfAllValuesWereProceeded($restData, $this->transformer); |
79
|
|
|
|
80
|
|
|
return $transformedData; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Unsets an amount of keys in given $data |
85
|
|
|
* |
86
|
|
|
* @param array $data Data array where the keys will be unset |
87
|
|
|
* @param array $keyList List of keys which will be unset |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
private function unsetKeys(array $data, array $keyList) |
91
|
|
|
{ |
92
|
|
|
foreach ($keyList as $key) { |
93
|
|
|
if (isset($data[$key]) === true) { |
94
|
|
|
unset($data[$key]); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $data; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Method to check if all data were imported. |
103
|
|
|
* Normally, this export script unsets the exported value after proceeding. |
104
|
|
|
* |
105
|
|
|
* If there are values left in the array, there could be |
106
|
|
|
* a) a bug, because the value is not unsetted |
107
|
|
|
* b) a change in the Gerrit server API |
108
|
|
|
* c) a bug, because not all values are exported / proceeded |
109
|
|
|
* |
110
|
|
|
* This methods help to detect this :) |
111
|
|
|
* |
112
|
|
|
* @param array $data Data to inspect |
113
|
|
|
* @param TransformerInterface $transformer The transformer class |
114
|
|
|
* @throws \Exception |
115
|
|
|
*/ |
116
|
|
|
private function checkIfAllValuesWereProceeded(array $data, TransformerInterface $transformer) |
117
|
|
|
{ |
118
|
|
|
if (count($data) > 0) { |
119
|
|
|
var_dump($data); |
|
|
|
|
120
|
|
|
$message = 'Not all values were proceeded / exported. Please have a look at "%s"'; |
121
|
|
|
$message = sprintf($message, get_class($transformer)); |
122
|
|
|
throw new \RuntimeException($message, 1363894644); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |